Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/Exceptions/ErrorException.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ final class ErrorException extends Exception
/**
* Creates a new Exception instance.
*
* @param array{message: string|array<int, string>, type: ?string, code: string|int|null} $contents
* @param array{message?: string|array<int, string>, type?: ?string, code?: string|int|null}|string $contents
*/
public function __construct(private readonly array $contents, public readonly ResponseInterface $response)
public function __construct(private readonly string|array $contents, public readonly ResponseInterface $response)
{
$this->statusCode = $response->getStatusCode();
$message = ($contents['message'] ?: (string) $this->contents['code']) ?: 'Unknown error';

// Errors can be a string or an object with message, type, and code
$contents = is_string($contents) ? ['message' => $contents] : $contents;
$message = ($contents['message'] ?? null) ?: (string) ($contents['code'] ?? null) ?: 'Unknown error';

if (is_array($message)) {
$message = implode(PHP_EOL, $message);
Expand Down Expand Up @@ -51,14 +54,14 @@ public function getErrorMessage(): string
*/
public function getErrorType(): ?string
{
return $this->contents['type'];
return $this->contents['type'] ?? null;
}

/**
* Returns the error code.
*/
public function getErrorCode(): string|int|null
{
return $this->contents['code'];
return $this->contents['code'] ?? null;
}
}
2 changes: 1 addition & 1 deletion src/Transporters/HttpTransporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ private function throwIfJsonError(ResponseInterface $response, string|ResponseIn
}

try {
/** @var array{error?: array{message: string|array<int, string>, type: string, code: string}} $data */
/** @var array{error?: string|array{message: string|array<int, string>, type: string, code: string}} $data */
$data = json_decode($contents, true, flags: JSON_THROW_ON_ERROR);

if (isset($data['error'])) {
Expand Down
44 changes: 44 additions & 0 deletions tests/Transporters/HttpTransporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,50 @@
});
})->with('request methods');

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

$response = new Response(404, ['Content-Type' => 'application/json; charset=utf-8'], json_encode([
'error' => 'You have insufficient permissions for this operation. Missing scopes: api.model.read',
]));

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

expect(fn () => $this->http->$requestMethod($payload))
->toThrow(function (ErrorException $e) {
expect($e->getMessage())->toBe('You have insufficient permissions for this operation. Missing scopes: api.model.read')
->and($e->getErrorMessage())->toBe('You have insufficient permissions for this operation. Missing scopes: api.model.read')
->and($e->getErrorCode())->toBeNull()
->and($e->getErrorType())->toBeNull();
});
})->with('request methods');

test('error code may have only message', function (string $requestMethod) {
$payload = Payload::create('completions', ['model' => 'gpt-42']);

$response = new Response(404, ['Content-Type' => 'application/json; charset=utf-8'], json_encode([
'error' => [
'message' => 'The engine is currently overloaded, please try again later',
],
]));

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

expect(fn () => $this->http->$requestMethod($payload))
->toThrow(function (ErrorException $e) {
expect($e->getMessage())->toBe('The engine is currently overloaded, please try again later')
->and($e->getErrorMessage())->toBe('The engine is currently overloaded, please try again later')
->and($e->getErrorCode())->toBeNull()
->and($e->getErrorType())->toBeNull();
});
})->with('request methods');

test('error type may be null on 429', function (string $requestMethod) {
$payload = Payload::list('models');

Expand Down