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 an internal error occurred on the receiver.
22: *
23: * This error is returned when the receiver encounters an unexpected condition that prevents it
24: * from fulfilling the request.
25: *
26: * @extends Error<array{code: -32603, message: non-empty-string, data?: mixed}>
27: *
28: * @see https://modelcontextprotocol.io/specification/draft/schema#internalerror
29: */
30: final readonly class InternalError extends Error
31: {
32: public const string DEFAULT_MESSAGE = 'Internal error';
33:
34: public function __construct(string $message, mixed $data = null)
35: {
36: parent::__construct(code: ProtocolErrorCode::InternalError, message: $message, data: $data);
37: }
38:
39: #[\Override]
40: public static function fromArray(array $data): static
41: {
42: $message = $data['message'] ?? self::DEFAULT_MESSAGE;
43: Assert::that($message)->isString('error "message" must be a string, {type} given.');
44:
45: return new self(message: $message, data: $data['data'] ?? null);
46: }
47:
48: #[\Override]
49: public function toArray(): array
50: {
51: $result = [
52: 'code' => ProtocolErrorCode::InternalError->value,
53: 'message' => $this->message,
54: ];
55:
56: $data = $this->data ?? [];
57:
58: if ([] !== $data) {
59: $result['data'] = $data;
60: }
61:
62: return $result;
63: }
64: }
65: