1: <?php
2:
3: declare(strict_types=1);
4:
5: /**
6: * This file is part of the Nexus MCP SDK package.
7: *
8: * (c) 2026 John Paul E. Balandan, CPA <paulbalandan@gmail.com>
9: *
10: * For the full copyright and license information, please view
11: * the LICENSE file that was distributed with this source code.
12: */
13:
14: namespace Nexus\Mcp\Core\Schema\Error;
15:
16: use Nexus\Assert\Assert;
17: use Nexus\Mcp\Core\Schema\Enum\ProtocolErrorCode;
18: use Nexus\Mcp\Core\Schema\Error;
19:
20: /**
21: * A JSON-RPC error indicating that invalid JSON was received by the server.
22: *
23: * This error is returned when the server cannot parse the JSON text of a message.
24: *
25: * @extends Error<array{code: -32700, message: non-empty-string, data?: mixed}>
26: *
27: * @see https://modelcontextprotocol.io/specification/2026-07-28/schema#parseerror
28: */
29: final readonly class ParseError extends Error
30: {
31: public const string DEFAULT_MESSAGE = 'Parse error';
32:
33: /**
34: * @param non-empty-string $message
35: */
36: public function __construct(string $message, mixed $data = null)
37: {
38: parent::__construct(code: ProtocolErrorCode::ParseError, message: $message, data: $data);
39: }
40:
41: #[\Override]
42: public static function fromArray(array $data): static
43: {
44: $message = $data['message'] ?? self::DEFAULT_MESSAGE;
45: Assert::that($message)->isNonEmptyString('error "message" must be a non-empty string, {type} given.');
46:
47: return new self(message: $message, data: $data['data'] ?? null);
48: }
49:
50: #[\Override]
51: public function toArray(): array
52: {
53: $result = [
54: 'code' => ProtocolErrorCode::ParseError->value,
55: 'message' => $this->message,
56: ];
57:
58: $data = $this->data ?? [];
59:
60: if ([] !== $data) {
61: $result['data'] = $data;
62: }
63:
64: return $result;
65: }
66: }
67: