Skip to content

Commit

Permalink
fix: exception handling for server errors with different content type…
Browse files Browse the repository at this point in the history
… header - fixes #132
  • Loading branch information
gehrisandro committed Jun 7, 2023
1 parent fbaaf67 commit f5a97ab
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/Enums/Transporter/ContentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ enum ContentType: string
{
case JSON = 'application/json';
case MULTIPART = 'multipart/form-data';
case TEXT_PLAIN = 'text/plain';
}
5 changes: 3 additions & 2 deletions src/Transporters/HttpTransporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use GuzzleHttp\Exception\ClientException;
use JsonException;
use OpenAI\Contracts\TransporterContract;
use OpenAI\Enums\Transporter\ContentType;
use OpenAI\Exceptions\ErrorException;
use OpenAI\Exceptions\TransporterException;
use OpenAI\Exceptions\UnserializableResponse;
Expand Down Expand Up @@ -48,7 +49,7 @@ public function requestObject(Payload $payload): array|string

$contents = $response->getBody()->getContents();

if ($response->getHeader('Content-Type')[0] === 'text/plain; charset=utf-8') {
if (str_contains($response->getHeaderLine('Content-Type'), ContentType::TEXT_PLAIN->value)) {
return $contents;
}

Expand Down Expand Up @@ -113,7 +114,7 @@ private function throwIfJsonError(ResponseInterface $response, string|ResponseIn
return;
}

if ($response->getheader('Content-Type')[0] !== 'application/json; charset=utf-8') {
if (! str_contains($response->getHeaderLine('Content-Type'), ContentType::JSON->value)) {
return;
}

Expand Down
28 changes: 27 additions & 1 deletion tests/Transporters/HttpTransporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
]);
});

test('request object server errors', function () {
test('request object server user errors', function () {
$payload = Payload::list('models');

$response = new Response(401, ['Content-Type' => 'application/json; charset=utf-8'], json_encode([
Expand All @@ -109,6 +109,32 @@
});
});

test('request object server errors', function () {
$payload = Payload::create('completions', ['model' => 'gpt-4']);

$response = new Response(401, ['Content-Type' => 'application/json'], json_encode([
'error' => [
'message' => 'That model is currently overloaded with other requests. You can ...',
'type' => 'server_error',
'param' => null,
'code' => null,
],
]));

$this->client
->shouldReceive('sendRequest')
->once()
->andReturn($response);

expect(fn () => $this->http->requestObject($payload))
->toThrow(function (ErrorException $e) {
expect($e->getMessage())->toBe('That model is currently overloaded with other requests. You can ...')
->and($e->getErrorMessage())->toBe('That model is currently overloaded with other requests. You can ...')
->and($e->getErrorCode())->toBeNull()
->and($e->getErrorType())->toBe('server_error');
});
});

test('error code may be null', function () {
$payload = Payload::create('completions', ['model' => 'gpt-42']);

Expand Down

0 comments on commit f5a97ab

Please sign in to comment.