| 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\Mcp\Core\Schema\Enum\ProtocolErrorCode; |
| 18: | use Nexus\Mcp\Core\Schema\Error; |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | final readonly class InvalidRequestError extends Error |
| 32: | { |
| 33: | public const string DEFAULT_MESSAGE = 'Invalid request'; |
| 34: | |
| 35: | public function __construct(string $message, mixed $data = null) |
| 36: | { |
| 37: | parent::__construct(code: ProtocolErrorCode::InvalidRequest, message: $message, data: $data); |
| 38: | } |
| 39: | |
| 40: | #[\Override] |
| 41: | public static function fromArray(array $data): static |
| 42: | { |
| 43: | $message = $data['message'] ?? self::DEFAULT_MESSAGE; |
| 44: | Assert::that($message)->isString('error "message" must be a string, {type} given.'); |
| 45: | |
| 46: | return new self(message: $message, data: $data['data'] ?? null); |
| 47: | } |
| 48: | |
| 49: | #[\Override] |
| 50: | public function toArray(): array |
| 51: | { |
| 52: | $result = [ |
| 53: | 'code' => ProtocolErrorCode::InvalidRequest->value, |
| 54: | 'message' => $this->message, |
| 55: | ]; |
| 56: | |
| 57: | $data = $this->data ?? []; |
| 58: | |
| 59: | if ([] !== $data) { |
| 60: | $result['data'] = $data; |
| 61: | } |
| 62: | |
| 63: | return $result; |
| 64: | } |
| 65: | } |
| 66: | |