Skip to content

[12.x] Fix TypeError in Http::retry() when callback type-hints Throwable#59197

Open
hamedelasma wants to merge 1 commit intolaravel:12.xfrom
hamedelasma:fix/http-retry-when-callback-typeerror
Open

[12.x] Fix TypeError in Http::retry() when callback type-hints Throwable#59197
hamedelasma wants to merge 1 commit intolaravel:12.xfrom
hamedelasma:fix/http-retry-when-callback-typeerror

Conversation

@hamedelasma
Copy link
Contributor

Summary

Fixes #59012

Http::retry() throws a TypeError when the when callback type-hints Exception or Throwable and the response is not a 4xx/5xx (e.g., a 3xx redirect). This happens because $response->toException() returns null for non-failed responses, and passing null to a callback expecting Exception causes the TypeError.

Fix: Extract $response->toException() into a variable first. If it's null, skip calling the when callback and default $shouldRetry to true. Applied in both send() and handlePromiseResponse().

Changes

File Change
PendingRequest.php Guard against null exception before invoking when callback in both sync and async paths
HttpClientTest.php Add 2 tests for Exception and Throwable type-hints with non-4xx/5xx responses

Test Plan

  • testRetryWhenCallbackWithExceptionTypeHintDoesNotThrowTypeErrorForNon4xxOr5xxResponses
  • testRetryWhenCallbackWithThrowableTypeHintDoesNotThrowTypeErrorForNon4xxOr5xxResponses
  • All existing retry tests pass

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant