ExceptionCatcherMiddleware 2.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package ExceptionCatcherMiddleware --version 2.0.0                
NuGet\Install-Package ExceptionCatcherMiddleware -Version 2.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ExceptionCatcherMiddleware" Version="2.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ExceptionCatcherMiddleware --version 2.0.0                
#r "nuget: ExceptionCatcherMiddleware, 2.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ExceptionCatcherMiddleware as a Cake Addin
#addin nuget:?package=ExceptionCatcherMiddleware&version=2.0.0

// Install ExceptionCatcherMiddleware as a Cake Tool
#tool nuget:?package=ExceptionCatcherMiddleware&version=2.0.0                

ExceptionCatcherMiddleware

This is a small package that provides a simple way to catch exceptions in middleware and map them to the appropriate status code and DTO that will be sent to the client. It integrated with ASP.NET content negotiation, so you don't need to worry about serializing the DTO.

Instalation

ExceptionCatcherMiddleware is available on NuGet and can be installed via the below commands:

$ Install-Package ExceptionCatcherMiddleware

or via the .NET Core CLI:

$ dotnet add package ExceptionCatcherMiddleware

Getting started

To add your own mapper you need to create class that implements IExceptionMapper<T> where T represents the type of exception that the mapper will map. Example for ArgumentException:

public class ArgumentExceptionMapper : IExceptionMapper<ArgumentException>
{
    public BadResponse Map(ArgumentException exception)
    {
        return BadResponse.FromObject(
            statusCode: 400,
            responseDto: new
            {
                Title = "Argument Exception occurred during execution",
                Detail = exception.Message
            });
    }
}

BadResponse has different factory methods:

  • FromObject Use it when you want to apply ASP.NET's content negotiation.
  • FromRaw Use it when you want to serialize response yourself without ASP.NET content negotiation.
  • FromJson Its just a FromRaw but with some predefined values.

You can easily create your own factory methods using extension methods with FromObject or FromRaw.

Adding mapper to middleware

builder.Services.AddExceptionCatcherMiddlewareServices(optionsBuilder =>
{
    optionsBuilder.RegisterExceptionMapper<ArgumentExceptionMapper>();
});

Note: TMapper must be bound strictly to TException. For instance, if your mapper implements IExceptionMapper<ArgumentException>, you can register it only for ArgumentException and not for ArgumentOutOfRangeException

Adding Middelware to pipeline

app.UseExceptionCatcherMiddleware();

That's it! Now, any ArgumentException and its derived exceptions, such as ArgumentOutOfRangeException, ArgumentNullException, and so on, will be mapped by the registered mapper, I mean it works just like try catch block. If an exception occurs that doesn't inherit from ArgumentException, it will be mapped by the default mapper for Exception. You can override this by registering your own IExceptionMapper<Exception>.

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.2 206 5/28/2023
2.0.1 156 5/27/2023
2.0.0 152 5/26/2023
1.0.3 552 2/28/2023