From e2c263e213fb720c0c642f1e97e2d87b912e7377 Mon Sep 17 00:00:00 2001 From: Guido Belluomo Date: Fri, 11 Oct 2024 02:03:45 +0200 Subject: [PATCH 1/2] HTTP status in ErrorException.php --- src/Exceptions/ErrorException.php | 13 ++++++++++++- src/Responses/StreamResponse.php | 2 +- src/Transporters/HttpTransporter.php | 4 +++- tests/Testing/ClientFake.php | 2 +- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/Exceptions/ErrorException.php b/src/Exceptions/ErrorException.php index f0dba316..53b7e8f4 100644 --- a/src/Exceptions/ErrorException.php +++ b/src/Exceptions/ErrorException.php @@ -12,8 +12,9 @@ final class ErrorException extends Exception * Creates a new Exception instance. * * @param array{message: string|array, type: ?string, code: string|int|null} $contents + * @param int $statusCode */ - public function __construct(private readonly array $contents) + public function __construct(private readonly array $contents, private readonly int $statusCode) { $message = ($contents['message'] ?: (string) $this->contents['code']) ?: 'Unknown error'; @@ -24,6 +25,16 @@ public function __construct(private readonly array $contents) parent::__construct($message); } + /** + * Returns the HTTP status code. + * + * **Note: For streamed requests it might be 200 even in case of an error.** + */ + public function getStatusCode(): int + { + return $this->statusCode; + } + /** * Returns the error message. */ diff --git a/src/Responses/StreamResponse.php b/src/Responses/StreamResponse.php index c22f6647..b890756f 100644 --- a/src/Responses/StreamResponse.php +++ b/src/Responses/StreamResponse.php @@ -57,7 +57,7 @@ public function getIterator(): Generator $response = json_decode($data, true, flags: JSON_THROW_ON_ERROR); if (isset($response['error'])) { - throw new ErrorException($response['error']); + throw new ErrorException($response['error'], $this->response->getStatusCode()); } if ($event !== null) { diff --git a/src/Transporters/HttpTransporter.php b/src/Transporters/HttpTransporter.php index 37ed0d7c..2299b832 100644 --- a/src/Transporters/HttpTransporter.php +++ b/src/Transporters/HttpTransporter.php @@ -119,6 +119,8 @@ private function throwIfJsonError(ResponseInterface $response, string|ResponseIn return; } + $statusCode = $response->getStatusCode(); + if ($contents instanceof ResponseInterface) { $contents = $contents->getBody()->getContents(); } @@ -128,7 +130,7 @@ private function throwIfJsonError(ResponseInterface $response, string|ResponseIn $response = json_decode($contents, true, flags: JSON_THROW_ON_ERROR); if (isset($response['error'])) { - throw new ErrorException($response['error']); + throw new ErrorException($response['error'], $statusCode); } } catch (JsonException $jsonException) { throw new UnserializableResponse($jsonException); diff --git a/tests/Testing/ClientFake.php b/tests/Testing/ClientFake.php index 29a6a90e..3e4d5fa5 100644 --- a/tests/Testing/ClientFake.php +++ b/tests/Testing/ClientFake.php @@ -54,7 +54,7 @@ 'message' => 'The model `gpt-1` does not exist', 'type' => 'invalid_request_error', 'code' => null, - ]), + ], 404), ]); $fake->completions()->create([ From 2a3df38a38815e81df674453114028dcd13108bf Mon Sep 17 00:00:00 2001 From: Guido Belluomo Date: Fri, 11 Oct 2024 02:13:59 +0200 Subject: [PATCH 2/2] Removed superfluous doc tag --- src/Exceptions/ErrorException.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Exceptions/ErrorException.php b/src/Exceptions/ErrorException.php index 53b7e8f4..a72a8666 100644 --- a/src/Exceptions/ErrorException.php +++ b/src/Exceptions/ErrorException.php @@ -12,7 +12,6 @@ final class ErrorException extends Exception * Creates a new Exception instance. * * @param array{message: string|array, type: ?string, code: string|int|null} $contents - * @param int $statusCode */ public function __construct(private readonly array $contents, private readonly int $statusCode) {