Global Error Handling In ASP.NET Core 5

Introduction

 
In this article we will learn how to process unhandled exceptions in Asp.Net Core 5.
 
Exception handling is one of the most important features of any application. Fortunately, ASP.NET Core includes a middleware that makes exception handling easy.
 
The benefit of implementing global exception handling is that you need to implement in at one place.
 

Implementation

 
Let us create a sample Asp.Net Core Web API project.
 
Step 1
 
Create an Asp.Net Core Web API project.
 
Global Error Handling In ASP.NET Core 5
 
I have selected .Net 5 as target framework. Make sure that you select the Enable Open API Support checkbox. This will add the swagger middleware for API testing.
 
Step 2
 
Remove the WeatherForecastController.cs and WeatherForecast.cs files.
 
Step 3
 
Add a new API controller named ValuesController with only one end point as shown in figure below.
 
Global Error Handling In ASP.NET Core 5
 
In the Get method, a NullRefernceException is thrown.
 
Step 4
 
Now run the application. You can see the swagger UI is displayed.
 
Global Error Handling In ASP.NET Core 5
 
Click on the Get button and execute it. You can see the output response.
 
Global Error Handling In ASP.NET Core 5
 
Step 5
 
As we have thrown a NullReferenceException, the response is System.NullReferenceException. Granular level exception details are displayed. So let us see how we are able to see so much detail.
 
Step 6
 
Open the Startup.cs file.
 
Step 7
 
In the Configure method you can see a code app.UseDeveloperExceptionPage().
 
Global Error Handling In ASP.NET Core 5
 
Step 8
 
The UseDeveloperExceptionPage extension method adds middleware into the request pipeline which displays developer friendly exception detail page. This helps developers in tracing errors that occur during development phase. It is advisable to add it only during the development phase as the middleware displays sensitive information.
 
Step 9
 
Now comment the UseDeveloperExceptionPage middleware and run the app and see the response in swagger UI.
 
Global Error Handling In ASP.NET Core 5
 
It just displays the server response as 500 (Internal Server Error).
 
Step 10
 
Now let’s see how to handle the exception outside the development phase.
 
Step 11
 
There is another built in middleware named UseExceptionHandler.
 
Global Error Handling In ASP.NET Core 5
 
There are many overloads for UseExceptionHandler middleware. You can see that a string argument /errors is passed. Behind the scenes, when an unhandled exception occurs, it will redirect to a controller action method decorated with the Route[“/errors”] attribute. Now let’s create a new API controller for handling errors.
 
Step 12
 
Create an API controller named GlobalErrorsController (you can give any name to the controller). The code for this controller is shown in figure below.
 
Global Error Handling In ASP.NET Core 5
 
Created an action method named HandleErrors. This method is decorated with a Route[“/errors”] attribute. The GlobalErrorsController is decorated with [ApiExplorerSettings (Ignore = true)]
 
This decorator will ignore this controller and its action methods in swagger UI as we don’t want to trigger this endpoint manually.
 
Step 13
 
Now run the application and see the response in swagger UI.
 
Global Error Handling In ASP.NET Core 5
 
We can see our error customized error message in the response body detail.
 
Step 14
 
We can even capture the actual exception by using the asp.net core built in interface named IExceptionHandlerFeature. Let’s modify the code as shown in figure below.
 
Global Error Handling In ASP.NET Core 5
 
In order to capture the actual exception details, invoke the get method of HttpContext.Features. Based on the type of error the response status code is customised. In our case if the error is of type NullReferenceException then return a bad request. For all other errors it will be service unavailable. Now run the application and see the respoonse.
 
Global Error Handling In ASP.NET Core 5
 

Summary

 
So, we have learned how to process unhandled exceptions using the UseExceptionHandler middleware. Another way to handle exceptions is to create our own custom error handling middleware and use it the pipeline.
 
The source code is attached for your reference.
 
Happy Coding!!!