Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle client errors in the requestStream method using ResponseClientException #90

Closed
Closed
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
17 changes: 14 additions & 3 deletions src/Exceptions/TransporterException.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,26 @@
namespace OpenAI\Exceptions;

use Exception;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Message\RequestInterface;

final class TransporterException extends Exception
{
private RequestInterface $request;

/**
* Creates a new Exception instance.
*/
public function __construct(ClientExceptionInterface $exception)
public function __construct(RequestInterface $request, string $message = '', int $code = 0)
{
parent::__construct($message, $code);
$this->request = $request;
}

/**
* Get the request that caused the exception.
*/
public function getRequest(): RequestInterface
{
parent::__construct($exception->getMessage(), 0, $exception);
return $this->request;
}
}
11 changes: 8 additions & 3 deletions src/Transporters/HttpTransporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function requestObject(Payload $payload): array|string
try {
$response = $this->client->sendRequest($request);
} catch (ClientExceptionInterface $clientException) {
throw new TransporterException($clientException);
throw new TransporterException($request, $clientException->getMessage(), $clientException->getCode());
}

$contents = (string) $response->getBody();
Expand Down Expand Up @@ -79,7 +79,7 @@ public function requestContent(Payload $payload): string
try {
$response = $this->client->sendRequest($request);
} catch (ClientExceptionInterface $clientException) {
throw new TransporterException($clientException);
throw new TransporterException($request, $clientException->getMessage(), $clientException->getCode());
}

$contents = $response->getBody()->getContents();
Expand Down Expand Up @@ -108,7 +108,12 @@ public function requestStream(Payload $payload): ResponseInterface
try {
$response = ($this->streamHandler)($request);
} catch (ClientExceptionInterface $clientException) {
throw new TransporterException($clientException);
throw new TransporterException($request, $clientException->getMessage(), $clientException->getCode());
}

$statusCode = $response->getStatusCode();
if ($statusCode >= 400 && $statusCode < 500) {
throw new TransporterException($request, 'Client error: ' . $statusCode, $statusCode);
}

return $response;
Expand Down
54 changes: 52 additions & 2 deletions tests/Transporters/HttpTransporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
expect(fn () => $this->http->requestObject($payload))->toThrow(function (TransporterException $e) {
expect($e->getMessage())->toBe('Could not resolve host.')
->and($e->getCode())->toBe(0)
->and($e->getPrevious())->toBeInstanceOf(ConnectException::class);
->and($e->getRequest())->toBeInstanceOf(RequestInterface::class);
});
});

Expand Down Expand Up @@ -209,7 +209,7 @@
expect(fn () => $this->http->requestContent($payload))->toThrow(function (TransporterException $e) {
expect($e->getMessage())->toBe('Could not resolve host.')
->and($e->getCode())->toBe(0)
->and($e->getPrevious())->toBeInstanceOf(ConnectException::class);
->and($e->getRequest())->toBeInstanceOf(RequestInterface::class);
});
});

Expand Down Expand Up @@ -261,3 +261,53 @@

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

test('request stream client error 401', function () {
$payload = Payload::create('completions', []);

$response = new Response(401, [], json_encode([
'error' => [
'message' => 'Unauthorized',
'type' => 'client_error',
'param' => null,
'code' => 'unauthorized',
],
]));

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

expect(fn() => $this->http->requestStream($payload))
->toThrow(function (TransporterException $e) {
expect($e->getMessage())->toBe('Client error: 401')
->and($e->getCode())->toBe(401)
->and($e->getRequest())->toBeInstanceOf(RequestInterface::class);
});
});

test('request stream client error 404', function () {
$payload = Payload::create('completions', []);

$response = new Response(404, [], json_encode([
'error' => [
'message' => 'Not Found',
'type' => 'client_error',
'param' => null,
'code' => 'not_found',
],
]));

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

expect(fn() => $this->http->requestStream($payload))
->toThrow(function (TransporterException $e) {
expect($e->getMessage())->toBe('Client error: 404')
->and($e->getCode())->toBe(404)
->and($e->getRequest())->toBeInstanceOf(RequestInterface::class);
});
});