1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Created by PhpStorm. |
5
|
|
|
* Project: json-rpc-server |
6
|
|
|
* User: sv |
7
|
|
|
* Date: 05.02.2021 |
8
|
|
|
* Time: 23:37 |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace Onnov\JsonRpcServer\Error; |
14
|
|
|
|
15
|
|
|
use Onnov\JsonRpcServer\Definition\RpcErrorDefinition; |
16
|
|
|
use Psr\Log\LogLevel; |
17
|
|
|
use Throwable; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class ErrorService |
21
|
|
|
* |
22
|
|
|
* @package Onnov\JsonRpcServer\Service |
23
|
|
|
*/ |
24
|
|
|
class RpcError |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var RpcErrorDefinition[] |
28
|
|
|
*/ |
29
|
|
|
private $errors; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* ErrorService constructor. |
33
|
|
|
* |
34
|
|
|
* @param RpcErrorDefinition $authError |
35
|
|
|
*/ |
36
|
9 |
|
public function __construct(RpcErrorDefinition $authError) |
37
|
|
|
{ |
38
|
9 |
|
$this->errors = [ |
39
|
9 |
|
'InvalidAuthorizeException' => $authError, |
40
|
9 |
|
'MethodErrorException' => new RpcErrorDefinition( |
41
|
9 |
|
[ |
42
|
9 |
|
'code' => 600, |
43
|
9 |
|
'message' => 'Error', |
44
|
9 |
|
'errorLevel' => LogLevel::NOTICE, |
45
|
9 |
|
] |
46
|
9 |
|
), |
47
|
9 |
|
'ParseErrorException' => new RpcErrorDefinition( |
48
|
9 |
|
[ |
49
|
9 |
|
'code' => -32700, |
50
|
9 |
|
'message' => 'Parse error', |
51
|
9 |
|
'errorLevel' => LogLevel::ERROR, |
52
|
9 |
|
] |
53
|
9 |
|
), |
54
|
9 |
|
'InvalidRequestException' => new RpcErrorDefinition( |
55
|
9 |
|
[ |
56
|
9 |
|
'code' => -32600, |
57
|
9 |
|
'message' => 'Invalid Request', |
58
|
9 |
|
'errorLevel' => LogLevel::ERROR, |
59
|
9 |
|
] |
60
|
9 |
|
), |
61
|
9 |
|
'MethodNotFoundException' => new RpcErrorDefinition( |
62
|
9 |
|
[ |
63
|
9 |
|
'code' => -32601, |
64
|
9 |
|
'message' => 'Method not found', |
65
|
9 |
|
'errorLevel' => LogLevel::ERROR, |
66
|
9 |
|
] |
67
|
9 |
|
), |
68
|
9 |
|
'InvalidParamsException' => new RpcErrorDefinition( |
69
|
9 |
|
[ |
70
|
9 |
|
'code' => -32602, |
71
|
9 |
|
'message' => 'Invalid params', |
72
|
9 |
|
'errorLevel' => LogLevel::ERROR, |
73
|
9 |
|
] |
74
|
9 |
|
), |
75
|
9 |
|
'Throwable' => new RpcErrorDefinition( |
76
|
9 |
|
[ |
77
|
9 |
|
'code' => -32603, |
78
|
9 |
|
'message' => 'Internal error', |
79
|
9 |
|
'errorLevel' => LogLevel::EMERGENCY, |
80
|
9 |
|
] |
81
|
9 |
|
), |
82
|
9 |
|
]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $errorName |
87
|
|
|
* @param Throwable|null $throw |
88
|
|
|
* @return RpcErrorDefinition |
89
|
|
|
*/ |
90
|
4 |
|
public function getErrorByName(string $errorName, Throwable $throw = null): RpcErrorDefinition |
91
|
|
|
{ |
92
|
4 |
|
$error = $this->errors['Throwable']; |
93
|
4 |
|
if ($throw !== null && isset($this->errors[$errorName])) { |
94
|
4 |
|
$error = $this->errors[$errorName]; |
95
|
|
|
|
96
|
4 |
|
if ($throw->getCode() !== 0) { |
97
|
|
|
$error->setCode($throw->getCode()); |
98
|
|
|
} |
99
|
4 |
|
if ($throw->getMessage() !== '') { |
100
|
1 |
|
$error->setMessage($throw->getMessage()); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
4 |
|
return $error; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $errorName |
109
|
|
|
* @param RpcErrorDefinition $error |
110
|
|
|
*/ |
111
|
|
|
public function addOrReplaceError(string $errorName, RpcErrorDefinition $error): void |
112
|
|
|
{ |
113
|
|
|
$this->errors[$errorName] = $error; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return RpcErrorDefinition[] |
118
|
|
|
*/ |
119
|
|
|
public function getErrors(): array |
120
|
|
|
{ |
121
|
|
|
return $this->errors; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param RpcErrorDefinition[] $errors |
126
|
|
|
*/ |
127
|
|
|
public function setErrors(array $errors): void |
128
|
|
|
{ |
129
|
|
|
$this->errors = $errors; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|