-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathError.php
More file actions
51 lines (43 loc) · 1.29 KB
/
Error.php
File metadata and controls
51 lines (43 loc) · 1.29 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
<?php
declare(strict_types=1);
namespace PhpMcp\Client\JsonRpc;
use PhpMcp\Client\Exception\ProtocolException;
class Error
{
/**
* @param mixed|null $data Optional additional data
*/
public function __construct(
public readonly int $code,
public readonly string $message,
public readonly mixed $data = null,
) {}
/**
* @throws ProtocolException
*/
public static function fromArray(array $data): self
{
if (! isset($data['code']) || ! is_int($data['code'])) {
throw new ProtocolException('Invalid or missing "code" field in error object.');
}
if (! isset($data['message']) || ! is_string($data['message'])) {
throw new ProtocolException('Invalid or missing "message" field in error object.');
}
return new self(
code: $data['code'],
message: $data['message'],
data: $data['data'] ?? null // Data is optional
);
}
public function toArray(): array // Primarily for internal logging/debugging
{
$payload = [
'code' => $this->code,
'message' => $this->message,
];
if ($this->data !== null) {
$payload['data'] = $this->data;
}
return $payload;
}
}