Skip to content

lastunicorn/error-flow

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Error Flow (ASP.NET Core)

This library offers an easy way to handle exceptions in a ASP.NET Core application and transform them into a HTTP response.

Usage

1) Register error handlers

Register the error handlers into the dependency container:

builder.Services.AddErrorFlow(configuration =>
{
    Assembly presentationAssembly = ...;
    configuration.AddErrorHandlersFromAssembly(presentationAssembly);
});

2) Add middleware

Add the middleware that handles the errors.

app.UseErrorFlow();

3) Create error handlers

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
        };
    }
}

About

A library for easily handling the exceptions in an ASP.NET Core application.

Topics

Resources

License

Stars

Watchers

Forks

Languages