Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize Developer Exception Page in Asp.Net Core?

It's common in the Configure method of the Startup.cs file to have code that looks like this:

if(env.IsDevelopment()) {
     app.UseDeveloperExceptionPage(new DeveloperExceptionPageOptions() { SourceCodeLineCount = 10 });
} 

The app.UseDeveloperExceptionPage(); line causes detailed information to be displayed in the browser when an exception occurs which is quite helpful for debugging.

I'm sure you've seen the output, it looks something like this:

enter image description here

This is a great start, but sometimes as developers we add additional information to exceptions to provide details about the error. The System.Exception class has a Data collection that can be used for storing such information. So for example my custom AppException that inherits from Exception has a constructor that accepts a private message in addition to the standard exception message, like so:

 /// <summary>
 /// Application Exception. 
 /// </summary>
 /// <param name="message">Message that can be displayed to a visitor.</param>
 /// <param name="privateMessage">Message for developers to pinpoint the circumstances that caused exception.</param>
 /// <param name="innerException"></param>
    public AppException(string message, string privateMessage, Exception innerException = null) 
        : base(message, innerException) {
        
        //Placing it in the exception Data collection makes the info available in the debugger
        this.Data["PrivateMessage"] = privateMessage;
    }

So as you can imaging, it'd be nice if the developer exception page displayed the PrivateMessage from the Exception (if it's an AppException) along with all the other information. I've looked high and low to figure out how to customize or augment the information displayed on the developer exception page but haven't been able to find any good information on it.

How can I customize or augment the information displayed on the developer exception page?

like image 462
RonC Avatar asked Jan 26 '17 20:01

RonC


People also ask

What is the developer exception page in ASP.NET Core?

The Developer Exception Page displays detailed information about unhandled request exceptions. ASP.NET Core apps enable the developer exception page by default when running in the Development environment.

What is UseExceptionHandler?

UseExceptionHandler(IApplicationBuilder) Adds a middleware to the pipeline that will catch exceptions, log them, and re-execute the request in an alternate pipeline. The request will not be re-executed if the response has already started.


2 Answers

The DeveloperExceptionPageMiddleware has the design problem - it assumes that all exceptions are 500 and it starts sending the response right away. It means, it's impossible to correct the behavior of this middleware by applying another custom middleware on top of that.

So, there are two options: replicate its nice HTML error page generation in your code or create your own dev exception page (I'd advise to dump current exception and the context in JSON and return it to the client).

like image 168
beloblotskiy Avatar answered Oct 17 '22 19:10

beloblotskiy


The app.UseDeveloperExceptionPage() extension method is shorthand for adding the DeveloperExceptionPageMiddleware into the request/response pipeline. You can find the full source code for DeveloperExceptionPageMiddleware here, including the middleware itself and the views. It should make an excellent base for rolling your own custom middleware.

like image 45
Polynomial Avatar answered Oct 17 '22 19:10

Polynomial