Skip to content

Commit ec7045e

Browse files
[9.x] Allow HTTP client requests with retries to optionally throw RequestExceptions (#40079)
* Update PendingRequest.php * Update Factory.php * Update HttpClientTest.php
1 parent 8e46f9c commit ec7045e

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

src/Illuminate/Http/Client/Factory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
* @method \Illuminate\Http\Client\PendingRequest contentType(string $contentType)
2626
* @method \Illuminate\Http\Client\PendingRequest dd()
2727
* @method \Illuminate\Http\Client\PendingRequest dump()
28-
* @method \Illuminate\Http\Client\PendingRequest retry(int $times, int $sleep = 0, ?callable $when = null)
28+
* @method \Illuminate\Http\Client\PendingRequest retry(int $times, int $sleep = 0, ?callable $when = null, bool $throw = true)
2929
* @method \Illuminate\Http\Client\PendingRequest sink(string|resource $to)
3030
* @method \Illuminate\Http\Client\PendingRequest stub(callable $callback)
3131
* @method \Illuminate\Http\Client\PendingRequest timeout(int $seconds)

src/Illuminate/Http/Client/PendingRequest.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ class PendingRequest
9898
*/
9999
protected $retryDelay = 100;
100100

101+
/**
102+
* Whether to throw an exception when all retries fail.
103+
*
104+
* @var bool
105+
*/
106+
protected $retryThrow = true;
107+
101108
/**
102109
* The callback that will determine if the request should be retried.
103110
*
@@ -451,12 +458,14 @@ public function timeout(int $seconds)
451458
* @param int $times
452459
* @param int $sleep
453460
* @param callable|null $when
461+
* @param bool $throw
454462
* @return $this
455463
*/
456-
public function retry(int $times, int $sleep = 0, ?callable $when = null)
464+
public function retry(int $times, int $sleep = 0, ?callable $when = null, bool $throw = true)
457465
{
458466
$this->tries = $times;
459467
$this->retryDelay = $sleep;
468+
$this->retryThrow = $throw;
460469
$this->retryWhenCallback = $when;
461470

462471
return $this;
@@ -679,7 +688,7 @@ public function send(string $method, string $url, array $options = [])
679688
return tap(new Response($this->sendRequest($method, $url, $options)), function ($response) {
680689
$this->populateResponse($response);
681690

682-
if ($this->tries > 1 && ! $response->successful()) {
691+
if ($this->tries > 1 && $this->retryThrow && ! $response->successful()) {
683692
$response->throw();
684693
}
685694

tests/Http/HttpClientTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,4 +1050,30 @@ public function testRequestIsMacroable()
10501050

10511051
$this->factory->get('https://example.com');
10521052
}
1053+
1054+
public function testRequestExceptionIsThrownWhenRetriesExhausted()
1055+
{
1056+
$this->expectException(RequestException::class);
1057+
1058+
$this->factory->fake([
1059+
'*' => $this->factory->response(['error'], 403),
1060+
]);
1061+
1062+
$this->factory
1063+
->retry(2, 1000, null, true)
1064+
->get('http://foo.com/get');
1065+
}
1066+
1067+
public function testRequestExceptionIsNotThrownWhenDisabledAndRetriesExhausted()
1068+
{
1069+
$this->factory->fake([
1070+
'*' => $this->factory->response(['error'], 403),
1071+
]);
1072+
1073+
$response = $this->factory
1074+
->retry(2, 1000, null, false)
1075+
->get('http://foo.com/get');
1076+
1077+
$this->assertTrue($response->failed());
1078+
}
10531079
}

0 commit comments

Comments
 (0)