ColinChang.ExceptionHandler
1.0.4
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
<PackageReference Include="ColinChang.ExceptionHandler" Version="1.0.4" />
paket add ColinChang.ExceptionHandler --version 1.0.4
#r "nuget: ColinChang.ExceptionHandler, 1.0.4"
// 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
Product | Versions 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. |
-
.NETCoreApp 3.1
- ColinChang.ExceptionHandler.Abstractions (>= 1.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 3.1.7)
- Newtonsoft.Json (>= 12.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
set CamelCasePropertyNamesContractResolver for json serilization