Skip to content
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
2 changes: 1 addition & 1 deletion src/Illuminate/Http/Client/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 11 additions & 2 deletions src/Illuminate/Http/Client/PendingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

Expand Down
26 changes: 26 additions & 0 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}