Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Http client: retry callback exception handling (follow-up to #41762) #41792

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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();
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 @@ -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) {
Expand Down