CrCore.Exceptions.AspNet
10.0.33
Prefix Reserved
See the version list below for details.
dotnet add package CrCore.Exceptions.AspNet --version 10.0.33
NuGet\Install-Package CrCore.Exceptions.AspNet -Version 10.0.33
<PackageReference Include="CrCore.Exceptions.AspNet" Version="10.0.33" />
<PackageVersion Include="CrCore.Exceptions.AspNet" Version="10.0.33" />
<PackageReference Include="CrCore.Exceptions.AspNet" />
paket add CrCore.Exceptions.AspNet --version 10.0.33
#r "nuget: CrCore.Exceptions.AspNet, 10.0.33"
#:package CrCore.Exceptions.AspNet@10.0.33
#addin nuget:?package=CrCore.Exceptions.AspNet&version=10.0.33
#tool nuget:?package=CrCore.Exceptions.AspNet&version=10.0.33
Intro
A lightweight library for defining application exceptions and automatically converting them into HTTP responses in ASP.NET Core.
Installation
Register the exception handler during application startup.
builder.Services.AddCrExceptionHandler();
Or configure custom exception mappings.
builder.Services.AddCrExceptionHandler(options =>
{
options.ExceptionMapping.AddDefaultMappings();
options.ExceptionMapping.Map<MyCustomException>(499);
});
Enable exception handling middleware.
app.UseExceptionHandler();
Creating a Custom Exception
Create your own exception by inheriting from one of the provided exception categories.
Example:
public sealed class UserNotFoundException : NotFoundException
{
public UserNotFoundException(Guid userId) : base(
[new CrError("UserNotFound", $"User '{userId}' was not found.")],
"User was not found.")
{
}
}
public sealed record class CrError(string Code, string Message);
Each error provides:
Code— unique identifier used by clients.Message— human-readable error description.
Throwing an Exception
Throw your custom exception normally.
throw new UserNotFoundException(userId);
The exception handler automatically converts it into an RFC 7807 ProblemDetails response.
Example:
{
"type": "about:blank",
"title": "Not Found",
"status": 404,
"detail": "User was not found.",
"instance": "/api/users/123",
"traceId": "0HNNAF6ABMHQO",
"errors": [
{
"code": "UserNotFound",
"message": "User '123' was not found."
}
]
}
Default Exception Mappings
| Exception Category | HTTP Status |
|---|---|
ValidationException |
400 Bad Request |
UnauthorizedException |
401 Unauthorized |
ForbiddenException |
403 Forbidden |
NotFoundException |
404 Not Found |
ConflictException |
409 Conflict |
UnprocessableException |
422 Unprocessable Entity |
Internal Errors
Unexpected exceptions are automatically converted into a generic internal error response.
Example:
{
"type": "about:blank",
"title": "Internal Server Error",
"status": 500,
"detail": "An unexpected error occurred.",
"instance": "",
"traceId": "0HNNAF6ABMHQO",
"errors": [
{
"code": "InternalError",
"message": "An unexpected internal error occurred."
}
]
}
| 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.33)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.