Skip to content

Preserve BadHttpRequestException status codes - #68632

Merged
javiercn merged 2 commits into
mainfrom
halter73-pass-through-bad-request-status-codes
Aug 19, 2026
Merged

Preserve BadHttpRequestException status codes#68632
javiercn merged 2 commits into
mainfrom
halter73-pass-through-bad-request-status-codes

Conversation

@halter73

@halter73 halter73 commented Aug 19, 2026

Copy link
Copy Markdown
Member

ExceptionHandlerMiddleware currently converts BadHttpRequestException responses to 500, masking client errors such as invalid or incomplete request bodies. This change uses BadHttpRequestException.StatusCode by default, while retaining 500 as the default exception status code for all other exceptions.

A configured StatusCodeSelector still takes precedence, and custom ExceptionHandler delegates and IExceptionHandler services can continue overriding the selected status code. Status code 404 carried by BadHttpRequestException is treated as intentional rather than as a misconfigured exception handler response.

Fixes: #43831

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI lite review requested due to automatic review settings August 19, 2026 02:08
@halter73
halter73 requested a review from DamianEdwards August 19, 2026 02:09

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.StatusCode when StatusCodeSelector is null.
  • The “404 implies misconfigured exception handler” safeguard is adjusted to treat BadHttpRequestException 404 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

Comment thread src/Middleware/Diagnostics/src/ExceptionHandler/ExceptionHandlerMiddlewareImpl.cs Outdated
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Comment on lines +167 to +168
var is404FromBadHttpRequestException = edi.SourceException is
BadHttpRequestException { StatusCode: StatusCodes.Status404NotFound };

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this right before its first use?

@javiercn
javiercn merged commit e7c22bd into main Aug 19, 2026
25 checks passed
@javiercn
javiercn deleted the halter73-pass-through-bad-request-status-codes branch August 19, 2026 15:47
@javiercn

Copy link
Copy Markdown
Member

/backport to release/11.0-rc1

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/11.0-rc1 (link to workflow run)

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.

Pass through response status codes from BadHttpRequestException in ExceptionHandlerMiddleware

4 participants