Skip to content
Open
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
7 changes: 6 additions & 1 deletion src/Transporters/HttpTransporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,16 @@ private function throwIfJsonError(ResponseInterface $response, string|ResponseIn
}

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

if (isset($data['error'])) {
throw new ErrorException($data['error'], $response);
} elseif (isset($data[0]['error'])) {
if (! isset($data[0]['error']['type']) && isset($data[0]['error']['status'])) {
$data[0]['error']['type'] = $data[0]['error']['status'];
}
Comment on lines +181 to +183
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is the only thing I'm iffy on right now. I'd rather the ErrorException class navigate the water of different error structures to normalize them. So the HTTPTransporter isn't required to formulate things perfectly for an exception class.

throw new ErrorException($data[0]['error'], $response);
}
} catch (JsonException $jsonException) {
throw new UnserializableResponse($jsonException, $response);
Expand Down
50 changes: 50 additions & 0 deletions tests/Transporters/HttpTransporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -624,3 +624,53 @@

$this->http->requestObject($payload);
});

test('error for gemini with invalid parameter', function (string $requestMethod) {
$payload = Payload::create('completions', [
'model' => 'gemini-2.5-flash',
'messages' => [
[
'role' => 'system',
'content' => 'You can use tools if needed.',
],
[
'role' => 'user',
'content' => 'Yo',
],
],
'ddd' => 'auto',
]);

$response = new Response(400, ['Content-Type' => 'application/json; charset=utf-8'], json_encode(
[[
'error' => [
'code' => 400,
'message' => 'Invalid JSON payload received. Unknown name "ddd": Cannot find field.',
'status' => 'INVALID_ARGUMENT',
'details' => [
[
'@type' => 'type.googleapis.com/google.rpc.BadRequest',
'fieldViolations' => [
[
'description' => 'Invalid JSON payload received. Unknown name "ddd": Cannot find field.',
],
],
],
],
],
]]
));

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

expect(fn () => $this->http->$requestMethod($payload))
->toThrow(function (ErrorException $e) {
expect($e->getMessage())->toBe('Invalid JSON payload received. Unknown name "ddd": Cannot find field.')
->and($e->getErrorMessage())->toBe('Invalid JSON payload received. Unknown name "ddd": Cannot find field.')
->and($e->getErrorCode())->toBe(400)
->and($e->getErrorType())->toBe('INVALID_ARGUMENT');
});
})->with('request methods');