This is a quick post to show how to implement a global exception handler in ASP.NET Core 3.1.

These are the main pieces involved in the error handling process with a brief description of each:

  • Global Error Handler Middleware - custom middleware that catches and handles all exceptions, and determines which HTTP response code to return based on the exception type.
  • Custom App Exception - a custom exception class used to differentiate between handled exceptions that return a 400 response and unhandled exceptions that return a 500 response.
  • Startup.cs - the ASP.NET Core startup class that adds the global error handler middleware to the application request pipeline.
  • Example Error Service - an example service that shows how to throw a custom exception that will be handled by the global error handler.

Global Error Handler Middleware

The global error handler middleware is used catch all exceptions thrown by the api in a single place, removing the need for duplicated error handling code throughout the application. It’s configured as middleware in the Configure method of the Startup.cs class.

Errors of type AppException are treated as custom (app specific) errors that return a 400 Bad Request response, the ASP.NET built-in KeyNotFoundException class is used to return 404 Not Found responses, all other exceptions are unhandled and return a 500 Internal Server Error response.

See the example service for examples of how to throw a custom app exception or not found exception that will be handled by the global error handler.

using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Net;
using System.Text.Json;
using System.Threading.Tasks;
using WebApi.Helpers;

namespace WebApi.Middleware
{
    public class ErrorHandlerMiddleware
    {
        private readonly RequestDelegate _next;

        public ErrorHandlerMiddleware(RequestDelegate next)
        {
            _next = next;
        }

        public async Task Invoke(HttpContext context)
        {
            try
            {
                await _next(context);
            }
            catch (Exception error)
            {
                var response = context.Response;
                response.ContentType = "application/json";

                switch(error)
                {
                    case AppException e:
                        // custom application error
                        response.StatusCode = (int)HttpStatusCode.BadRequest;
                        break;
                    case KeyNotFoundException e:
                        // not found error
                        response.StatusCode = (int)HttpStatusCode.NotFound;
                        break;
                    default:
                        // unhandled error
                        response.StatusCode = (int)HttpStatusCode.InternalServerError;
                        break;
                }

                var result = JsonSerializer.Serialize(new { message = error?.Message });
                await response.WriteAsync(result);
            }
        }
    }
}

#aspdotnet #web-development #programming #developer

How to Implement Global Exception Handler in ASP.NET Core 3.1
5.25 GEEK