Skip to content

Commit 8261764

Browse files
authored
Merge pull request #125 from cyppe/fix/exception-http-status-code
Carry the HTTP status code in the exceptions ApiCall::getException() creates
2 parents f5be49d + c34a8ee commit 8261764

2 files changed

Lines changed: 53 additions & 9 deletions

File tree

‎src/ApiCall.php‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -421,23 +421,23 @@ public function getException(int $httpCode): TypesenseClientError
421421
{
422422
switch ($httpCode) {
423423
case 0:
424-
return new HTTPStatus0Error();
424+
return new HTTPStatus0Error('', $httpCode);
425425
case 400:
426-
return new RequestMalformed();
426+
return new RequestMalformed('', $httpCode);
427427
case 401:
428-
return new RequestUnauthorized();
428+
return new RequestUnauthorized('', $httpCode);
429429
case 404:
430-
return new ObjectNotFound();
430+
return new ObjectNotFound('', $httpCode);
431431
case 409:
432-
return new ObjectAlreadyExists();
432+
return new ObjectAlreadyExists('', $httpCode);
433433
case 422:
434-
return new ObjectUnprocessable();
434+
return new ObjectUnprocessable('', $httpCode);
435435
case 500:
436-
return new ServerError();
436+
return new ServerError('', $httpCode);
437437
case 503:
438-
return new ServiceUnavailable();
438+
return new ServiceUnavailable('', $httpCode);
439439
default:
440-
return new TypesenseClientError();
440+
return new TypesenseClientError('', $httpCode);
441441
}
442442
}
443443

‎tests/Feature/ApiCallRetryTest.php‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Typesense\Lib\Configuration;
88
use Typesense\Exceptions\ServerError;
99
use Typesense\Exceptions\RequestMalformed;
10+
use Typesense\Exceptions\ObjectNotFound;
1011
use Http\Client\Exception\HttpException;
1112
use Http\Client\Exception\TransferException;
1213
use JsonException;
@@ -31,6 +32,49 @@ private function createJsonResponseMock(string $json, int $statusCode = 200): Re
3132
return $response;
3233
}
3334

35+
public function testExceptionsCarryTheHttpStatusCode(): void
36+
{
37+
$config = new Configuration([
38+
'api_key' => 'test-key',
39+
'nodes' => [
40+
['host' => 'node1', 'port' => 8108, 'protocol' => 'http'],
41+
],
42+
'client' => $this->createMock(ClientInterface::class),
43+
]);
44+
$apiCall = new ApiCall($config);
45+
46+
foreach ([0, 400, 401, 403, 404, 408, 409, 422, 429, 500, 502, 503, 504] as $statusCode) {
47+
$exception = $apiCall->getException($statusCode);
48+
49+
$this->assertSame($statusCode, $exception->getCode());
50+
$this->assertSame('', $exception->getMessage());
51+
}
52+
}
53+
54+
public function testThrownExceptionsCarryTheStatusCodeAndTheServerMessage(): void
55+
{
56+
$httpClient = $this->createMock(ClientInterface::class);
57+
$httpClient->method('sendRequest')
58+
->willReturn($this->createJsonResponseMock('{"message": "Not found."}', 404));
59+
60+
$config = new Configuration([
61+
'api_key' => 'test-key',
62+
'nodes' => [
63+
['host' => 'node1', 'port' => 8108, 'protocol' => 'http'],
64+
],
65+
'client' => $httpClient,
66+
]);
67+
$apiCall = new ApiCall($config);
68+
69+
try {
70+
$apiCall->get('/collections/missing', []);
71+
$this->fail('Expected an ObjectNotFound exception.');
72+
} catch (ObjectNotFound $exception) {
73+
$this->assertSame(404, $exception->getCode());
74+
$this->assertSame('Not found.', $exception->getMessage());
75+
}
76+
}
77+
3478
public function testRetriesOnHttpExceptionWithNon408Status(): void
3579
{
3680
$callCount = 0;

0 commit comments

Comments
 (0)