CrCore.Exceptions.AspNetCore
10.0.47
Prefix Reserved
See the version list below for details.
dotnet add package CrCore.Exceptions.AspNetCore --version 10.0.47
NuGet\Install-Package CrCore.Exceptions.AspNetCore -Version 10.0.47
<PackageReference Include="CrCore.Exceptions.AspNetCore" Version="10.0.47" />
<PackageVersion Include="CrCore.Exceptions.AspNetCore" Version="10.0.47" />
<PackageReference Include="CrCore.Exceptions.AspNetCore" />
paket add CrCore.Exceptions.AspNetCore --version 10.0.47
#r "nuget: CrCore.Exceptions.AspNetCore, 10.0.47"
#:package CrCore.Exceptions.AspNetCore@10.0.47
#addin nuget:?package=CrCore.Exceptions.AspNetCore&version=10.0.47
#tool nuget:?package=CrCore.Exceptions.AspNetCore&version=10.0.47
Intro
ASP.NET Core integration for Cr.Exceptions to automatically map exceptions into RFC-compliant Problem Details with customizable HTTP status codes and log levels.
Installation
Default
builder.Services.AddCrExceptionHandling();
app.UseExceptionHandler();
Custom
builder.Services
.AddCrExceptionHandler()
.AddCrStatusCodeMapping(builder =>
{
// Configure
})
.AddCrLogLevelMapping(builder =>
{
// Configure
});
app.UseExceptionHandler();
Exception Handling
Flow
When a CrException is thrown:
public sealed class KeycloakInvalidGrantException : ValidationException
{
private static readonly ImmutableArray<CrError> _errors
= [new("IdentityInvalidGrant", "The authorization grant is invalid.")];
public KeycloakInvalidGrantException() : base(_errors) { }
}
The client receives a next response:
{
"type": "https://tools.ietf.org/html/rfc9110#section-15.5.1",
"title": "Bad Request",
"status": 400,
"errors": [
{
"code": "IdentityInvalidGrant",
"message": "The authorization grant is invalid."
}
],
"traceId": "6dd0bbb69ae26c77dd09d915915b1958"
}
traceId populates from Activity.Current?.TraceId with a fallback to HttpContext.TraceIdentifier.
If an exception is not derived from CrException, or for any unknown exception, the handler returns the following fallback response:
{
"type": "https://tools.ietf.org/html/rfc9110#section-15.6.1",
"title": "An error occurred while processing your request.",
"status": 500,
"errors": [
{
"code": "InternalError",
"message": "An unexpected internal error occurred."
}
],
"traceId": "ff3e0d35e6f0fa022a6de34583ea9e81"
}
Logging
The handler emits structured logs for exceptions and handling events. See Log Event IDs.
HTTP Status Code Mapping
You can customize how CrException types map to HTTP status codes.
The mapping process traverses the exception type hierarchy.
Registration
builder.Services.AddCrStatusCodeMapping(builder =>
{
builder.AddDefaultMappings();
builder.Map<MyCustomException>(StatusCodes.Status499ClientClosedRequest);
});
See Default Mappings.
Log Level Mapping
You can customize how CrException types map to a LogLevel.
The mapping process traverses the exception type hierarchy.
Registration
builder.Services.AddCrLogLevelMapping(builder =>
{
builder.AddDefaultMappings();
builder.Map<MyCustomException>(LogLevel.Warning);
});
See Default Mappings.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- CrCore.Exceptions (>= 10.0.47)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.