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
18 changes: 9 additions & 9 deletions src/ApiCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -421,23 +421,23 @@ public function getException(int $httpCode): TypesenseClientError
{
switch ($httpCode) {
case 0:
return new HTTPStatus0Error();
return new HTTPStatus0Error('', $httpCode);
case 400:
return new RequestMalformed();
return new RequestMalformed('', $httpCode);
case 401:
return new RequestUnauthorized();
return new RequestUnauthorized('', $httpCode);
case 404:
return new ObjectNotFound();
return new ObjectNotFound('', $httpCode);
case 409:
return new ObjectAlreadyExists();
return new ObjectAlreadyExists('', $httpCode);
case 422:
return new ObjectUnprocessable();
return new ObjectUnprocessable('', $httpCode);
case 500:
return new ServerError();
return new ServerError('', $httpCode);
case 503:
return new ServiceUnavailable();
return new ServiceUnavailable('', $httpCode);
default:
return new TypesenseClientError();
return new TypesenseClientError('', $httpCode);
}
}

Expand Down
44 changes: 44 additions & 0 deletions tests/Feature/ApiCallRetryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Typesense\Lib\Configuration;
use Typesense\Exceptions\ServerError;
use Typesense\Exceptions\RequestMalformed;
use Typesense\Exceptions\ObjectNotFound;
use Http\Client\Exception\HttpException;
use Http\Client\Exception\TransferException;
use JsonException;
Expand All @@ -31,6 +32,49 @@ private function createJsonResponseMock(string $json, int $statusCode = 200): Re
return $response;
}

public function testExceptionsCarryTheHttpStatusCode(): void
{
$config = new Configuration([
'api_key' => 'test-key',
'nodes' => [
['host' => 'node1', 'port' => 8108, 'protocol' => 'http'],
],
'client' => $this->createMock(ClientInterface::class),
]);
$apiCall = new ApiCall($config);

foreach ([0, 400, 401, 403, 404, 408, 409, 422, 429, 500, 502, 503, 504] as $statusCode) {
$exception = $apiCall->getException($statusCode);

$this->assertSame($statusCode, $exception->getCode());
$this->assertSame('', $exception->getMessage());
}
}

public function testThrownExceptionsCarryTheStatusCodeAndTheServerMessage(): void
{
$httpClient = $this->createMock(ClientInterface::class);
$httpClient->method('sendRequest')
->willReturn($this->createJsonResponseMock('{"message": "Not found."}', 404));

$config = new Configuration([
'api_key' => 'test-key',
'nodes' => [
['host' => 'node1', 'port' => 8108, 'protocol' => 'http'],
],
'client' => $httpClient,
]);
$apiCall = new ApiCall($config);

try {
$apiCall->get('/collections/missing', []);
$this->fail('Expected an ObjectNotFound exception.');
} catch (ObjectNotFound $exception) {
$this->assertSame(404, $exception->getCode());
$this->assertSame('Not found.', $exception->getMessage());
}
}

public function testRetriesOnHttpExceptionWithNon408Status(): void
{
$callCount = 0;
Expand Down