| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 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: | |
| 23: | |
| 24: | |
| 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: | |
| 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: | |