ColinChang.ExceptionHandler 1.0.4

There is a newer version of this package available.
See the version list below for details.
dotnet add package ColinChang.ExceptionHandler --version 1.0.4                
NuGet\Install-Package ColinChang.ExceptionHandler -Version 1.0.4                
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="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                
#r "nuget: ColinChang.ExceptionHandler, 1.0.4"                
#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 ColinChang.ExceptionHandler as a Cake Addin
#addin nuget:?package=ColinChang.ExceptionHandler&version=1.0.4

// Install ColinChang.ExceptionHandler as a Cake Tool
#tool nuget:?package=ColinChang.ExceptionHandler&version=1.0.4                

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

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  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. 
.NET Core netcoreapp3.1 is compatible. 
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
1.0.6 611 3/29/2021
1.0.4 454 9/14/2020
1.0.3 442 9/14/2020
1.0.2 518 9/13/2020
1.0.1 403 8/31/2020
1.0.0 467 8/30/2020

set CamelCasePropertyNamesContractResolver for json serilization