Add an option to allow 404 responses from ExceptionHandlers #26567
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.
Addresses #26528
Description
We addressed the issue #21510 in RC1 but introduced a breaking change in the default behaviour of the middleware. Specifically, we decided that a 404 response from the exception handler should be considered an error in the server and we will rethrow the original exception instead of returning the 404 response. This is to address a common pitfall where the exception handler middleware was misconfigured with a non-existent path.
However, the change in default behaviour regressed use cases where the exception handler intentionally produces a 404 response as mentioned in #26528. This PR introduces a fix where we add an option on the middleware to allow such responses. When enabled, this option considers 404 responses produced by the exception handler to be valid and will not rethrow the original exception.
Customer Impact
This issue was reported by @danikun in #26528. The breaking change introduced in 5.0 RC1 regressed the customer's exception handling logic which maps some exceptions to 404s. There is no acceptable workaround. We considered alternatives such as invoking CompleteAsync in the exception handler to flush the 404 response but this won't work in all environments; for example, unit testing using TestServer would not work since it doesn't implement CompleteAsync as discussed in #25288 (comment).
Regression?
Yes this is a regression that was introduced in #25062 in 5.0 RC1
Risk
Low. The solution is limited to this middleware and an unit test is added to verify its correctness.