diff --git a/src/Illuminate/Http/Client/PendingRequest.php b/src/Illuminate/Http/Client/PendingRequest.php index f987f0aed653..307e51476931 100644 --- a/src/Illuminate/Http/Client/PendingRequest.php +++ b/src/Illuminate/Http/Client/PendingRequest.php @@ -2,6 +2,7 @@ namespace Illuminate\Http\Client; +use Exception; use GuzzleHttp\Client; use GuzzleHttp\Cookie\CookieJar; use GuzzleHttp\Exception\ConnectException; @@ -715,7 +716,13 @@ public function send(string $method, string $url, array $options = []) $this->populateResponse($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(); diff --git a/tests/Http/HttpClientTest.php b/tests/Http/HttpClientTest.php index 5a528d4445de..7c4eb54f939a 100644 --- a/tests/Http/HttpClientTest.php +++ b/tests/Http/HttpClientTest.php @@ -2,6 +2,7 @@ namespace Illuminate\Tests\Http; +use Exception; use GuzzleHttp\Middleware; use GuzzleHttp\Promise\PromiseInterface; use GuzzleHttp\Psr7\Response as Psr7Response; @@ -1271,6 +1272,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) {