csgsolutions-dev - Csg.AspNetCore.ExceptionManagement 1.0.8-preview
Package Description
PM> Install-Package Csg.AspNetCore.ExceptionManagement -Version 1.0.8-preview -Source https://www.myget.org/F/csgsolutions-dev/api/v3/index.json
> nuget.exe install Csg.AspNetCore.ExceptionManagement -Version 1.0.8-preview -Source https://www.myget.org/F/csgsolutions-dev/api/v3/index.json
> dotnet add package Csg.AspNetCore.ExceptionManagement --version 1.0.8-preview --source https://www.myget.org/F/csgsolutions-dev/api/v3/index.json
<PackageReference Include="Csg.AspNetCore.ExceptionManagement" Version="1.0.8-preview" />
Copy to clipboard
source https://www.myget.org/F/csgsolutions-dev/api/v3/index.json
nuget Csg.AspNetCore.ExceptionManagement ~> 1.0.8-preview
Copy to clipboard
> choco install Csg.AspNetCore.ExceptionManagement --version 1.0.8-preview --source https://www.myget.org/F/csgsolutions-dev/api/v2
Import-Module PowerShellGet
Register-PSRepository -Name "csgsolutions-dev" -SourceLocation "https://www.myget.org/F/csgsolutions-dev/api/v2"
Install-Module -Name "Csg.AspNetCore.ExceptionManagement" -RequiredVersion "1.0.8-preview" -Repository "csgsolutions-dev" -AllowPreRelease
Copy to clipboard
Introduction
Provides basic exception management for an ASP.NET Core Web API.
Getting Started
Install the Nuget package.
Packages
| Package | NuGet Stable | NuGet Pre-release | MyGet Dev |
|---|---|---|---|
| Csg.AspNetCore.ExceptionManagement | n/a | n/a | Link |
Examples
dotnet add package Csg.AspNetCore.ExceptionManagement
<PackageReference Include="Csg.AspNetCore.ExceptionManagement" Version="<version>" />
Add Services
Register the exception management related services in ConfigureServices()
public void ConfigureServices(IServiceCollection services)
{
// other services
services.AddExceptionManagement().AddWebApiDefaults()
// other services
}
Add Middlware
Add the exception management middleware (exception handler) into the exception handler pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseExceptionManagement(env.IsDevelopment());
// other middlware
}
The UseExceptionManagement() method has a single parameter, allowUnsafeExceptions, whose default
value is false. This parameter indicates if an ExceptionResult should be passed to
the exception handler if the IsSafe property of the result has not been set to true.
In the above example code, we pass in the value of env.IsDevelopment() in order to ensure
that all error messages are marked as safe when running in a development environment. In any other
environment, you can use Filters to ensure that only the exception data you want to return to the caller
is sent.
Adding Filters
An exception filter is a method (usually static) matching the signature of the following delegate:
public delegate void ExceptionFilterDelegate(ExceptionContext context);
For example, to build an exception filter that handles any thrown SqlException, the following
filter could be used:
public class ExceptionFilters
{
public static void FilterSqlExceptions(Csg.AspNetCore.ExceptionManagement.ExceptionContext context)
{
// Handle duplicate key exception
if (context.Error is SqlException sqlException && sqlException.Number == 2601)
{
// The context.Result property will be passed to the exception handler
context.Result = new Csg.AspNetCore.ExceptionManagement.ExceptionResult()
{
StatusCode = 500,
IsSafe = true, //Ensures this result can be sent to a caller in all environments
ErrorCode = "SqlException",
ErrorTitle = "Duplicate Value Error",
ErrorDetail = "A value provided was the same as an existing value."
};
}
}
}
This filter would return an HTTP 500 status code and error information to the caller. It can be registered in ConfigureServices
services.AddExceptionManagement()
.AddWebApiDefaults()
.AddFilter(ExceptionFilters.FilterSqlExceptions);
The context.Result property must be set to an instance of ExceptionResult, which has the following properties:
| Property | Data Type | Default Value | Description |
|---|---|---|---|
| StatusCode | Int32 | 500 | The HTTP Status Code that should be sent in the response. |
| IsSafe | String | false | Indicates if the error information provided is safe to display to a caller in any environment. |
| ErrorCode | String | null | An opaque value used to convey information to a caller. |
| ErrorTitle | String | null | An opaque value used to convey information to a caller. |
| ErrorDetail | String | null | An opaque value used to convey information to a caller. |
| ErrorData | Object | null | An opaque value used to convey information to a caller. |
The ErrorData property can be used to return complex objects to the caller with additional error information.
Using a Custom Handler
Options can be configured by using a setup action with AddExceptionManagement().
services.AddExceptionManagement(options =>
{
options.Handler = CustomHandlerDelegate;
options.UnsafeResult = ExceptionManagementOptions.GenericErrorResult;
});
Defines the handler that is responsible for turning an ExceptionResult into an API response.
A handler must be a function that matches the ExceptionHandlerDelegate method signature as follows:
public delegate Task ExceptionHandlerDelegate(ExceptionContext context);
Using a Custom Unsafe Result
When using AddWebApiDefaults(), the value of the UnsafeResult configuration option is passed to the exception handler when:
ExceptionContext.Result.IsSafe == false- and
allowUnsafeExceptionspassed toUseExceptionManagement()isfalse(the default).
You can use a custom UnsafeResult instead by setting the configuration option.
public void ConfigureServices(IServiceCollection services)
{
services.AddExceptionManagement(options =>
{
options.UnsafeResult = new ExceptionResult()
{
StatusCode = 418,
ErrorTitle = "The server is a teapot",
ErrorDetail = "This server is short and stout."
};
}).AddWebApiDefaults();
}
Default Handler Behavior
When using AddWebApiDefaults(), the configured handler is Handlers.WebApiExceptionHandler.
This handler produces the following output when the request Accept header is for text/plain:
ID: <value>
Title: <value>
Detail: <value>
ErrorCode: <value>
and this output when the request Accept header is missing or for application/json:
{
"title": "<value of Result.ErrorTitle>",
"detail": "<value of Result.ErrorDetail>",
"instance": "<value of HttpContext.Request.Path>"
"id": "<value of ExceptionContext.ErrorID>",
"code": "<value of Result.ErrorCode>",
"data": { Result.ErrorData }
}
All the above values are from their corresponding properties on ExceptionResult, with the exception of ID,
which is from the ErrorID on the ExceptionContext object passed into each filter. By default, this contains
a new UUID (generated with System.Guid.NewGuid()). The ID property can be assigned by any registered filter.
-
.NETStandard 2.0
- Microsoft.AspNetCore.Diagnostics (>= 2.0.0)
- Microsoft.AspNetCore.Http.Abstractions (>= 2.0.0)
- Microsoft.AspNetCore.Http.Extensions (>= 2.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 2.0.0)
- Microsoft.Extensions.Options (>= 2.0.0)
- Newtonsoft.Json (>= 9.0.1)
- .NETStandard 2.0: 2.0.0.0
OwnersJustin Buchanan |
AuthorsCSG |
Project URLhttps://github.com/csgsolutions/Csg.AspNetCore.ExceptionManagement |
LicenseUnknown |
Info53 total downloads |
| 6 downloads for version 1.0.8-preview |
| Download (20.11 KB) |
| Found on the current feed only |
Package history
| Version | Size | Last updated | Downloads | Mirrored? | |||
|---|---|---|---|---|---|---|---|
|
1.0.8-preview | 20.11 KB | Tue, 20 Oct 2020 22:19:21 GMT | 6 |
|
||
|
1.0.3-preview | 15.54 KB | Mon, 20 Jan 2020 19:54:24 GMT | 4 |
|
||
|
1.0.1-preview | 15.42 KB | Tue, 03 Dec 2019 16:07:10 GMT | 5 |
|
||
|
1.0.0-preview-00008 | 10.35 KB | Tue, 03 Dec 2019 15:10:54 GMT | 5 |
|
||
|
1.0.0-preview-00007 | 9.93 KB | Fri, 12 Jul 2019 21:23:54 GMT | 6 |
|
||
|
1.0.0-preview-00006 | 9.93 KB | Fri, 12 Jul 2019 21:17:37 GMT | 5 |
|
||
|
1.0.0-preview-00005 | 9.92 KB | Fri, 12 Jul 2019 16:41:21 GMT | 7 |
|
||
|
1.0.0-preview-00004 | 9.92 KB | Fri, 12 Jul 2019 13:42:07 GMT | 5 |
|
||
|
1.0.0-preview-00003 | 9.52 KB | Fri, 12 Jul 2019 12:58:51 GMT | 5 |
|
||
|
1.0.0-preview-00002 | 9.53 KB | Thu, 11 Jul 2019 21:20:55 GMT | 5 |
|