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/2026-07-28/schema#error
25: */
26: abstract readonly class Error implements Arrayable
27: {
28: public int $code;
29:
30: /**
31: * @param non-empty-string $message
32: */
33: public function __construct(
34: int|ProtocolErrorCode $code,
35: public string $message,
36: public mixed $data = null,
37: ) {
38: Assert::that($message)->isNonEmptyString('error "message" must be a non-empty string.');
39:
40: $this->code = $code instanceof ProtocolErrorCode ? $code->value : $code;
41: }
42:
43: #[\Override]
44: public function jsonSerialize(): array
45: {
46: return $this->toArray();
47: }
48: }
49: