This library offers an easy way to handle exceptions in a ASP.NET Core application and transform them into a HTTP response.
Register the error handlers into the dependency container:
builder.Services.AddErrorFlow(configuration =>
{
Assembly presentationAssembly = ...;
configuration.AddErrorHandlersFromAssembly(presentationAssembly);
});Add the middleware that handles the errors.
app.UseErrorFlow();Create a handler class for each exception that needs to be handled.
Specify the HTTP Status Code to be returned and the body object to be serialized as JSON.
public class DummyErrorHandler : JsonErrorHandler<DummyException, ErrorResponseDto>
{
protected override HttpStatusCode HttpStatusCode => HttpStatusCode.NotFound;
protected override ErrorResponseDto BuildHttpResponseBody(DummyException ex)
{
return new ErrorResponseDto
{
ErrorMessage = ex.Message
};
}
}