Skip to content

Commit dd9efc2

Browse files
authored
fix(meta): use raw response on ErrorException (#649)
1 parent 850dad2 commit dd9efc2

File tree

4 files changed

+11
-9
lines changed

4 files changed

+11
-9
lines changed

src/Exceptions/ErrorException.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,20 @@
55
namespace OpenAI\Exceptions;
66

77
use Exception;
8+
use Psr\Http\Message\ResponseInterface;
89

910
final class ErrorException extends Exception
1011
{
12+
private readonly int $statusCode;
13+
1114
/**
1215
* Creates a new Exception instance.
1316
*
1417
* @param array{message: string|array<int, string>, type: ?string, code: string|int|null} $contents
1518
*/
16-
public function __construct(private readonly array $contents, private readonly int $statusCode)
19+
public function __construct(private readonly array $contents, public readonly ResponseInterface $response)
1720
{
21+
$this->statusCode = $response->getStatusCode();
1822
$message = ($contents['message'] ?: (string) $this->contents['code']) ?: 'Unknown error';
1923

2024
if (is_array($message)) {

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'], $this->response->getStatusCode());
60+
throw new ErrorException($response['error'], $this->response);
6161
}
6262

6363
if (isset($response['type']) && $response['type'] === 'ping') {

src/Transporters/HttpTransporter.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,18 +143,16 @@ private function throwIfJsonError(ResponseInterface $response, string|ResponseIn
143143
return;
144144
}
145145

146-
$statusCode = $response->getStatusCode();
147-
148146
if ($contents instanceof ResponseInterface) {
149147
$contents = (string) $contents->getBody();
150148
}
151149

152150
try {
153-
/** @var array{error?: array{message: string|array<int, string>, type: string, code: string}} $response */
154-
$response = json_decode($contents, true, flags: JSON_THROW_ON_ERROR);
151+
/** @var array{error?: array{message: string|array<int, string>, type: string, code: string}} $data */
152+
$data = json_decode($contents, true, flags: JSON_THROW_ON_ERROR);
155153

156-
if (isset($response['error'])) {
157-
throw new ErrorException($response['error'], $statusCode);
154+
if (isset($data['error'])) {
155+
throw new ErrorException($data['error'], $response);
158156
}
159157
} catch (JsonException $jsonException) {
160158
throw new UnserializableResponse($jsonException, $response);

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-
], 404),
57+
], new \GuzzleHttp\Psr7\Response(404)),
5858
]);
5959

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

0 commit comments

Comments
 (0)