ColinChang.ExceptionHandler 1.0.4

a custom exception handler with specific data models for asp.net core, including a middleware and an MVC exception filter.

Install-Package ColinChang.ExceptionHandler -Version 1.0.4
dotnet add package ColinChang.ExceptionHandler --version 1.0.4
<PackageReference Include="ColinChang.ExceptionHandler" Version="1.0.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ColinChang.ExceptionHandler --version 1.0.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

ExceptionHandler

A custom exception handler with a specific data model for asp.net core, including middleware and MVC exception filter.

What this is about?

Exceptions are usually divided into expected exceptions(OperationException) and unexpected exceptions. Expected exceptions are usually errors thrown manually by developers, such as invalid data validation, etc. Such exception messages are relatively safe and friendly and can be displayed directly to client users; Unexpected exceptions are unexpected program errors, such as logic bugs, database errors, etc. Such exception information usually contains sensitive information that requires the developer to intercept and process it and return a more user-friendly error message to the client users.

We providers an exception middleware, an exception filter, and an exception filter attribute that can help to handle exceptions in asp.net core web applications.

Abstractions

OperationException

A client-friendly exception type that can be used to show expected and safe information of the exception occurred.

IOperationResult<T>

The actual return type for web requests with exceptions. We suggested you use this as the return type of all success requests, so whatever exceptions occurred or not, client users can always get responses with the same data structure.

How to use it?

UseErrorHandler

Custom exception middleware could help to handle exceptions in the middleware pipeline globally.

public void ConfigureServices(IServiceCollection services)
{
    // inject IOperationResult for unexpected exception
    services.AddTransient<IOperationResult>(provider =>
        new OperationResult<object>(null, -1, OperationException.DefaultMessage));

    services.AddControllers();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    // use middleware
    app.UseErrorHandler();
    // app.UseErrorHandler(async (context, e) => await context.Response.WriteAsync("unexpected exception"));

    app.UseRouting();
    app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
}

OperationExceptionFilter

OperationExceptionFilter can be used for asp.net core MVC application to handle exceptions of the framework.

public void ConfigureServices(IServiceCollection services)
{
    // inject IOperationResult for unexpected exception
    services.AddTransient<IOperationResult>(provider =>
        new OperationResult<object>(null, -1, OperationException.DefaultMessage));
    // inject filter globally  
    services.AddControllers(options => options.Filters.Add<OperationExceptionFilter>());
}

OperationExceptionFilterAttribute

we could use OperationExceptionFilterAttribute in the same way as OperationExceptionFilter to handle MVC framework exceptions. Plus, it can also be used on Controllers and Actions as an Attribute which allows for more detailed control of exceptions.

global
public void ConfigureServices(IServiceCollection services)
{
    // inject IOperationResult for unexpected exception
    services.AddTransient<IOperationResult>(provider =>
        new OperationResult<object>(null, -1, OperationException.DefaultMessage));
    // inject filter globally  
    services.AddControllers(options => options.Filters.Add<OperationExceptionFilterAttribute>());
}
Controller/Action
public void ConfigureServices(IServiceCollection services)
{
    // inject IOperationResult for unexpected exception
    services.AddTransient<IOperationResult>(provider =>
        new OperationResult<object>(null, -1, OperationException.DefaultMessage));
    services.AddControllers();
}

[ApiController]
[Route("[controller]")]
public class TestController : ControllerBase
{
    [HttpGet("{id}")]
    [OperationExceptionFilter]
    public Task<IOperationResult> GetAsync(int id)
    {
        if (id < 0)
            throw new OperationException("custom exception");

        if (id > 0)
            return Task.FromResult<IOperationResult>(new OperationResult<string>("success"));

        throw new Exception("unexpected exception");
    }
}

Sample

Sample project shows how to use this middleware.

Docs

https://ccstudio.org/dotnet/middleware/exception.html

ExceptionHandler

A custom exception handler with a specific data model for asp.net core, including middleware and MVC exception filter.

What this is about?

Exceptions are usually divided into expected exceptions(OperationException) and unexpected exceptions. Expected exceptions are usually errors thrown manually by developers, such as invalid data validation, etc. Such exception messages are relatively safe and friendly and can be displayed directly to client users; Unexpected exceptions are unexpected program errors, such as logic bugs, database errors, etc. Such exception information usually contains sensitive information that requires the developer to intercept and process it and return a more user-friendly error message to the client users.

We providers an exception middleware, an exception filter, and an exception filter attribute that can help to handle exceptions in asp.net core web applications.

Abstractions

OperationException

A client-friendly exception type that can be used to show expected and safe information of the exception occurred.

IOperationResult<T>

The actual return type for web requests with exceptions. We suggested you use this as the return type of all success requests, so whatever exceptions occurred or not, client users can always get responses with the same data structure.

How to use it?

UseErrorHandler

Custom exception middleware could help to handle exceptions in the middleware pipeline globally.

public void ConfigureServices(IServiceCollection services)
{
    // inject IOperationResult for unexpected exception
    services.AddTransient<IOperationResult>(provider =>
        new OperationResult<object>(null, -1, OperationException.DefaultMessage));

    services.AddControllers();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    // use middleware
    app.UseErrorHandler();
    // app.UseErrorHandler(async (context, e) => await context.Response.WriteAsync("unexpected exception"));

    app.UseRouting();
    app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
}

OperationExceptionFilter

OperationExceptionFilter can be used for asp.net core MVC application to handle exceptions of the framework.

public void ConfigureServices(IServiceCollection services)
{
    // inject IOperationResult for unexpected exception
    services.AddTransient<IOperationResult>(provider =>
        new OperationResult<object>(null, -1, OperationException.DefaultMessage));
    // inject filter globally  
    services.AddControllers(options => options.Filters.Add<OperationExceptionFilter>());
}

OperationExceptionFilterAttribute

we could use OperationExceptionFilterAttribute in the same way as OperationExceptionFilter to handle MVC framework exceptions. Plus, it can also be used on Controllers and Actions as an Attribute which allows for more detailed control of exceptions.

global
public void ConfigureServices(IServiceCollection services)
{
    // inject IOperationResult for unexpected exception
    services.AddTransient<IOperationResult>(provider =>
        new OperationResult<object>(null, -1, OperationException.DefaultMessage));
    // inject filter globally  
    services.AddControllers(options => options.Filters.Add<OperationExceptionFilterAttribute>());
}
Controller/Action
public void ConfigureServices(IServiceCollection services)
{
    // inject IOperationResult for unexpected exception
    services.AddTransient<IOperationResult>(provider =>
        new OperationResult<object>(null, -1, OperationException.DefaultMessage));
    services.AddControllers();
}

[ApiController]
[Route("[controller]")]
public class TestController : ControllerBase
{
    [HttpGet("{id}")]
    [OperationExceptionFilter]
    public Task<IOperationResult> GetAsync(int id)
    {
        if (id < 0)
            throw new OperationException("custom exception");

        if (id > 0)
            return Task.FromResult<IOperationResult>(new OperationResult<string>("success"));

        throw new Exception("unexpected exception");
    }
}

Sample

Sample project shows how to use this middleware.

Docs

https://ccstudio.org/dotnet/middleware/exception.html

Release Notes

set CamelCasePropertyNamesContractResolver for json serilization

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.0.4 90 9/14/2020
1.0.3 85 9/14/2020
1.0.2 156 9/13/2020
1.0.1 72 8/31/2020
1.0.0 140 8/30/2020