DEV Community

Mganda
Mganda

Posted on

Exception Handling in a .NET 6 Core Web API

Introduction:
Exception handling is a critical aspect of building robust and reliable applications. In a .NET 6 Core Web API, handling exceptions in a centralized and consistent manner can greatly enhance the overall stability and user experience. In this article, we'll explore how to effectively handle exceptions in a .NET 6 Core Web API using a custom middleware class and demonstrate best practices.

Table of Contents:

  • Overview of Exception Handling in a Web API
  • Setting up the ErrorHandlingMiddleware Class
  • Handling Exceptions in the Application Layer
  • Injecting the ErrorHandlingMiddleware in Program.cs
  • Conclusion

Overview of Exception Handling in a Web API:
Exception handling plays a vital role in ensuring the stability and reliability of a web API. By handling exceptions in a centralized manner, we can provide consistent error responses and capture important details for debugging and monitoring purposes. In a .NET 6 Core Web API, we can leverage a custom middleware to catch and process exceptions.

Setting up the ErrorHandlingMiddleware Class:
To begin, let's create an ErrorHandlingMiddleware class that will intercept exceptions thrown within the pipeline. This middleware will enable centralized exception handling and allow us to customize the error response based on the exception type.

public class ErrorHandlingMiddleware
{
    private readonly RequestDelegate _next;

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

    public async Task InvokeAsync(HttpContext context)
    {
        try
        {
            await _next(context);
        }
        catch (Exception ex)
        {
            await HandleExceptionAsync(context, ex);
        }
    }

    private static Task HandleExceptionAsync(HttpContext context, Exception ex)
    {
        // Customize the error response based on the exception
        var statusCode = HttpStatusCode.InternalServerError;
        var message = "An unexpected error occurred.";

        // Set the status code and error message based on the exception type
        if (ex is BadRequestException)
        {
            statusCode = HttpStatusCode.BadRequest;
            message = "A bad request exception message";
        }

        // Log the exception if needed

        // Set the response status code and content
        context.Response.ContentType = "application/json";
        context.Response.StatusCode = (int)statusCode;

        return context.Response.WriteAsJsonAsync(new { error = message });
    }
}
Enter fullscreen mode Exit fullscreen mode

Handling Exceptions in the Application Layer:
Instead of handling exceptions in the controller, we can handle them in the application layer, specifically in the command handler classes, to keep the controller clean. By doing this, we centralize the exception handling logic and separate it from the controller's responsibilities. Let's consider an example of an ExampleCommandHandler:

// ExampleCommandHandler.cs - Application Layer

using MediatR;
using System;
using System.Threading;
using System.Threading.Tasks;

public class ExampleCommandHandler : IRequestHandler<ExampleCommand, Unit>
{
    public Task<Unit> Handle(ExampleCommand request, CancellationToken cancellationToken)
    {
        try
        {
            // Code that performs operations

            return Task.FromResult(Unit.Value);
        }
        catch (BadRequestException ex)
        {
            // Log or handle the exception

            throw; // Re-throw the exception to be caught by the ErrorHandlingMiddleware
        }
        catch (Exception ex)
        {
            // Log or handle other exceptions

            throw; // Re-throw the exception to be caught by the ErrorHandlingMiddleware
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example, we catch the BadRequestException specifically and re-throw the exception. By doing this, the exception will be caught by the ErrorHandlingMiddleware, allowing us to handle it centrally. Similarly, we catch other exceptions and re-throw them for centralized handling.

Injecting the ErrorHandlingMiddleware in Program.cs:
To enable the ErrorHandlingMiddleware, we need to inject it into the middleware pipeline in the Program.cs file of the presentation layer. Here's how it can be done:

app.UseMiddleware<ErrorHandlingMiddleware>();
Enter fullscreen mode Exit fullscreen mode

By injecting the ErrorHandlingMiddleware into the middleware pipeline, we ensure that exceptions thrown within the pipeline will be caught and processed by the middleware, providing a centralized mechanism for handling exceptions.

Conclusion:
Exception handling is an integral part of building reliable web APIs. In a .NET 6 Core Web API, handling exceptions in a centralized and consistent manner is crucial for stability and user experience. By using a custom middleware class, we can intercept and handle exceptions, and by handling exceptions in the application layer (command handler classes), we can keep the controller clean and separate the concerns. Implementing best practices in exception handling helps create robust and reliable web APIs.
Remember to customize the exception handling code to fit your specific application needs and consider logging and capturing exception details for debugging and monitoring purposes.

Top comments (0)