CrCore.Exceptions
10.0.47
Prefix Reserved
See the version list below for details.
dotnet add package CrCore.Exceptions --version 10.0.47
NuGet\Install-Package CrCore.Exceptions -Version 10.0.47
<PackageReference Include="CrCore.Exceptions" Version="10.0.47" />
<PackageVersion Include="CrCore.Exceptions" Version="10.0.47" />
<PackageReference Include="CrCore.Exceptions" />
paket add CrCore.Exceptions --version 10.0.47
#r "nuget: CrCore.Exceptions, 10.0.47"
#:package CrCore.Exceptions@10.0.47
#addin nuget:?package=CrCore.Exceptions&version=10.0.47
#tool nuget:?package=CrCore.Exceptions&version=10.0.47
Intro
A high-performance .NET library to map external error codes into typed exceptions and translate infrastructure failures into domain exceptions.
Exception Factory
Use Case: Materialize string-based error codes from external APIs into strongly-typed exceptions.
{
"error": "invalid_grant"
}
Creating
public sealed class KeycloakInvalidGrantException : ValidationException
{
private static readonly ImmutableArray<CrError> _errors
= [new("IdentityInvalidGrant", "The authorization grant is invalid.")];
public KeycloakInvalidGrantException() : base(_errors) { }
}
Registration
var builder = new ExceptionFactoryBuilder()
.Map("invalid_grant", static () => new KeycloakInvalidGrantException());
var factory = builder.Build();
Receiving
throw factory.Create(errorCode); // Throws KeyNotFoundException if no mapping exists.
Or:
if (factory.TryCreate(errorCode, out var exception)) throw exception;
Exception Translator
Use Case: Catch infrastructure failures and translate them into clean domain exceptions.
The mapping process traverses the exception type hierarchy.
Creating
public sealed class UserInvalidCredentialsException : ValidationException
{
private static readonly ImmutableArray<CrError> _errors = [new("UserInvalidCredentials")];
public UserInvalidCredentialsException(Exception? innerException = null) : base(_errors, innerException) { }
}
Registration
var builder = new ExceptionTranslatorBuilder()
.Map<KeycloakInvalidGrantException>(static ex => new UserInvalidCredentialsException(ex));
var translator = builder.Build();
Receiving
try
{
// Your code
}
catch (Exception ex)
{
throw translator.Translate(ex); // Throws KeyNotFoundException if no mapping exists.
}
Or:
try
{
// Your code
}
catch (Exception ex)
{
if (translator.TryTranslate(ex, out var translatedException))
{
throw translatedException;
}
throw;
}
Core
Base: All custom exceptions inherit from the CrException class.
It carries an ImmutableArray of structured error details (zero or more CrError instances):
record class CrError(string Code, string? Message = null);
Exception Categories
Inherit from semantic categories depending on the architectural context:
See the Categories folder for all available built-in exceptions.
| 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
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.