|
5 | 5 | namespace OpenAI\Exceptions;
|
6 | 6 |
|
7 | 7 | use Exception;
|
| 8 | +use Psr\Http\Message\RequestInterface; |
| 9 | +use Psr\Http\Message\ResponseInterface; |
8 | 10 |
|
9 | 11 | final class UnexpectedStatusCodeException extends Exception
|
10 | 12 | {
|
| 13 | + private RequestInterface $request; |
| 14 | + |
| 15 | + private ResponseInterface $response; |
| 16 | + |
| 17 | + private array $contents; |
| 18 | + |
11 | 19 | /**
|
12 |
| - * Creates a new Exception instance. |
| 20 | + * Creates a new UnexpectedStatusCodeException instance. |
13 | 21 | *
|
14 |
| - * @param int $statusCode The HTTP status code. |
| 22 | + * @param int $statusCode The unexpected HTTP status code. |
| 23 | + * @param RequestInterface $request The request that was sent. |
| 24 | + * @param ResponseInterface $response The response that was received. |
15 | 25 | */
|
16 |
| - |
17 |
| - public function __construct(int $statusCode) |
| 26 | + public function __construct(int $statusCode, RequestInterface $request, ResponseInterface $response) |
18 | 27 | {
|
19 |
| - $message = "Unexpected status code: {$statusCode}"; |
| 28 | + $this->request = $request; |
| 29 | + $this->response = $response; |
| 30 | + |
| 31 | + $body = (string) $response->getBody(); |
| 32 | + $decoded = json_decode($body, true); |
| 33 | + |
| 34 | + $this->contents = is_array($decoded['error'] ?? null) |
| 35 | + ? $decoded['error'] |
| 36 | + : ['message' => $body, 'type' => null, 'code' => null]; |
| 37 | + |
| 38 | + $message = ($this->contents['message'] ?: "Unexpected status code: {$statusCode}"); |
| 39 | + |
| 40 | + if (is_array($message)) { |
| 41 | + $message = implode(PHP_EOL, $message); |
| 42 | + } |
20 | 43 |
|
21 | 44 | parent::__construct($message, $statusCode);
|
22 | 45 | }
|
| 46 | + |
| 47 | + /** |
| 48 | + * Returns the HTTP status code of the response. |
| 49 | + */ |
| 50 | + public function getStatusCode(): int |
| 51 | + { |
| 52 | + return $this->response->getStatusCode(); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Returns the error message. |
| 57 | + */ |
| 58 | + public function getErrorMessage(): string |
| 59 | + { |
| 60 | + return $this->getMessage(); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Returns the error code if available. |
| 65 | + */ |
| 66 | + public function getErrorCode(): string|int|null |
| 67 | + { |
| 68 | + return $this->contents['code'] ?? null; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Returns the error type if available. |
| 73 | + */ |
| 74 | + public function getErrorType(): ?string |
| 75 | + { |
| 76 | + return $this->contents['type'] ?? null; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Returns the request that was made. |
| 81 | + */ |
| 82 | + public function getRequest(): RequestInterface |
| 83 | + { |
| 84 | + return $this->request; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Returns the response that was received. |
| 89 | + */ |
| 90 | + public function getResponse(): ResponseInterface |
| 91 | + { |
| 92 | + return $this->response; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Returns a string representation of the request. |
| 97 | + */ |
| 98 | + public function getRequestDetails(): string |
| 99 | + { |
| 100 | + return sprintf( |
| 101 | + 'Request: %s %s', |
| 102 | + $this->request->getMethod(), |
| 103 | + $this->request->getUri() |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Returns a string representation of the response. |
| 109 | + */ |
| 110 | + public function getResponseDetails(): string |
| 111 | + { |
| 112 | + return sprintf( |
| 113 | + 'Response: %d %s', |
| 114 | + $this->response->getStatusCode(), |
| 115 | + $this->response->getReasonPhrase() |
| 116 | + ); |
| 117 | + } |
23 | 118 | }
|
0 commit comments