CrCore.Exceptions
10.0.42
Prefix Reserved
See the version list below for details.
dotnet add package CrCore.Exceptions --version 10.0.42
NuGet\Install-Package CrCore.Exceptions -Version 10.0.42
<PackageReference Include="CrCore.Exceptions" Version="10.0.42" />
<PackageVersion Include="CrCore.Exceptions" Version="10.0.42" />
<PackageReference Include="CrCore.Exceptions" />
paket add CrCore.Exceptions --version 10.0.42
#r "nuget: CrCore.Exceptions, 10.0.42"
#:package CrCore.Exceptions@10.0.42
#addin nuget:?package=CrCore.Exceptions&version=10.0.42
#tool nuget:?package=CrCore.Exceptions&version=10.0.42
Intro
A lightweight library for defining application errors, creating typed exceptions, and mapping external error codes into domain-specific exceptions.
This package contains only the core exception model and does not depend on ASP.NET Core.
Features
- Typed application exceptions
- Standard exception categories
- Structured application errors (
CrError) - External error mapping (
ErrorMap) - Exception factory (
ExceptionFactory) - No ASP.NET Core dependencies
Installation
dotnet add package CrCore.Exceptions
Error Model
Every application error is represented by CrError.
var error = new CrError("IdentityUserNotFound", "User was not found.");
Each error contains:
Code— stable identifier for clients.Message— human-readable description.
Exception Categories
Applications should inherit from one of the predefined exception categories.
Available categories:
| Exception | Purpose |
|---|---|
ValidationException |
Validation failures |
UnauthorizedException |
Authentication required |
ForbiddenException |
Access denied |
NotFoundException |
Resource not found |
ConflictException |
Resource conflict |
UnprocessableException |
Business rule violation |
InternalException |
Internal server error |
Example:
public sealed class UserNotFoundException : NotFoundException
{
public UserNotFoundException(Guid userId) : base(
[new CrError("IdentityUserNotFound", $"User '{userId}' was not found.")],
"User was not found.")
{
}
}
Usage:
throw new UserNotFoundException(userId);
ErrorMap
External systems usually expose their own error codes.
For example:
user_not_found
Those codes can be mapped into application errors.
ErrorMap errorMap = builder
.Add(new ErrorRegistration(
"user_not_found",
[new CrError("IdentityUserNotFound", "User was not found.")]))
.Build();
Resolving an external error:
if (errorMap.TryGet("user_not_found", out var errors))
{
throw new UserNotFoundException(errors);
}
This keeps external service contracts isolated from the application domain.
ExceptionFactory
ExceptionFactory creates typed exceptions from registered external error codes.
Registration:
ExceptionFactory factory = builder
.Add(new ExceptionRegistration(
new ErrorRegistration(
"invalid_grant",
[new CrError("IdentityInvalidCredentials", "Invalid username or password.")]),
errors => new InvalidCredentialsException(errors)))
.Build();
Usage:
throw factory.Create("invalid_grant");
The factory only creates exceptions for registered error codes.
| 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
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on CrCore.Exceptions:
| Package | Downloads |
|---|---|
|
CrCore.Exceptions.AspNet
ASP.NET Core integration for CR.Exceptions. |
GitHub repositories
This package is not used by any popular GitHub repositories.