CrCore.Exceptions 10.0.40

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package CrCore.Exceptions --version 10.0.40
                    
NuGet\Install-Package CrCore.Exceptions -Version 10.0.40
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CrCore.Exceptions" Version="10.0.40" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CrCore.Exceptions" Version="10.0.40" />
                    
Directory.Packages.props
<PackageReference Include="CrCore.Exceptions" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CrCore.Exceptions --version 10.0.40
                    
#r "nuget: CrCore.Exceptions, 10.0.40"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package CrCore.Exceptions@10.0.40
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CrCore.Exceptions&version=10.0.40
                    
Install as a Cake Addin
#tool nuget:?package=CrCore.Exceptions&version=10.0.40
                    
Install as a Cake Tool

Intro

A lightweight framework for defining application errors, creating typed exceptions, and exposing consistent error responses across application boundaries.

CR.Exceptions separates:

  • error definition (CrError);
  • application exceptions (CrException);
  • external error mapping (ErrorMap);
  • exception creation (ExceptionFactory);
  • HTTP response representation (ASP.NET Core integration).

The goal is to keep external service errors (for example Keycloak, GitHub, payment providers) isolated from application logic while providing a consistent error contract for clients.


Installation

Register the exception handler during application startup:

builder.Services.AddCrExceptionHandler();

app.UseExceptionHandler();

Custom mappings can be configured:

builder.Services.AddCrExceptionHandler(options =>
{
    options.StatusCodes.AddDefaultMappings();
    options.StatusCodes.Map<MyCustomException>(499);
});

Error Model

Every application error is represented by CrError.

Each error provides:

  • Code — stable identifier used by clients.
  • Message — human-readable error description.

Application Exceptions

Application exceptions are created by inheriting from one of the provided exception categories.

Available categories:

Exception HTTP Status
ValidationException 400
UnauthorizedException 401
ForbiddenException 403
NotFoundException 404
ConflictException 409
UnprocessableException 422

Example:

public sealed class UserNotFoundException : NotFoundException
{
    public UserNotFoundException(Guid userId) : base(
        [new CrError("Identity.UserNotFound", $"User '{userId}' was not found.")],
        "User was not found.")
    {
    }
}

Throw exceptions normally:

throw new UserNotFoundException(userId);

External Error Mapping

External systems usually expose their own error codes.

For example, an external API may return:

user_not_found

These codes can be registered in ErrorMap and converted into application-level errors:

ErrorMap errorMap = _errorMapBuilder.Add(new ErrorRegistration(
    "user_not_found",
    [new CrError("IdentityUserNotFound", "User was not found.")])).Build();

After registration, the application can resolve errors by external code:

if (errorMap.TryGet("user_not_found", out var errors))
{
    throw new UserNotFoundException(errors);
}

This allows application services to decide how external errors should be handled.

For example, API clients may map external HTTP responses to application exceptions:

switch (response.StatusCode)
{
    case 404:
        throw new ApiNotFoundException(errors);

    case 403:
        throw new ApiForbiddenException(errors);

    default:
        throw new ApiException(errors);
}

ExceptionFactory

ExceptionFactory creates typed exceptions from registered error codes.

Example registration:

var registration = new ErrorRegistration(
    "invalid_grant",
    [new CrError("IdentityInvalidCredentials", "Invalid username or password.")]);

ExceptionFactory factory = _exceptionFactoryBuilder.Add(
    new ExceptionRegistration(
        registration,
        errors => new InvalidCredentialsException(errors))).Build();

After registration:

var exception = factory.Create("invalid_grant");
throw exception;

The factory only handles registered error codes. If a code is not registered, the application should decide how to handle this case.


ASP.NET Core Integration

CR.Exceptions automatically converts CrException instances into RFC 7807 ProblemDetails responses.

Example response:

{
  "type": "https://tools.ietf.org/html/rfc9110#section-15.5.5",
  "title": "Not Found",
  "status": 404,
  "detail": "The requested resource was not found.",
  "instance": "/api/test",
  "errors": [
    {
      "code": "TestNotFound",
      "message": "Test Entity not found"
    }
  ],
  "traceId": "5a1192a06ca5cd006057ca7e6b84e231"
}

Clients should use the errors[].code value as the stable identifier.


Internal Errors

Unexpected exceptions are converted into a generic internal error response.

Example:

{
  "type": "https://tools.ietf.org/html/rfc9110#section-15.6.1",
  "title": "An error occurred while processing your request.",
  "status": 500,
  "detail": "An unexpected error occurred.",
  "instance": "/api/test",
  "errors": [
    {
      "code": "InternalError",
      "message": "An unexpected internal error occurred."
    }
  ],
  "traceId": "3e071f69b5f9e695a32a699369b57651"
}
Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • 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.

Version Downloads Last Updated
10.0.45 104 8/7/2026
10.0.44 94 8/5/2026
10.0.43 112 8/3/2026
10.0.42 109 8/1/2026
10.0.41 115 8/1/2026
10.0.40 109 7/30/2026
10.0.33 110 7/27/2026