diff --git a/src/ApiCall.php b/src/ApiCall.php index 401be2d4..0a528318 100644 --- a/src/ApiCall.php +++ b/src/ApiCall.php @@ -286,8 +286,11 @@ private function makeRequest(string $method, string $endPoint, bool $asJson, arr } catch (HttpException $exception) { $statusCode = $exception->getResponse()->getStatusCode(); - // Skip 408 timeouts and continue to next iteration + // Skip 408 timeouts and continue to next iteration, but keep the + // exception so that exhausting the retries still reports the timeout if ($statusCode === 408) { + $lastException = $this->getException($statusCode) + ->setMessage($exception->getMessage()); continue; } diff --git a/tests/Feature/ApiCallRetryTest.php b/tests/Feature/ApiCallRetryTest.php index 5b570fbd..4fc400f1 100644 --- a/tests/Feature/ApiCallRetryTest.php +++ b/tests/Feature/ApiCallRetryTest.php @@ -569,4 +569,67 @@ public function testDrainedResponseBodyStillDecodesWithoutRetrying(): void $this->assertSame(['ok' => true], $result); $this->assertSame(1, $callCount); } + + public function testEvery408AttemptStillThrowsInsteadOfReturningNothing(): void + { + $callCount = 0; + + $httpClient = $this->createMock(ClientInterface::class); + $httpClient->method('sendRequest') + ->willReturnCallback(function() use (&$callCount) { + $callCount++; + + $response = $this->createMock(ResponseInterface::class); + $response->method('getStatusCode')->willReturn(408); + throw new HttpException('Request timeout', $this->createMock(RequestInterface::class), $response); + }); + + $config = new Configuration([ + 'api_key' => 'test-key', + 'nodes' => [ + ['host' => 'node1', 'port' => 8108, 'protocol' => 'http'], + ['host' => 'node2', 'port' => 8108, 'protocol' => 'http'] + ], + 'num_retries' => 2, + 'retry_interval_seconds' => 0, + 'client' => $httpClient + ]); + + $apiCall = new ApiCall($config); + + try { + $result = $apiCall->get('/test', []); + $this->fail('Expected a TypesenseClientError, got ' . var_export($result, true)); + } catch (TypesenseClientError $exception) { + $this->assertSame(408, $exception->getCode()); + $this->assertSame('Request timeout', $exception->getMessage()); + } + + $this->assertSame(3, $callCount); + } + + public function testSearchSurfacesATimeoutWhenEveryAttemptReturns408(): void + { + $httpClient = $this->createMock(ClientInterface::class); + $httpClient->method('sendRequest') + ->willReturnCallback(function() { + $response = $this->createMock(ResponseInterface::class); + $response->method('getStatusCode')->willReturn(408); + throw new HttpException('Request timeout', $this->createMock(RequestInterface::class), $response); + }); + + $client = new \Typesense\Client([ + 'api_key' => 'test-key', + 'nodes' => [ + ['host' => 'node1', 'port' => 8108, 'protocol' => 'http'] + ], + 'num_retries' => 1, + 'retry_interval_seconds' => 0, + 'client' => $httpClient + ]); + + $this->expectException(TypesenseClientError::class); + + $client->collections['books']->documents->search(['q' => '*', 'query_by' => 'title']); + } }