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\Assert\ExpectationFailedException;
18: use Nexus\Mcp\Core\Schema\Enum\ProtocolErrorCode;
19: use Nexus\Mcp\Core\Schema\Error;
20:
21: /**
22: * Error carrying a raw integer code that does not map to a known `ProtocolErrorCode` case.
23: *
24: * @see https://www.jsonrpc.org/specification#error_object
25: */
26: final readonly class UnknownProtocolError extends Error
27: {
28: public function __construct(int $code, string $message, mixed $data = null)
29: {
30: if (ProtocolErrorCode::tryFrom($code) !== null) {
31: throw new ExpectationFailedException(
32: 'code {value} maps to a known protocol error code.',
33: ['value' => var_export($code, true)],
34: );
35: }
36:
37: parent::__construct($code, $message, $data);
38: }
39:
40: /**
41: * @param array{code: int, message: string, data?: mixed} $data
42: */
43: #[\Override]
44: public static function fromArray(array $data): static
45: {
46: Assert::that($data)->hasOffset('code', 'missing the required "code" key.');
47: Assert::that($data['code'])->isInt('"code" must be an integer, {type} given.');
48:
49: Assert::that($data)->hasOffset('message', 'missing the required "message" key.');
50: Assert::that($data['message'])->isString('"message" must be a string, {type} given.');
51:
52: return new self($data['code'], $data['message'], $data['data'] ?? null);
53: }
54: }
55: