Skip to content

Commit 2e62342

Browse files
authored
Merge pull request #487 from GuidoBelluomo/feature/http-status-in-errors
HTTP status in ErrorException
2 parents 04c08f2 + 2a3df38 commit 2e62342

File tree

4 files changed

+16
-4
lines changed

4 files changed

+16
-4
lines changed

src/Exceptions/ErrorException.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ final class ErrorException extends Exception
1313
*
1414
* @param array{message: string|array<int, string>, type: ?string, code: string|int|null} $contents
1515
*/
16-
public function __construct(private readonly array $contents)
16+
public function __construct(private readonly array $contents, private readonly int $statusCode)
1717
{
1818
$message = ($contents['message'] ?: (string) $this->contents['code']) ?: 'Unknown error';
1919

@@ -24,6 +24,16 @@ public function __construct(private readonly array $contents)
2424
parent::__construct($message);
2525
}
2626

27+
/**
28+
* Returns the HTTP status code.
29+
*
30+
* **Note: For streamed requests it might be 200 even in case of an error.**
31+
*/
32+
public function getStatusCode(): int
33+
{
34+
return $this->statusCode;
35+
}
36+
2737
/**
2838
* Returns the error message.
2939
*/

src/Responses/StreamResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function getIterator(): Generator
5757
$response = json_decode($data, true, flags: JSON_THROW_ON_ERROR);
5858

5959
if (isset($response['error'])) {
60-
throw new ErrorException($response['error']);
60+
throw new ErrorException($response['error'], $this->response->getStatusCode());
6161
}
6262

6363
if ($event !== null) {

src/Transporters/HttpTransporter.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ private function throwIfJsonError(ResponseInterface $response, string|ResponseIn
119119
return;
120120
}
121121

122+
$statusCode = $response->getStatusCode();
123+
122124
if ($contents instanceof ResponseInterface) {
123125
$contents = $contents->getBody()->getContents();
124126
}
@@ -128,7 +130,7 @@ private function throwIfJsonError(ResponseInterface $response, string|ResponseIn
128130
$response = json_decode($contents, true, flags: JSON_THROW_ON_ERROR);
129131

130132
if (isset($response['error'])) {
131-
throw new ErrorException($response['error']);
133+
throw new ErrorException($response['error'], $statusCode);
132134
}
133135
} catch (JsonException $jsonException) {
134136
throw new UnserializableResponse($jsonException);

tests/Testing/ClientFake.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
'message' => 'The model `gpt-1` does not exist',
5555
'type' => 'invalid_request_error',
5656
'code' => null,
57-
]),
57+
], 404),
5858
]);
5959

6060
$fake->completions()->create([

0 commit comments

Comments
 (0)