Skip to content

Http::retry() when callback throws TypeError when type-hinting Throwable or Exception #59012

@laserhybiz

Description

@laserhybiz

Laravel Version

12.52.0

PHP Version

8.5.2

Database Driver & Version

MySQL 8.0.33

Description

When type-hinting Throwable or Exception for the $exception parameter in the when callback of Http::retry(), a TypeError is thrown if the response status is 3xx.

Example:

Http::retry(
    times: 3,
    when: fn (Exception $exception) => $exception instanceof ConnectionException
);

If the HTTP response is a 3xx status code, the following error occurs:

{closure}(): Argument #1 ($exception) must be of type Exception, TypeError given.

This happens because Http::retry() retries on all non-2xx responses. Internally, it calls $response->toException(), which only returns an exception instance for 4xx and 5xx responses. For other non-2xx responses (such as 3xx), toException() returns null.

As a result, the when callback may receive null, causing a TypeError when the parameter is strictly type-hinted as Exception or Throwable.

If this behavior is intentional, the documentation should clarify that the $exception argument passed to the when callback can be Throwable|null, so developers can type-hint accordingly.

Steps To Reproduce

Run Http::retry(3, 0, fn (Exception $exception) => true); on a URL that returns a 3xx response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions