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;
15:
16: use Nexus\Assert\Assert;
17: use Nexus\Mcp\Core\Schema\Enum\ProtocolErrorCode;
18:
19: /**
20: * @template-covariant T of array<string, mixed> = array<string, mixed>
21: *
22: * @implements Arrayable<T>
23: *
24: * @see https://modelcontextprotocol.io/specification/draft/schema#error
25: */
26: abstract readonly class Error implements Arrayable
27: {
28: public int $code;
29:
30: /**
31: * @var non-empty-string
32: */
33: public string $message;
34:
35: public function __construct(
36: int|ProtocolErrorCode $code,
37: string $message,
38: public mixed $data = null,
39: ) {
40: Assert::that($message)->isNonEmptyString('error "message" must be a non-empty string.');
41:
42: $this->code = $code instanceof ProtocolErrorCode ? $code->value : $code;
43: $this->message = $message;
44: }
45:
46: #[\Override]
47: public function jsonSerialize(): array
48: {
49: return $this->toArray();
50: }
51: }
52: