forked from modelcontextprotocol/php-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathError.php
More file actions
147 lines (129 loc) · 4.48 KB
/
Error.php
File metadata and controls
147 lines (129 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
/*
* This file is part of the official PHP MCP SDK.
*
* A collaboration between Symfony and the PHP Foundation.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mcp\Schema\JsonRpc;
use Mcp\Exception\InvalidArgumentException;
/**
* A response to a request that indicates an error occurred.
*
* @phpstan-type ErrorData array{
* jsonrpc: string,
* id: string|int,
* code: int,
* message: string,
* data?: mixed,
* }
*
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
*/
class Error implements MessageInterface
{
public const PARSE_ERROR = -32700;
public const INVALID_REQUEST = -32600;
public const METHOD_NOT_FOUND = -32601;
public const INVALID_PARAMS = -32602;
public const INTERNAL_ERROR = -32603;
public const SERVER_ERROR = -32000;
public const RESOURCE_NOT_FOUND = -32002;
/**
* @param int $code the error type that occurred
* @param string $message a short description of the error
* @param mixed|null $data additional information about the error
*/
public function __construct(
public readonly string|int $id,
public readonly int $code,
public readonly string $message,
public readonly mixed $data = null,
) {
}
/**
* @param ErrorData $data
*/
final public static function fromArray(array $data): self
{
if (!isset($data['jsonrpc']) || MessageInterface::JSONRPC_VERSION !== $data['jsonrpc']) {
throw new InvalidArgumentException('Invalid or missing "jsonrpc" in Error data.');
}
if (!isset($data['id'])) {
throw new InvalidArgumentException('Invalid or missing "id" in Error data.');
}
if (!\is_string($data['id']) && !\is_int($data['id'])) {
throw new InvalidArgumentException('Invalid "id" type in Error data.');
}
if (!isset($data['error']) || !\is_array($data['error'])) {
throw new InvalidArgumentException('Invalid or missing "error" field in Error data.');
}
if (!isset($data['error']['code']) || !\is_int($data['error']['code'])) {
throw new InvalidArgumentException('Invalid or missing "code" in Error data.');
}
if (!isset($data['error']['message']) || !\is_string($data['error']['message'])) {
throw new InvalidArgumentException('Invalid or missing "message" in Error data.');
}
return new self($data['id'], $data['error']['code'], $data['error']['message'], $data['error']['data'] ?? null);
}
final public static function forParseError(string $message, string|int $id = ''): self
{
return new self($id, self::PARSE_ERROR, $message);
}
final public static function forInvalidRequest(string $message, string|int $id = ''): self
{
return new self($id, self::INVALID_REQUEST, $message);
}
final public static function forMethodNotFound(string $message, string|int $id = ''): self
{
return new self($id, self::METHOD_NOT_FOUND, $message);
}
final public static function forInvalidParams(string $message, string|int $id = '', mixed $data = null): self
{
return new self($id, self::INVALID_PARAMS, $message, $data);
}
final public static function forInternalError(string $message, string|int $id = ''): self
{
return new self($id, self::INTERNAL_ERROR, $message);
}
final public static function forServerError(string $message, string|int $id = ''): self
{
return new self($id, self::SERVER_ERROR, $message);
}
final public static function forResourceNotFound(string $message, string|int $id = ''): self
{
return new self($id, self::RESOURCE_NOT_FOUND, $message);
}
public function getId(): string|int
{
return $this->id;
}
/**
* @return array{
* jsonrpc: string,
* id: string|int,
* error: array{
* code: int,
* message: string,
* },
* data?: mixed,
* }
*/
public function jsonSerialize(): array
{
$error = [
'code' => $this->code,
'message' => $this->message,
];
if (null !== $this->data) {
$error['data'] = $this->data;
}
return [
'jsonrpc' => MessageInterface::JSONRPC_VERSION,
'id' => $this->id,
'error' => $error,
];
}
}