77use Typesense \Lib \Configuration ;
88use Typesense \Exceptions \ServerError ;
99use Typesense \Exceptions \RequestMalformed ;
10+ use Typesense \Exceptions \ObjectNotFound ;
1011use Http \Client \Exception \HttpException ;
1112use Http \Client \Exception \TransferException ;
1213use 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