DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for .NET 6 - Web API Global Exceptions Handling 🌐
Mohamad Lawand
Mohamad Lawand

Posted on

.NET 6 - Web API Global Exceptions Handling 🌐

Intro

In this article we will be exploring Global Error Handling in .Net 6 Web Api. You can watch the full video on youtube

You can find the full source code on github

Agenda

  • Introduction
  • Implementation of Global Exception Handling

Prerequisites

  • Visual Studio Code / Visual Studio / Rider
  • .NET Core 6 SDK
  • Understanding of C# Programming
  • Understanding of .NET Core APIs

Introduction

When we are building our application, although we hope that our application will run without any errors until the end of time. This is not really the case exceptions happens in applications and we need to handle them.

Exception Handling is a foundation that we need to consider while we are designing and building our application to have a stable application and avoid application crashes.

There are many ways to implement exception handling while building our applications from a very granular approach to a more generic way.

In this article we will be exploring global exception handling through middleware to catch runtime errors efficiently as per our requirement

Code

The first thing we need to do is to create a new WebApi application

dotnet new webapi -n ErrorManagement
Enter fullscreen mode Exit fullscreen mode

Now that our application has been created we need to install some packages

dotnet add package Microsoft.EntityFrameworkCore 
dotnet add package Microsoft.EntityFrameworkCore.Design 
dotnet add package Npgsql.EntityFrameworkCore.PostgreSQL
dotnet add package Microsoft.EntityFrameworkCore.Tools
Enter fullscreen mode Exit fullscreen mode

To make sure everything is running as it should we need to build our application

dotnet build
Enter fullscreen mode Exit fullscreen mode

Now it’s time to add the models, for this sample application we will be creating an app to list all F1 drivers. For this we will create a Models folder in the root directory of our application which will contain our models

Inside the Models folder we will create a new class called Driver

namespace ErrorManagement.Models;
public class Driver
{
    public int Id { get; set; }
    public string Name { get; set; } = "";
    public int DriverNumber { get; set; }
    public string Team { get; set; } = "";
}
Enter fullscreen mode Exit fullscreen mode

After the model has been created the next step is to create our database db context in the root directory of our application we will create a new folder called Data and inside the Data folder will add the AppDbContext class

using ErrorManagement.Models;
using Microsoft.EntityFrameworkCore;

namespace ErrorManagement.Data;

public class AppDbContext: DbContext
{
    public AppDbContext(DbContextOptions<AppDbContext> options): base(options) {  }

    public DbSet<Driver> Drivers { get; set; }
}
Enter fullscreen mode Exit fullscreen mode

Now we need to add the connection string in the appsettings.json

"ConnectionStrings": {
    "SampleDbConnection": "User ID =mohamad;Password=12345678;Server=localhost;Port=5432;Database=sampledb; Integrated Security=true;Pooling=true;"
  }
Enter fullscreen mode Exit fullscreen mode

Next we need to update our program.cs

builder.Services.AddEntityFrameworkNpgsql().AddDbContext<ApiDbContext>(opt =>
        opt.UseNpgsql(builder.Configuration.GetConnectionString("SampleDbConnection")));
Enter fullscreen mode Exit fullscreen mode

Once we add these we can do our migration

dotnet ef migrations add "initial_migration"
dotnet ef database update
Enter fullscreen mode Exit fullscreen mode

Now lets create the DriverServices in the root directory of our application let us create a new folder called Services and inside that folder we will create a new interface called IDriverService

using ErrorManagement.Models;

namespace ErrorManagement.Services;

public interface IDriverService
{
    public Task<IEnumerable<Driver>> GetDrivers();
    public Task<Driver?> GetDriverById(int id);
    public Task<Driver> AddDriver(Driver Driver);
    public Task<Driver> UpdateDriver(Driver Driver);
    public Task<bool> DeleteDriver(int Id);
}
Enter fullscreen mode Exit fullscreen mode

Now inside the same folder we will create a new class called DriverService

using ErrorManagement.Data;
using ErrorManagement.Models;
using Microsoft.EntityFrameworkCore;

namespace ErrorManagement.Services;

public class DriverService : IDriverService
{
    private readonly AppDbContext _dbContext;

    public DriverService(AppDbContext dbContext)
    {
        _dbContext = dbContext;
    }

    public async Task<IEnumerable<Driver>> GetDrivers()
    {
        return  await _dbContext.Drivers.ToListAsync();
    }

    public async Task<Driver?> GetDriverById(int id)
    {
        return await _dbContext.Drivers.FirstOrDefaultAsync(x => x.Id == id);
    }

    public async Task<Driver> AddDriver(Driver Driver)
    {
        var result = _dbContext.Drivers.Add(Driver);
        await _dbContext.SaveChangesAsync();
        return result.Entity;
    }

    public async Task<Driver> UpdateDriver(Driver Driver)
    {
        var result = _dbContext.Drivers.Update(Driver);
        await _dbContext.SaveChangesAsync();
        return result.Entity;
    }

    public async Task<bool> DeleteDriver(int Id)
    {
        var filteredData = _dbContext.Drivers.FirstOrDefault(x => x.Id == Id);
        var result = _dbContext.Remove(filteredData);
        await _dbContext.SaveChangesAsync();
        return result != null ? true : false;
    }
}
Enter fullscreen mode Exit fullscreen mode

Let us now update our Program.cs so our DriverServices would be injected in our Dependency Inject container

builder.Services.AddScoped<IDriverService, DriverService>();
Enter fullscreen mode Exit fullscreen mode

Now lets create our DriverController, insider the controller folder we will create a new class called DriversController and will add the following

using ErrorManagement.Models;
using ErrorManagement.Services;
using Microsoft.AspNetCore.Mvc;

namespace ErrorManagement.Controllers;

[ApiController]
[Route("[controller]")]
public class DriversController : ControllerBase
{
    private readonly ILogger<WeatherForecastController> _logger;
    private readonly IDriverService _driverServices;

    public DriversController(
        ILogger<WeatherForecastController> logger,
        IDriverService driverServices)
    {
        _logger = logger;
        _driverServices = driverServices;
    }

    [HttpGet("driverlist")]
    public async Task<IEnumerable<Driver>> DriverList()
    {
        var driverList = await _driverServices.GetDrivers();
        return driverList;
    }

    [HttpGet("getdriverbyid")]
    public async Task<IActionResult> GetDriverById(int Id)
    {
        _logger.LogInformation($"Fetch Driver with ID: {Id} from the database");
        var driver = await _driverServices.GetDriverById(Id);
        if (driver == null)
        {
            //throw new Notfound($"Driver ID {Id} not found.");
            return NotFound();
        }
        _logger.LogInformation($"Returning driver with ID: {driver.Id}.");
        return Ok(driver) ;
    }

    [HttpPost("adddriver")]
    public async Task<IActionResult> AddDriver(Driver driver)
    {
        var result = await _driverServices.AddDriver(driver);
        return Ok(result);
    }

    [HttpPut("updatedriver")]
    public async Task<IActionResult> UpdateDriver(Driver driver)
    {
        var result = await _driverServices.UpdateDriver(driver);
        return Ok(result);
    }

    [HttpDelete("deletedriver")]
    public async Task<bool> DeleteDriver(int Id)
    {
        return await _driverServices.DeleteDriver(Id);
    }
}
Enter fullscreen mode Exit fullscreen mode

Now let us add a new folder called Exceptions which will be utilised to manage all of our exceptions

We will be adding the following exceptions

namespace ErrorManagement.Exceptions;

public class BadRequestException : Exception
{
    public BadRequestException(string message) : base(message)
    { }
}
Enter fullscreen mode Exit fullscreen mode
namespace ErrorManagement.Exceptions;

public class KeyNotFoundException : Exception
{
    public KeyNotFoundException(string message) : base(message)
    { }
}
Enter fullscreen mode Exit fullscreen mode
namespace ErrorManagement.Exceptions;

public class NotFoundException : Exception
{
    public NotFoundException(string message) : base(message)
    { }
}
Enter fullscreen mode Exit fullscreen mode
namespace ErrorManagement.Exceptions;

public class NotImplementedException : Exception
{
    public NotImplementedException(string message) : base(message)
    { }
}
Enter fullscreen mode Exit fullscreen mode
namespace ErrorManagement.Exceptions;

public class UnauthorizedAccessException : Exception
{
    public UnauthorizedAccessException(string message) : base(message)
    { }
}
Enter fullscreen mode Exit fullscreen mode

Now that our exceptions has been added, we need to add a folder to the root directory of our application called configurations where we can build our GlobalErrorHandlingMiddleware

using System.Net;
using System.Text.Json;
using ErrorManagement.Exceptions;

using KeyNotFoundException = ErrorManagement.Exceptions.KeyNotFoundException;
using NotImplementedException = ErrorManagement.Exceptions.NotImplementedException;
using UnauthorizedAccessException = ErrorManagement.Exceptions.UnauthorizedAccessException;

namespace ErrorManagement.Configurations;

public class GlobalErrorHandlingMiddleware
{
    private readonly RequestDelegate _next;

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

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

    private static Task HandleExceptionAsync(HttpContext context, Exception exception)
    {
        HttpStatusCode status;
        var stackTrace = string.Empty;
        string message;

        var exceptionType = exception.GetType();

        if (exceptionType == typeof(BadRequestException))
        {
            message = exception.Message;
            status = HttpStatusCode.BadRequest;
            stackTrace = exception.StackTrace;
        }
        else if (exceptionType == typeof(NotFoundException))
        {
            message = exception.Message;
            status = HttpStatusCode.NotFound;
            stackTrace = exception.StackTrace;
        }
        else if (exceptionType == typeof(NotImplementedException))
        {
            status = HttpStatusCode.NotImplemented;
            message = exception.Message;
            stackTrace = exception.StackTrace;
        }
        else if (exceptionType == typeof(UnauthorizedAccessException))
        {
            status = HttpStatusCode.Unauthorized;
            message = exception.Message;
            stackTrace = exception.StackTrace;
        }
        else if (exceptionType == typeof(KeyNotFoundException))
        {
            status = HttpStatusCode.Unauthorized;
            message = exception.Message;
            stackTrace = exception.StackTrace;
        }
        else
        {
            status = HttpStatusCode.InternalServerError;
            message = exception.Message;
            stackTrace = exception.StackTrace;
        }

        var exceptionResult = JsonSerializer.Serialize(new { error = message, stackTrace });
        context.Response.ContentType = "application/json";
        context.Response.StatusCode = (int)status;

        return context.Response.WriteAsync(exceptionResult);
    }
}
Enter fullscreen mode Exit fullscreen mode

The GlobalErrorHandlingMiddleware is used to provide more control over exceptions which the application will generate

If there is any errors within an incoming request the GlobalErrorHandlingMiddleware will handle the error

Now lets create ApplicationBuilderExtension so we can inject our middleware inside the Services folder

namespace ErrorManagement.Configurations;

public static class ApplicationBuilderExtensions
{
    public static IApplicationBuilder AddGlobalErrorHandler(this IApplicationBuilder applicationBuilder)
        => applicationBuilder.UseMiddleware<GlobalErrorHandlingMiddleware>();
}
Enter fullscreen mode Exit fullscreen mode

Now let us inject this in the Program.cs

app.AddGlobalErrorHandler();
Enter fullscreen mode Exit fullscreen mode

Please provide your feedback and ask any question.

Top comments (0)

Hacktoberfest is happening now!



It is a month-long celebration of open source. For a lot of devs, its their introduction to open source.


Check out the Hacktoberfest tag on DEV to keep up with the latest!