Skip to content

Commit

Permalink
handle timeout exception
Browse files Browse the repository at this point in the history
  • Loading branch information
mkalkbrenner committed Jan 3, 2024
1 parent 13b4ab9 commit 9eb457c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/Core/Client/Adapter/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions src/Plugin/NoResponseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}

Expand Down
4 changes: 3 additions & 1 deletion tests/Plugin/NoResponseRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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());
Expand Down

0 comments on commit 9eb457c

Please sign in to comment.