8000 [9.x] Http client: retry callback exception handling (follow-up to #4… · laravel/framework@9879b90 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9879b90

Browse files
authored
[9.x] Http client: retry callback exception handling (follow-up to #41762) (#41792)
* Add failing test * Catch exception of retryWhenCallback
1 parent 6bec179 commit 9879b90

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/Illuminate/Http/Client/PendingRequest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Illuminate\Http\Client;
44

5+
use Exception;
56
use GuzzleHttp\Client;
67
use GuzzleHttp\Cookie\CookieJar;
78
use GuzzleHttp\Exception\ConnectException;
@@ -717,7 +718,13 @@ public function send(string $method, string $url, array $options = [])
717718
$this->dispatchResponseReceivedEvent($response);
718719

719720
if (! $response->successful()) {
720-
$shouldRetry = $this->retryWhenCallback ? call_user_func($this->retryWhenCallback, $response->toException()) : true;
721+
try {
722+
$shouldRetry = $this->retryWhenCallback ? call_user_func($this->retryWhenCallback, $response->toException()) : true;
723+
} catch (Exception $exception) {
724+
$shouldRetry = false;
725+
726+
throw $exception;
727+
}
721728

722729
if ($attempt < $this->tries && $shouldRetry) {
723730
$response->throw();

tests/Http/HttpClientTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Illuminate\Tests\Http;
44

5+
use Exception;
56
use GuzzleHttp\Middleware;
67
use GuzzleHttp\Promise\PromiseInterface;
78
use GuzzleHttp\Psr7\Response as Psr7Response;
@@ -1289,6 +1290,31 @@ public function testRequestExceptionIsNotThrownWithoutRetriesIfRetryNotNecessary
12891290
$this->factory->assertSentCount(1);
12901291
}
12911292

1293+
public function testExceptionThrownInRetryCallbackWithoutRetrying()
1294+
{
1295+
$this->factory->fake([
1296+
'*' => $this->factory->response(['error'], 500),
1297+
]);
1298+
1299+
$exception = null;
1300+
1301+
try {
1302+
$this->factory
1303+
->retry(2, 1000, function ($exception) use (&$whenAttempts) {
1304+
throw new Exception('Foo bar');
1305+
}, false)
1306+
->get('http://foo.com/get');
1307+
} catch (Exception $e) {
1308+
$exception = $e;
1309+
}
1310+
1311+
$this->assertNotNull($exception);
1312+
$this->assertInstanceOf(Exception::class, $exception);
1313+
$this->assertEquals('Foo bar', $exception->getMessage());
1314+
1315+
$this->factory->assertSentCount(1);
1316+
}
1317+
12921318
public function testMiddlewareRunsWhenFaked()
12931319
{
12941320
$this->factory->fake(function (Request $request) {

0 commit comments

Comments
 (0)
0