ExceptionCatcherMiddleware 1.0.3
See the version list below for details.
dotnet add package ExceptionCatcherMiddleware --version 1.0.3
NuGet\Install-Package ExceptionCatcherMiddleware -Version 1.0.3
<PackageReference Include="ExceptionCatcherMiddleware" Version="1.0.3" />
paket add ExceptionCatcherMiddleware --version 1.0.3
#r "nuget: ExceptionCatcherMiddleware, 1.0.3"
// Install ExceptionCatcherMiddleware as a Cake Addin #addin nuget:?package=ExceptionCatcherMiddleware&version=1.0.3 // Install ExceptionCatcherMiddleware as a Cake Tool #tool nuget:?package=ExceptionCatcherMiddleware&version=1.0.3
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:
using ExceptionCatcherMiddleware.Mappers.CreatingCustomMappers;
public class ArgumentExceptionMapper : IExceptionMapper<ArgumentException>
{
public BadResponse Map(ArgumentException exception)
{
return new BadResponse()
{
StatusCode = 400,
ResponseDto = new
{
Title = "Argument Exception occurred during execution",
Detail = exception.Message
}
};
}
}
Exception mappers should map from TException
to BadResponse
. BadResponse
class is utilized by the middleware.
StatusCode
will be applied to response's status code.ResponseDto
will be used as the response body. Since the package is connected to ASP.NET content negotiation, the framework will handle the serialization of the DTO.
Adding mapper to middleware
builder.Services.AddExceptionCatcherMiddlewareServices(optionsBuilder =>
{
optionsBuilder.RegisterExceptionMapper<ArgumentException, ArgumentExceptionMapper>();
});
This method has the following signature RegisterExceptionMapper<TException, TMapper>
.
TException
is a type of exception that mapper will map.TMapper
is a type of mapper for that exception
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. 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 | Versions 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. |
-
net7.0
- Microsoft.AspNetCore.Http.Abstractions (>= 2.2.0)
- Microsoft.AspNetCore.Mvc.Abstractions (>= 2.2.0)
- Microsoft.AspNetCore.Mvc.Core (>= 2.2.5)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 7.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.