1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tonic\Component\ApiLayer\JsonRpc\Response; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Represents error. |
7
|
|
|
*/ |
8
|
|
|
class ErrorResponse extends AbstractResponse |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Parse error. |
12
|
|
|
*/ |
13
|
|
|
const CODE_PARSE_ERROR = -32700; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Invalid request. |
17
|
|
|
*/ |
18
|
|
|
const CODE_INVALID_REQUEST_ERROR = -32600; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Method not found. |
22
|
|
|
*/ |
23
|
|
|
const CODE_METHOD_NOT_FOUND_ERROR = -32601; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Invalid params. |
27
|
|
|
*/ |
28
|
|
|
const CODE_INVALID_PARAMS_ERROR = -32602; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Internal application error. |
32
|
|
|
*/ |
33
|
|
|
const CODE_APPLICATION_ERROR = -32603; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var Error |
37
|
|
|
*/ |
38
|
|
|
private $error; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $message |
42
|
|
|
* |
43
|
|
|
* @return ErrorResponse |
44
|
|
|
*/ |
45
|
|
|
public static function parseError($message = 'Parse error') |
46
|
|
|
{ |
47
|
|
|
return new static(null, new Error($message, self::CODE_PARSE_ERROR)); // @codingStandardsIgnoreLine |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $message |
52
|
|
|
* |
53
|
|
|
* @return ErrorResponse |
54
|
|
|
*/ |
55
|
|
|
public static function invalidRequestError($message = 'Invalid request') |
56
|
|
|
{ |
57
|
|
|
return new static(null, new Error($message, self::CODE_INVALID_REQUEST_ERROR)); // @codingStandardsIgnoreLine |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $id |
62
|
|
|
* @param string $message |
63
|
|
|
* |
64
|
|
|
* @return ErrorResponse |
65
|
|
|
*/ |
66
|
|
|
public static function invalidParamsError($id, $message = 'Invalid params') |
67
|
|
|
{ |
68
|
|
|
return new static($id, new Error($message, self::CODE_INVALID_PARAMS_ERROR)); // @codingStandardsIgnoreLine |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $id |
73
|
|
|
* @param string $message |
74
|
|
|
* |
75
|
|
|
* @return ErrorResponse |
76
|
|
|
*/ |
77
|
|
|
public static function methodNotFoundError($id, $message = 'Method not found') |
78
|
|
|
{ |
79
|
|
|
return new static($id, new Error($message, self::CODE_METHOD_NOT_FOUND_ERROR)); // @codingStandardsIgnoreLine |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $id |
84
|
|
|
* @param string $message |
85
|
|
|
* |
86
|
|
|
* @return ErrorResponse |
87
|
|
|
*/ |
88
|
|
|
public static function applicationError($id, $message = 'Internal error') |
89
|
|
|
{ |
90
|
|
|
return new static($id, new Error($message, self::CODE_APPLICATION_ERROR)); // @codingStandardsIgnoreLine |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $id |
95
|
|
|
* @param string $message |
96
|
|
|
* @param int $code |
97
|
|
|
* |
98
|
|
|
* @return ErrorResponse |
99
|
|
|
*/ |
100
|
|
|
public static function applicationDefinedError($id, $message, $code) |
101
|
|
|
{ |
102
|
|
|
return new static($id, new Error($message, $code)); // @codingStandardsIgnoreLine |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Constructor. |
107
|
|
|
* |
108
|
|
|
* @param mixed $id |
109
|
|
|
* @param Error $error |
110
|
|
|
*/ |
111
|
|
|
public function __construct($id, Error $error) |
112
|
|
|
{ |
113
|
|
|
parent::__construct($id); |
114
|
|
|
|
115
|
|
|
$this->error = $error; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return Error |
120
|
|
|
*/ |
121
|
|
|
public function getError() |
122
|
|
|
{ |
123
|
|
|
return $this->error; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return array |
128
|
|
|
*/ |
129
|
|
|
public function toArray() |
130
|
|
|
{ |
131
|
|
|
return array_merge(parent::toArray(), [ |
132
|
|
|
'error' => $this->getError()->toArray(), |
133
|
|
|
]); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|