|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace ArrayAccess\RdapClient\Exceptions; |
| 6 | + |
| 7 | +use RuntimeException; |
| 8 | + |
| 9 | +/** |
| 10 | + * Thrown when the remote RDAP server returns an error response payload. |
| 11 | + * |
| 12 | + * The exception message is assembled from the `title` and `description` |
| 13 | + * properties supplied by the server. The exception code contains the |
| 14 | + * numeric `errorCode` value. The full decoded response is available via |
| 15 | + * {@see getResponse()}. |
| 16 | + */ |
| 17 | +class RdapResponseException extends RuntimeException |
| 18 | +{ |
| 19 | + /** |
| 20 | + * Full decoded payload returned by the server. |
| 21 | + * |
| 22 | + * Keeping the entire array allows callers to inspect additional |
| 23 | + * properties without having to parse/reconstruct them from the message |
| 24 | + * or code. |
| 25 | + * |
| 26 | + * @var array<string, mixed> |
| 27 | + */ |
| 28 | + private array $response; |
| 29 | + |
| 30 | + /** |
| 31 | + * Construct from a previously prepared message. |
| 32 | + * |
| 33 | + * Use the named factory if possible. |
| 34 | + * |
| 35 | + * @param string $message human readable description |
| 36 | + * @param int $code errorCode from the payload |
| 37 | + * @param array<string, mixed> $response original decoded response |
| 38 | + * @param \Throwable|null $previous previous exception |
| 39 | + */ |
| 40 | + public function __construct( |
| 41 | + string $message = "", |
| 42 | + int $code = 0, |
| 43 | + array $response = [], |
| 44 | + ?\Throwable $previous = null |
| 45 | + ) { |
| 46 | + parent::__construct($message, $code, $previous); |
| 47 | + $this->response = $response; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Create an exception instance from a decoded RDAP error response. |
| 52 | + * |
| 53 | + * @param array<string, mixed> $response |
| 54 | + * @return self |
| 55 | + */ |
| 56 | + public static function fromResponse(array $response): self |
| 57 | + { |
| 58 | + $code = isset($response['errorCode']) ? (int) $response['errorCode'] : 0; |
| 59 | + $title = is_string($response['title'] ?? null) ? $response['title'] : ''; |
| 60 | + $description = ''; |
| 61 | + if (isset($response['description'])) { |
| 62 | + if (is_string($response['description'])) { |
| 63 | + $description = $response['description']; |
| 64 | + } elseif (is_array($response['description'])) { |
| 65 | + $description = implode(' ', $response['description']); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + $parts = array_filter([$title, $description]); |
| 70 | + $message = $parts ? implode(' - ', $parts) : 'RDAP error response'; |
| 71 | + |
| 72 | + return new self($message, $code, $response); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Get the raw response array that triggered the exception. |
| 77 | + * |
| 78 | + * @return array<string, mixed> |
| 79 | + */ |
| 80 | + public function getResponse(): array |
| 81 | + { |
| 82 | + return $this->response; |
| 83 | + } |
| 84 | +} |
| 85 | + |
0 commit comments