feat(router): allow error responses to carry headers - #2267
Merged
Conversation
osbre
force-pushed
the
feat/failed-request-headers
branch
from
August 29, 2026 15:03
0399f02 to
356bd6c
Compare
Benchmark ResultsComparison of Open to see the benchmark resultsNo benchmark changes above ±5%. Generated by phpbench against commit 356bd6c |
Member
|
Great, thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Returning response with headers works from a controller:
But when using
throw HttpRequestFailedto return an error response from a different place, there is no way of passing headers, which leads to a few statuses that can only be sent correctly from inside a controller:429needsRetry-After, otherwise the client has no idea when to come back401needsWWW-Authenticateas per RFC 9110; the response is malformed without it405needsAllowX-RateLimit-*)Solution
HttpRequestFailedgains aheadersparameter, and the handler applies it to whatever the renderer returned:Values may be a string or a list of strings, so multi-value headers work:
Declared headers replace whatever the renderer set rather than adding to it: they describe the failure, which outranks anything assembled for the body. The lookup is case-insensitive, so a header the renderer spelled
Retry-Afterand a throw site spelling itretry-afterresolve to one header rather than two.This mirrors Symfony's
HttpExceptionInterface::getHeaders(), which Laravel reuses and also exposes asabort($status, $message, $headers).