From ed86d28fd295a1a8be3a1ee646337dbfe317fdad Mon Sep 17 00:00:00 2001 From: Matt Kingshott <51963402+mattkingshott@users.noreply.github.com> Date: Thu, 16 Dec 2021 19:39:40 +0000 Subject: [PATCH 1/3] Update PendingRequest.php --- src/Illuminate/Http/Client/PendingRequest.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Http/Client/PendingRequest.php b/src/Illuminate/Http/Client/PendingRequest.php index fbbc288bb121..068a748c447d 100644 --- a/src/Illuminate/Http/Client/PendingRequest.php +++ b/src/Illuminate/Http/Client/PendingRequest.php @@ -98,6 +98,13 @@ class PendingRequest */ protected $retryDelay = 100; + /** + * Whether to throw an exception when all retries fail. + * + * @var bool + */ + protected $retryThrow = true; + /** * The callback that will determine if the request should be retried. * @@ -451,12 +458,14 @@ public function timeout(int $seconds) * @param int $times * @param int $sleep * @param callable|null $when + * @param bool $throw * @return $this */ - public function retry(int $times, int $sleep = 0, ?callable $when = null) + public function retry(int $times, int $sleep = 0, ?callable $when = null, bool $throw = true) { $this->tries = $times; $this->retryDelay = $sleep; + $this->retryThrow = $throw; $this->retryWhenCallback = $when; return $this; @@ -679,7 +688,7 @@ public function send(string $method, string $url, array $options = []) return tap(new Response($this->sendRequest($method, $url, $options)), function ($response) { $this->populateResponse($response); - if ($this->tries > 1 && ! $response->successful()) { + if ($this->tries > 1 && $this->retryThrow && ! $response->successful()) { $response->throw(); } From f0f2bfd04d7f58d3c1e7319ee49f7035142fd020 Mon Sep 17 00:00:00 2001 From: Matt Kingshott <51963402+mattkingshott@users.noreply.github.com> Date: Thu, 16 Dec 2021 19:40:01 +0000 Subject: [PATCH 2/3] Update Factory.php --- src/Illuminate/Http/Client/Factory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Http/Client/Factory.php b/src/Illuminate/Http/Client/Factory.php index 1b915d7603ca..cabf3c7b60a9 100644 --- a/src/Illuminate/Http/Client/Factory.php +++ b/src/Illuminate/Http/Client/Factory.php @@ -25,7 +25,7 @@ * @method \Illuminate\Http\Client\PendingRequest contentType(string $contentType) * @method \Illuminate\Http\Client\PendingRequest dd() * @method \Illuminate\Http\Client\PendingRequest dump() - * @method \Illuminate\Http\Client\PendingRequest retry(int $times, int $sleep = 0, ?callable $when = null) + * @method \Illuminate\Http\Client\PendingRequest retry(int $times, int $sleep = 0, ?callable $when = null, bool $throw = true) * @method \Illuminate\Http\Client\PendingRequest sink(string|resource $to) * @method \Illuminate\Http\Client\PendingRequest stub(callable $callback) * @method \Illuminate\Http\Client\PendingRequest timeout(int $seconds) From 2a6c78e27037da734ef1ccd71bd7be231382668e Mon Sep 17 00:00:00 2001 From: Matt Kingshott <51963402+mattkingshott@users.noreply.github.com> Date: Thu, 16 Dec 2021 19:41:42 +0000 Subject: [PATCH 3/3] Update HttpClientTest.php --- tests/Http/HttpClientTest.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/Http/HttpClientTest.php b/tests/Http/HttpClientTest.php index 8bb9edb7dc7f..762fb13df27f 100644 --- a/tests/Http/HttpClientTest.php +++ b/tests/Http/HttpClientTest.php @@ -1050,4 +1050,30 @@ public function testRequestIsMacroable() $this->factory->get('https://example.com'); } + + public function testRequestExceptionIsThrownWhenRetriesExhausted() + { + $this->expectException(RequestException::class); + + $this->factory->fake([ + '*' => $this->factory->response(['error'], 403), + ]); + + $this->factory + ->retry(2, 1000, null, true) + ->get('http://foo.com/get'); + } + + public function testRequestExceptionIsNotThrownWhenDisabledAndRetriesExhausted() + { + $this->factory->fake([ + '*' => $this->factory->response(['error'], 403), + ]); + + $response = $this->factory + ->retry(2, 1000, null, false) + ->get('http://foo.com/get'); + + $this->assertTrue($response->failed()); + } }