|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace UMA\JsonRpc; |
|
6
|
|
|
|
|
7
|
|
|
final class Error extends Response |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var int |
|
11
|
|
|
*/ |
|
12
|
|
|
private $code; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
private $message; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var mixed |
|
21
|
|
|
*/ |
|
22
|
|
|
private $data; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param int $code |
|
26
|
|
|
* @param string $message |
|
27
|
|
|
* @param mixed $data |
|
28
|
|
|
* @param int|string|null $id |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(int $code, string $message, $data = null, $id = null) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->code = $code; |
|
33
|
|
|
$this->message = $message; |
|
34
|
|
|
$this->data = $data; |
|
35
|
|
|
$this->id = $id; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public static function parsing(): Error |
|
39
|
|
|
{ |
|
40
|
|
|
return new static(-32700, 'Parse error'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public static function invalidRequest(): Error |
|
44
|
|
|
{ |
|
45
|
|
|
return new static(-32600, 'Invalid Request'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public static function unknownMethod($id): Error |
|
49
|
|
|
{ |
|
50
|
|
|
return new static(-32601, 'Method not found', null, $id); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public static function invalidParams($id): Error |
|
54
|
|
|
{ |
|
55
|
|
|
return new static(-32602, 'Invalid params', null, $id); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public static function internal($id): Error |
|
59
|
|
|
{ |
|
60
|
|
|
return new static(-32603, 'Internal error', null, $id); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public static function tooManyBatchRequests(int $limit): Error |
|
64
|
|
|
{ |
|
65
|
|
|
return new static(-32000, 'Too many batch requests sent to server', ['limit' => $limit]); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function jsonSerialize(): array |
|
69
|
|
|
{ |
|
70
|
|
|
$payload = [ |
|
71
|
|
|
'jsonrpc' => '2.0', |
|
72
|
|
|
'error' => [ |
|
73
|
|
|
'code' => $this->code, |
|
74
|
|
|
'message' => $this->message |
|
75
|
|
|
], |
|
76
|
|
|
'id' => $this->id |
|
77
|
|
|
]; |
|
78
|
|
|
|
|
79
|
|
|
if (null !== $this->data) { |
|
80
|
|
|
$payload['error']['data'] = $this->data; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $payload; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|