Now, we need to check this exception message. For that purpose, open the WeatherForecastController.cs file and add the below action method to throw an exception –
If we want to capture the details of the exception objects – i.e. like the stack trace, message, etc then we use the below code as the exception middleware –
Also, we can write our custom middleware to handle any type of exceptions. In this section, we demonstrate how to create a typical custom middleware class. Custom middleware also provides much more flexibility to handle exceptions. We can add a stack trace, an exception type name, error code, or anything else which we want to include as a part of the error message. The below code snippet shows the typical custom middleware class:
- using Microsoft.AspNetCore.Http;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Threading.Tasks;
-
- namespace API.DemoSample.Exceptions
- {
- public class ExceptionHandlerMiddleware
- {
- private readonly RequestDelegate _next;
-
- public ExceptionHandlerMiddleware(RequestDelegate next)
- {
- _next = next;
- }
-
- public async Task Invoke(HttpContext context)
- {
- try
- {
- await _next.Invoke(context);
- }
- catch (Exception ex)
- {
-
- }
- }
- }
- }
In the above class, a request delegate is passed to any middleware. The middleware either processes this or passes it to the next middleware in the chain. If the request is unsuccessful, then an exception will be thrown, and then the HandleExceptionMessageAsync method will be executed within the catch block. So, let's update the Invoke method code as shown below:
- public async Task Invoke(HttpContext context)
- {
- try
- {
- await _next.Invoke(context);
- }
- catch (Exception ex)
- {
- await HandleExceptionMessageAsync(context, ex).ConfigureAwait(false);
- }
- }
Now, we need to implement the HandleExceptionMessageAsync method, as shown below:
- private static Task HandleExceptionMessageAsync(HttpContext context, Exception exception)
- {
- context.Response.ContentType = "application/json";
- int statusCode = (int)HttpStatusCode.InternalServerError;
- var result = JsonConvert.SerializeObject(new
- {
- StatusCode = statusCode,
- ErrorMessage = exception.Message
- });
- context.Response.ContentType = "application/json";
- context.Response.StatusCode = statusCode;
- return context.Response.WriteAsync(result);
- }
Now, in the next step, we need to create a static class named ExceptionHandlerMiddlewareExtensions and add the below code within that class,
- using Microsoft.AspNetCore.Builder;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
-
- namespace API.DemoSample.Exceptions
- {
- public static class ExceptionHandlerMiddlewareExtensions
- {
- public static void UseExceptionHandlerMiddleware(this IApplicationBuilder app)
- {
- app.UseMiddleware<ExceptionHandlerMiddleware>();
- }
- }
- }
Now, in the last step, we need to turn on our custom middleware within the Configure method of the startup class, as shown below:
- app.UseExceptionHandlerMiddleware();
Conclusion
Exception handling is a mainly cross-cutting concept for any type of application. In this article, we discuss the implementation process of the global exception handling concept. We can take the benefits of global exception handling in any ASP.NET Core application to ensure that every exception will be caught and return the proper details related to that exception. With the global exception handling, we just need to write the exception handling related code for our entire application just in one place. Any suggestions or feedback or query related to this article are most welcome.