Preserve BadHttpRequestException status codes - #68632
Merged
Merged
Conversation
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This pull request updates ExceptionHandlerMiddleware so that when ExceptionHandlerOptions.StatusCodeSelector is not configured, BadHttpRequestException.StatusCode is preserved instead of being replaced by the default 500, preventing client-error status codes from being masked.
Changes:
- Default status code selection now preserves
BadHttpRequestException.StatusCodewhenStatusCodeSelectorisnull. - The “404 implies misconfigured exception handler” safeguard is adjusted to treat
BadHttpRequestException404 as intentional. - Unit tests and option documentation are updated to validate precedence/override behavior.
Show a summary per file
| File | Description |
|---|---|
| src/Middleware/Diagnostics/src/ExceptionHandler/ExceptionHandlerMiddlewareImpl.cs | Updates default status code selection and 404 misconfiguration handling behavior. |
| src/Middleware/Diagnostics/src/ExceptionHandler/ExceptionHandlerOptions.cs | Updates StatusCodeSelector remarks to document BadHttpRequestException.StatusCode preservation. |
| src/Middleware/Diagnostics/test/UnitTests/ExceptionHandlerMiddlewareTest.cs | Adds coverage for preserved status codes and override precedence (selector/delegate/service). |
Review details
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
- Files reviewed: 3/3 changed files
- Comments generated: 1
- Review effort level: Lite
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
javiercn
reviewed
Aug 19, 2026
Comment on lines
+167
to
+168
| var is404FromBadHttpRequestException = edi.SourceException is | ||
| BadHttpRequestException { StatusCode: StatusCodes.Status404NotFound }; |
Member
There was a problem hiding this comment.
Move this right before its first use?
javiercn
approved these changes
Aug 19, 2026
DamianEdwards
approved these changes
Aug 19, 2026
Member
|
/backport to release/11.0-rc1 |
Contributor
|
Started backporting to |
10 tasks
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.
ExceptionHandlerMiddlewarecurrently convertsBadHttpRequestExceptionresponses to 500, masking client errors such as invalid or incomplete request bodies. This change usesBadHttpRequestException.StatusCodeby default, while retaining 500 as the default exception status code for all other exceptions.A configured
StatusCodeSelectorstill takes precedence, and customExceptionHandlerdelegates andIExceptionHandlerservices can continue overriding the selected status code. Status code 404 carried byBadHttpRequestExceptionis treated as intentional rather than as a misconfigured exception handler response.Fixes: #43831