CSharpEssentials.Errors
9.0.0
dotnet add package CSharpEssentials.Errors --version 9.0.0
NuGet\Install-Package CSharpEssentials.Errors -Version 9.0.0
<PackageReference Include="CSharpEssentials.Errors" Version="9.0.0" />
<PackageVersion Include="CSharpEssentials.Errors" Version="9.0.0" />
<PackageReference Include="CSharpEssentials.Errors" />
paket add CSharpEssentials.Errors --version 9.0.0
#r "nuget: CSharpEssentials.Errors, 9.0.0"
#:package CSharpEssentials.Errors@9.0.0
#addin nuget:?package=CSharpEssentials.Errors&version=9.0.0
#tool nuget:?package=CSharpEssentials.Errors&version=9.0.0
CSharpEssentials.Errors
Errors as data instead of exceptions. Error is a value type with a code, description, ErrorType and optional ErrorMetadata. The package also converts exceptions to errors, maps ErrorType to HTTP status codes and back, and has two exception types that carry errors.
Targets net11.0, net10.0, net9.0 and netstandard2.1. Depends on CSharpEssentials.Core and CSharpEssentials.Enums.
Benchmarks for creating errors, equality and hashing, errors from exceptions, metadata and record ToString, and JSON against ErrorOr and FluentResults are in BENCHMARKS.md.
Installation
dotnet add package CSharpEssentials.Errors
Usage
Creating errors
using CSharpEssentials.Errors;
Error notFound = Error.NotFound("User.NotFound", "User was not found.");
Error validation = Error.Validation("Email.Invalid", "Email format is invalid.");
Error conflict = Error.Conflict("User.Duplicate", "User already exists.");
Error domain = Error.Domain("Payment.Declined", "The card was declined.");
Error failure = Error.Failure("Payment.GatewayDown", "The payment gateway did not respond.");
Error forbidden = Error.Forbidden("Order.Forbidden", "You cannot cancel this order.");
Error unauthorized = Error.Unauthorized("Token.Expired", "The token has expired.");
Error unexpected = Error.Unexpected("Db.Down", "The database is unreachable.");
Failure, Unexpected and Unknown map to HTTP 500. The AspNetCore problem details hide the description of a 5xx error unless ExposeExceptionDetails is on, so use Domain (422) for a business-rule rejection the client should read. The factories cover every ErrorType except Unknown; Error.Create(code, description, type, metadata = null) takes any ErrorType, so it can turn any IError into an Error. Each takes code, description and an optional ErrorMetadata, and all three have defaults: Error.NotFound() has the code NotFound and the description A 'Not Found' error has occurred. An Error exposes Code, Description, Type, NumericType (the int value of Type) and Metadata (null when none was given). It implements IError, and two errors are equal when type, code, description and metadata entries are equal. Metadata is compared by key, so entry order does not affect equality or GetHashCode.
Error metadata
Error withMeta = Error.NotFound(
"User.NotFound",
"User was not found.",
new ErrorMetadata()
.AddMetadata("TraceId", Guid.NewGuid().ToString())
.AddMetadata("RequestPath", "/api/users/123"));
ErrorMetadata is a Dictionary<string, object?>. AddMetadata keeps the existing value when the key is already present. AddStackTrace, AddException and AddExceptionDetailed add well-known keys (stackTrace, exception, exceptionType, exceptionStackTrace, exceptionMessage, innerException), Combine merges another instance without overwriting, and CreateEmpty, CreateWithStackTrace, CreateWithException and CreateWithExceptionDetailed are the static counterparts. exception holds a nested ErrorMetadata with the exceptionType, exceptionStackTrace, exceptionMessage and innerException keys, so the metadata stays serializable with System.Text.Json.
Combining errors
Error a = Error.Validation("Name.Empty", "Name is required.");
Error b = Error.Validation("Email.Invalid", "Email is invalid.");
Error[] combined = a + b;
Error[] many = Error.CreateMany(a, b); // params of Error or of Error collections
Error[] single = a; // implicit Error -> Error[]
Converting from exceptions
try { /* ... */ }
catch (Exception ex)
{
Error error = Error.Exception(ex);
}
Error.Exception(ex) uses the exception type name as the code, the exception message as the description and ErrorType.Unexpected (before 9.0 it was ErrorType.Failure). The metadata carries exceptionType, exceptionStackTrace (null when the exception was never thrown), exceptionMessage and, when there is one, a nested innerException. Overloads take an explicit code, an explicit code and description, an ErrorType and extra ErrorMetadata:
Error custom = Error.Exception("Orders.SaveFailed", "The order could not be saved.", ex, ErrorType.Unexpected);
Domain exceptions
using CSharpEssentials.Errors;
using CSharpEssentials.Exceptions;
Error error = Error.Validation("Order.Invalid", "Total must be greater than zero.");
throw new DomainException(error);
DomainException and EnhancedValidationException live in the CSharpEssentials.Exceptions namespace of this package; see the exception types.
HTTP status mapping
int status = ErrorType.NotFound.ToHttpStatusCode(); // 404
ErrorType type = 401.ToErrorType(); // Unauthorized
ErrorType |
HTTP status |
|---|---|
Validation |
400 |
Unauthorized |
401 |
Forbidden |
403 |
NotFound |
404 |
Conflict |
409 |
Domain |
422 |
Failure, Unexpected, Unknown |
500 |
ToErrorType is the reverse for 400, 401, 403, 404, 409, 422 (Domain) and 500 (Failure); every other status code gives Unexpected.
Unknown is the [EnumFallback] member of ErrorType: a data read (JSON with the CSharpEssentials.Json enum conventions, a cached or stored payload) maps a type name or number it does not know, such as a member added by a later version, to Unknown, which maps to 500. Request input through the AspNetCore enum conventions rejects Unknown. 8.x has no fallback member, so 8.x readers reject Domain; see the 8.x to 9.0 migration guide.
Domain-specific error hierarchies
Error is a readonly record struct and cannot be subclassed. Group static factories in one class per aggregate or bounded context instead (or use the typed kinds in the next section):
using CSharpEssentials.Errors;
public static class UserErrors
{
public static Error NotFound(Guid id) =>
Error.NotFound("User.NotFound", $"User '{id}' was not found.");
public static readonly Error AlreadyExists =
Error.Conflict("User.AlreadyExists", "A user with that email already exists.");
public static Error InvalidAge(int age) =>
Error.Validation("User.InvalidAge", $"Age {age} is not allowed; must be 18 or older.");
public static readonly Error Unauthorized =
Error.Unauthorized("User.Unauthorized", "You are not authorized to perform this action.");
}
public static class OrderErrors
{
public static readonly Error EmptyCart =
Error.Validation("Order.EmptyCart", "Cannot place an order with an empty cart.");
public static Error InsufficientFunds(decimal required, decimal available) =>
Error.Domain(
"Order.InsufficientFunds",
$"Payment requires {required:C} but only {available:C} is available.",
new ErrorMetadata().AddMetadata("Required", required).AddMetadata("Available", available));
}
Usage with Result:
using CSharpEssentials.ResultPattern; // CSharpEssentials.Results package
public Result<User> FindUser(Guid id)
{
User? user = _repo.Find(id);
if (user is null)
return UserErrors.NotFound(id); // implicit Error → Result<User>
return user;
}
public Result PlaceOrder(Cart cart, decimal balance)
{
if (cart.IsEmpty)
return OrderErrors.EmptyCart;
if (balance < cart.Total)
return OrderErrors.InsufficientFunds(cart.Total, balance);
return Result.Success();
}
Typed error kinds
TypedError is an abstract record that implements IError. Each ErrorType except Unknown has a kind that derives from it:
| Kind | Type |
|---|---|
ValidationError |
Validation |
UnauthorizedError |
Unauthorized |
ForbiddenError |
Forbidden |
NotFoundError |
NotFound |
ConflictError |
Conflict |
DomainError |
Domain |
FailureError |
Failure |
UnexpectedError |
Unexpected |
The CLR type carries the kind, so code can match on the type instead of reading Type. The kinds are not sealed: derive a record from one to give an error its own type. The derived type keeps the kind's Type (the override is sealed). The constructor rejects a null code or description, and so does a with expression.
Write a catalog per aggregate with predefined instances, a Codes class of constants, and a nested type for any error that callers must tell apart by type:
using CSharpEssentials.Errors;
public static class UserErrors
{
public static class Codes
{
public const string NotFound = "User.NotFound";
public const string Deleted = "User.Deleted";
public const string Locked = "User.Locked";
}
public static readonly NotFoundError NotFound = new(Codes.NotFound, "User not found.");
public static readonly NotFoundError Deleted = new(Codes.Deleted, "User was deleted.");
public static NotFoundError NotFoundById(Guid id) => new(Codes.NotFound, $"User {id} not found.");
public sealed record Locked(Guid Id) : ConflictError(Codes.Locked, "User is locked.");
}
SonarAnalyzer reports S3218 on Codes.NotFound because it shares a name with the NotFound field. The shadowing is harmless here; move the constants to a separate UserErrorCodes class if your build treats that rule as an error.
A static field cannot be a pattern, so match predefined instances on their constant code. Put the bare kind after the code arms:
string message = error switch
{
NotFoundError { Code: UserErrors.Codes.Deleted } => "User was deleted.",
NotFoundError => "User not found.",
UserErrors.Locked locked => $"User {locked.Id} is locked.",
_ => error.Description,
};
ValidationError.Field reads the field metadata entry (ErrorMetadata.FieldKey), the property path that CSharpEssentials.Validation stores on each failure. Build the typed error from a validation failure with its metadata to keep it: new ValidationError(error.Code, error.Description, error.Metadata).Field is Address.City. ErrorMetadata.GetField() returns the same value from any metadata, including a string read back from JSON. Field is not serialized; the entry stays in Metadata.
ToError() turns a typed error into an Error with the same code, description and type, and the same ErrorMetadata instance, so it feeds today's Result and the AspNetCore problem details:
Result<User> result = UserErrors.NotFoundById(id).ToError();
[ErrorKind(ErrorType.X)] declares the kind on the type itself, so tools can read it without an instance. Every built-in kind has it and derived types inherit it. Do not redeclare it with a different kind on a derived error: Type stays the base kind's, so the attribute would disagree with the runtime type. The CSE5005 analyzer reports that mismatch, and a Type property that returns another constant kind; it ships with the CSharpEssentials.Results package, so a project that references only CSharpEssentials.Errors does not get it. The attribute also works on a struct that implements IError:
[ErrorKind(ErrorType.Conflict)]
public readonly record struct OrderLocked(int OrderId) : IError
{
public string Code => "Order.Locked";
public string Description => $"Order {OrderId} is locked.";
public ErrorType Type => ErrorType.Conflict;
public int NumericType => (int)Type;
public ErrorMetadata? Metadata => null;
}
A struct error is boxed whenever it is used as IError, so prefer the class kinds. Bridge it with Error.Create(e.Code, e.Description, e.Type, e.Metadata). Nothing stops default(OrderLocked), and a struct that stores its code in a field returns a null Code from default, which breaks the IError contract.
Equality:
- Two typed errors are equal when the runtime type, code, description and type match and the metadata has the same content (compared like
Errorcompares it). - A derived error never equals its base kind, even with the same code and description.
- Equality does not survive serialization when metadata holds values: System.Text.Json reads them back as
JsonElement. Code and type do survive. - Tell errors apart by
Code, not by comparing values across kinds or after a round trip.
Predefined instances share one ErrorMetadata dictionary, which is mutable. Do not add metadata to a shared instance; create a new error with its own metadata instead.
Moving a catalog to typed kinds
A catalog of Error fields does not feed a typed result. Error has no conversion to a kind, because its kind is only known at run time, so returning UserErrors.AlreadyExists from the catalog above into a ConflictError slot of Result<T, E1..E3> reports CS0029. Rewrite each catalog once:
- Change the field or return type from
Errorto the kind. - Construct the kind instead of calling the
Errorfactory. - Add a
Codesclass with the codes as constants, so callers can match on them.
// Before
public static class UserErrors
{
public static readonly Error AlreadyExists =
Error.Conflict("User.AlreadyExists", "A user with that email already exists.");
public static Error InvalidAge(int age) =>
Error.Validation("User.InvalidAge", $"Age {age} is not allowed; must be 18 or older.");
}
// After
public static class UserErrors
{
public static class Codes
{
public const string AlreadyExists = "User.AlreadyExists";
public const string InvalidAge = "User.InvalidAge";
}
public static readonly ConflictError AlreadyExists =
new(Codes.AlreadyExists, "A user with that email already exists.");
public static ValidationError InvalidAge(int age) =>
new(Codes.InvalidAge, $"Age {age} is not allowed; must be 18 or older.");
}
The codes, descriptions and HTTP statuses stay the same. Code written for Result<T> needs ToError() where it used a catalog entry as an Error: a kind has no implicit conversion to Error or Result<T> (CS0029), and comparing a kind with an Error through == does not compile (CS0019). Compare codes instead.
Result<User> legacy = UserErrors.AlreadyExists.ToError();
Result<User, ConflictError> typed = UserErrors.AlreadyExists;
Upgrading to 8.0
ErrorMetadata.CreateWithException and AddException no longer store the Exception object under exception. They store a nested ErrorMetadata with exceptionType, exceptionStackTrace, exceptionMessage and innerException, because System.Text.Json cannot serialize a thrown exception. Code that cast metadata["exception"] to Exception should read the nested keys instead, or keep the exception in its own variable.
The nested entry keeps only the short type name (GetType().Name), the message, the stack trace and the inner exception chain. The full type name, Data, HResult and the exception object itself are dropped, so code that rethrew metadata["exception"] or tested it with is SqlException has no key to migrate to. Keep the exception in its own variable if you need its identity.
exceptionStackTrace holds exception.StackTrace and nothing else. An exception that was never thrown has no stack trace, so the key is present with a null value. Earlier versions fell back to Environment.StackTrace, which recorded where the metadata was built rather than where the exception came from and cost a full stack walk on every call. Call AddStackTrace() if you want the current call stack. Because the key no longer depends on the call site, Error.Exception values built from the same unthrown exception now compare equal.
Error equality and GetHashCode now compare nested ErrorMetadata values by content. Do not add a metadata instance to itself (directly or through a chain): hashing or comparing such a cycle overflows the stack.
Code and Description reject null in with expressions too. error with { Code = null! } used to produce an Error with a null code; it now throws ArgumentNullException, as the factory methods already did.
ErrorMetadata.ToString() formats values with the invariant culture on every target. The netstandard2.1 build, which net8.0 apps load, used the current culture, so 2.5 came out as 2,5 under tr-TR. Code that parsed that text with the current culture should parse it as invariant.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. 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. net11.0 is compatible. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- CSharpEssentials.Core (>= 9.0.0)
- CSharpEssentials.Enums (>= 9.0.0)
- System.Text.Json (>= 9.0.4)
-
net10.0
- CSharpEssentials.Core (>= 9.0.0)
- CSharpEssentials.Enums (>= 9.0.0)
-
net11.0
- CSharpEssentials.Core (>= 9.0.0)
- CSharpEssentials.Enums (>= 9.0.0)
-
net9.0
- CSharpEssentials.Core (>= 9.0.0)
- CSharpEssentials.Enums (>= 9.0.0)
NuGet packages (8)
Showing the top 5 NuGet packages that depend on CSharpEssentials.Errors:
| Package | Downloads |
|---|---|
|
CSharpEssentials.Results
Result pattern implementation for functional error handling in C#. Provides Result<T> and Result types for railway-oriented programming, eliminating exceptions for expected failures. Foundation for functional programming patterns and robust error handling. Bundles analyzers and code fixes for the typed results (CSE5001-CSE5006) and a source generator that registers typed result JSON converters for Native AOT. |
|
|
CSharpEssentials
A comprehensive C# library enhancing functional programming capabilities with type-safe monads (Maybe, Result), discriminated unions (Any), and robust error handling. Features include: domain-driven design support, enhanced Entity Framework integration, testable time management, JSON utilities, and LINQ extensions. Built for modern C# development with focus on maintainability, testability, and functional programming principles. |
|
|
CSharpEssentials.AspNetCore
A comprehensive ASP.NET Core library that enhances functional programming capabilities in web applications. Features include API versioning, global exception handling with enhanced problem details, enum binding and wire formats, model validation, optimized JSON handling, and seamless integration with CSharpEssentials core functional patterns (Result, Maybe, Rule Engine). Perfect for building robust, maintainable, and type-safe web APIs following functional programming principles. OpenAPI documents come from CSharpEssentials.AspNetCore.OpenApi or CSharpEssentials.AspNetCore.Swashbuckle. |
|
|
CSharpEssentials.Maybe
Maybe monad implementation with LINQ support for functional programming in C#. Provides type-safe null handling, eliminates null reference exceptions, and enables functional composition. Essential for functional programming patterns and safe value handling. |
|
|
CSharpEssentials.Http
HttpClient extensions that bridge HTTP calls to the Result pattern. Provides GetFromJsonAsResultAsync, PostAsJsonAsResultAsync, status code mapping, and Polly resilience integration. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 9.0.0 | 0 | 11/28/2026 |
| 8.0.0 | 554 | 11/9/2026 |
| 7.0.0 | 539 | 10/20/2026 |
| 6.5.2 | 507 | 10/10/2026 |
| 6.5.1 | 478 | 10/10/2026 |
| 6.5.0 | 470 | 10/9/2026 |
| 6.4.0 | 479 | 10/9/2026 |
| 6.3.0 | 476 | 10/8/2026 |
| 6.2.0 | 439 | 10/8/2026 |
| 6.1.0 | 468 | 10/7/2026 |
| 6.0.0 | 460 | 10/7/2026 |
| 5.2.1 | 484 | 10/7/2026 |
| 5.2.0 | 480 | 10/7/2026 |
| 5.1.0 | 491 | 10/7/2026 |
| 5.0.0 | 485 | 10/6/2026 |
| 4.1.0 | 475 | 10/6/2026 |
| 4.0.0 | 464 | 10/5/2026 |
| 3.2.3 | 704 | 5/31/2026 |
| 3.2.2 | 521 | 5/31/2026 |
| 3.2.1 | 483 | 5/31/2026 |