From 9eb457cb5105240c3e2406a651c2a9e71f4c6d79 Mon Sep 17 00:00:00 2001 From: Markus Kalkbrenner Date: Wed, 3 Jan 2024 13:25:47 +0100 Subject: [PATCH] handle timeout exception --- src/Core/Client/Adapter/Curl.php | 4 +++- src/Plugin/NoResponseRequest.php | 12 ++++++++++++ tests/Plugin/NoResponseRequestTest.php | 4 +++- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/Core/Client/Adapter/Curl.php b/src/Core/Client/Adapter/Curl.php index 675a954ca..6650447df 100644 --- a/src/Core/Client/Adapter/Curl.php +++ b/src/Core/Client/Adapter/Curl.php @@ -54,7 +54,9 @@ public function execute(Request $request, Endpoint $endpoint): Response public function getResponse(\CurlHandle $handle, $httpResponse): Response { if (CURLE_OK !== curl_errno($handle)) { - throw new HttpException(sprintf('HTTP request failed, %s', curl_error($handle))); + $error = curl_error($handle); + curl_close($handle); + throw new HttpException(sprintf('HTTP request failed, %s', $error)); } $httpCode = curl_getinfo($handle, CURLINFO_RESPONSE_CODE); diff --git a/src/Plugin/NoResponseRequest.php b/src/Plugin/NoResponseRequest.php index e24778fb0..cafeceab8 100644 --- a/src/Plugin/NoResponseRequest.php +++ b/src/Plugin/NoResponseRequest.php @@ -13,9 +13,11 @@ use Solarium\Core\Client\Adapter\TimeoutAwareInterface; use Solarium\Core\Client\Adapter\TimeoutAwareTrait; use Solarium\Core\Client\Request; +use Solarium\Core\Client\Response; use Solarium\Core\Event\Events; use Solarium\Core\Event\PreExecuteRequest; use Solarium\Core\Plugin\AbstractPlugin; +use Solarium\Exception\HttpException; /** * NoResponseRequest plugin. @@ -59,6 +61,16 @@ public function preExecuteRequest($event): self $this->client->getAdapter()->setOption('return_transfer', false); } + try { + $this->client->getAdapter()->execute($request, $event->getEndpoint()); + } + catch (HttpException $e) { + // We expect to run into a timeout. + } + + $response = new Response('', ['HTTP 1.0 200 OK']); + $event->setResponse($response); + return $this; } diff --git a/tests/Plugin/NoResponseRequestTest.php b/tests/Plugin/NoResponseRequestTest.php index 72a565c40..c8a28786e 100644 --- a/tests/Plugin/NoResponseRequestTest.php +++ b/tests/Plugin/NoResponseRequestTest.php @@ -89,6 +89,7 @@ public function testExecuteRequest() $endpoint = $this->client->getEndpoint(); $event = new PreExecuteRequestEvent($requestOutput, $endpoint); $this->plugin->preExecuteRequest($event); + $response = $event->getResponse(); $this->assertSame(Request::METHOD_GET, $requestInput->getMethod()); $this->assertSame(Request::METHOD_POST, $requestOutput->getMethod()); @@ -97,8 +98,9 @@ public function testExecuteRequest() $this->assertSame('', $requestOutput->getQueryString()); $this->assertSame(TimeoutAwareInterface::MINIMUM_TIMEOUT, $this->client->getAdapter()->getTimeout()); $this->assertFalse($this->client->getAdapter()->getOption('return_transfer')); + $this->assertSame(200, $response->getStatusCode()); - $event = new PostExecuteRequest($requestOutput, $endpoint, new Response('response')); + $event = new PostExecuteRequest($requestOutput, $endpoint, $response); $this->plugin->preExecuteRequest($event); $this->assertSame(TimeoutAwareInterface::DEFAULT_TIMEOUT, $this->client->getAdapter()->getTimeout());