-
Notifications
You must be signed in to change notification settings - Fork 10.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Throw original exception if exception handler is not found #25062
Conversation
src/Middleware/Diagnostics/src/ExceptionHandler/ExceptionHandlerMiddleware.cs
Outdated
Show resolved
Hide resolved
context.Response.OnStarting(_clearCacheHeadersDelegate, context.Response); | ||
|
||
await _options.ExceptionHandler(context); | ||
|
||
if (context.Response.StatusCode == StatusCodes.Status404NotFound) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we handle other 4xx statuses? Also, what about the case where the configured exception handler was executed correctly and it itself returned a 404? We would be overriding what the exception handler did. It seems like an odd behaviour for an exception handler but theoretically, we would be breaking them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we handle other 4xx statuses?
404s are the main concern, that's what most people do by accident. A few trigger auth challenges (301 or 401) if they forget to allow anonymous requests to their error page, but those should produce their own logs to make them somewhat discoverable.
Also, what about the case where the configured exception handler was executed correctly and it itself returned a 404? We would be overriding what the exception handler did. It seems like an odd behaviour for an exception handler but theoretically, we would be breaking them.
In theory that could break, but I'd expect it to be far less common than the current issue. In that scenario they could call CompleteAsync to make sure the response was flushed regardless of the exception being rethrown.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @JunTaoLuo,
Regarding your comment, it turns out that the assumptions with 404 doesn't hold.
If my app. has a global exception handler, and it is catching custom Exceptions and determines "Oh look, this problem was caused by missing data" and it decides to put context.Response.StatusCode = 404 (in the ExceptionHandlerMiddlware), then first problem is that the response doesn't get sent (the workaround that I used is exactly the one you recommended, to flush the response using CompleteAsync method), but the second problem is that the ExceptionHandlerMiddlware won't handle that exception and it will propagate the original exception as if no exception handler was found - resulting in other middlerwares receiving the original exception.
Currently the only thing I can think of to help myself is not to use 404 for NO DATA FOUND, and use some other status code until this problem gets fixed.
Is there a plan to change this assumption that was made regarding 404 error?
Tnx!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We added an option to allow 404 response codes: https://github.com/dotnet/aspnetcore/blob/main/src/Middleware/Diagnostics/src/ExceptionHandler/ExceptionHandlerOptions.cs#L33. We also updated the error message to make this more obvious.
Fixes #21510.
In the event where the exception handler could not be resolved (i.e. 404) I've taken the third approach as suggested in #21510 (comment) which is to re-throw the original error.
The problem would also be logged: