question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow ExceptionHandlerOptions.ExceptionHandler to be called even if the response has started

See original GitHub issue

Is your feature request related to a problem? Please describe.

I am trying to set up error handling for my application. When an error occurs, I want to POST it to an error tracking service we use. I was going to use ExceptionHandlerMiddleware for this with a custom ExceptionHandlerOptions.ExceptionHandler, but reading through the source of ExceptionHandlerMiddleware I noticed that the delegate won’t even be invoked if the response has started. This is a downside since we have some endpoints that export very large, dynamically generated files in a streaming fashion so something really can go wrong after the response starts and we want to know.

Describe the solution you’d like

I’d like it if there were a way to have the handler called anyway even if the response has started (e. g. specified through a flag on the options).

Describe alternatives you’ve considered

(1) I could not not use ExceptionHandlerMiddleware and write my own middleware instead. ExceptionHandlerMiddleware does a bunch of stuff with registering features and such; it’s not clear to me how important that is. (2) Since ExceptionHandlerMiddleware does a rethrow when the response has started, it seems like I could put a custom piece of middleware before the main exception handler to catch this case. I’d probably prefer to just go with (1), though.

Additional context

I’m curious if most users of the framework are even aware of this behavior. Had I not happened to look at the source, I never would have guessed that it did this.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
analogrelaycommented, May 7, 2019

The ExceptionHandlerMiddleware logic (not running if the request has started) seems by-design to me. Since it’s supposed to handle exceptions, it can’t really do that if the response has already started (otherwise it would insert whatever content it’s using to handle the exception in mid-stream). A middleware of your own (just a try...catch wrapped around next) could work here, or the DiagnosticListener option @davidfowl mentioned.

Moving to Discussions since we don’t have plans to add this functionality to ExceptionHandlerMiddleware. If some work comes out of this discussion we can always adjust or file new issues 😃.

0reactions
msftbot[bot]commented, Jan 11, 2021

Thank you for contacting us. Due to a lack of activity on this discussion issue we’re closing it in an effort to keep our backlog clean. If you believe there is a concern related to the ASP.NET Core framework, which hasn’t been addressed yet, please file a new issue.

This issue will be locked after 30 more days of inactivity. If you still wish to discuss this subject after then, please create a new issue!

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Handling error in .NET5 with a custom response class
My startup class has the Exception Handler pointing to my route : // This method gets called by the runtime. Use this method...
Read more >
ASP.NET Core global exception handling gotchas
Implementing an ASP.NET global exception handler is usually done for purposes such as: Translating exception types into HTTP status codes, ...
Read more >
Handle errors in ASP.NET Core
If an exception isn't handled by any exception handler, then control falls back to the default behavior and options from the middleware.
Read more >
Exception Handling (3), In ASP.NET Core MVC
The request isn't re-executed if the response has started. The template generated code re-executes the request using the /Home/Error path.
Read more >
Creating a custom ErrorHandlerMiddleware function
In this post I show how to customise the ExceptionHandlerMiddleware to create custom responses when an error occurs in your middleware ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found