Skip to content

Commit

Permalink
[9.x] Http client: retry callback exception handling (follow-up to #4…
Browse files Browse the repository at this point in the history
…1762) (#41792)

* Add failing test

* Catch exception of retryWhenCallback
  • Loading branch information
gdebrauwer authored Apr 2, 2022
1 parent 6bec179 commit 9879b90
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Illuminate/Http/Client/PendingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Http\Client;

use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Exception\ConnectException;
Expand Down Expand Up @@ -717,7 +718,13 @@ public function send(string $method, string $url, array $options = [])
$this->dispatchResponseReceivedEvent($response);

if (! $response->successful()) {
$shouldRetry = $this->retryWhenCallback ? call_user_func($this->retryWhenCallback, $response->toException()) : true;
try {
$shouldRetry = $this->retryWhenCallback ? call_user_func($this->retryWhenCallback, $response->toException()) : true;
} catch (Exception $exception) {
$shouldRetry = false;

throw $exception;
}

if ($attempt < $this->tries && $shouldRetry) {
$response->throw();
Expand Down
26 changes: 26 additions & 0 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Tests\Http;

use Exception;
use GuzzleHttp\Middleware;
use GuzzleHttp\Promise\PromiseInterface;
use GuzzleHttp\Psr7\Response as Psr7Response;
Expand Down Expand Up @@ -1289,6 +1290,31 @@ public function testRequestExceptionIsNotThrownWithoutRetriesIfRetryNotNecessary
$this->factory->assertSentCount(1);
}

public function testExceptionThrownInRetryCallbackWithoutRetrying()
{
$this->factory->fake([
'*' => $this->factory->response(['error'], 500),
]);

$exception = null;

try {
$this->factory
->retry(2, 1000, function ($exception) use (&$whenAttempts) {
throw new Exception('Foo bar');
}, false)
->get('http://foo.com/get');
} catch (Exception $e) {
$exception = $e;
}

$this->assertNotNull($exception);
$this->assertInstanceOf(Exception::class, $exception);
$this->assertEquals('Foo bar', $exception->getMessage());

$this->factory->assertSentCount(1);
}

public function testMiddlewareRunsWhenFaked()
{
$this->factory->fake(function (Request $request) {
Expand Down

0 comments on commit 9879b90

Please sign in to comment.