Skip to content

HTTP client automatically throws exception after retrying #40055

@mattkingshott

Description

@mattkingshott
  • Laravel Version: 8.75.0
  • PHP Version: 8.1.0
  • Database Driver & Version: MySQL 8.0.27

Description:

When a retry is specified within the HTTP client, and the retry limit is reached, then the response is automatically thrown. Since there is no way to hook into the throw method, you'll always get a RequestException. Therefore, you cannot do things like $response->failed()

The problem lies within Illuminate\Http\Client\PendingRequest, on lines 682-684:

if ($this->tries > 1 && ! $response->successful()) {
    $response->throw();
}

Steps To Reproduce:

Simply, add a retry configuration to the HTTP client:

Http::fake([
    '*' => Http::response(['error'], 403),
]);

$response = Http::retry(3, 5000)->post('https://example.com/route'); 

// The above line throws a RequestException, so the below error handling will not be reached

if ($response->failed()) {
    // error handling
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions