CSharpEssentials.Errors 3.0.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package CSharpEssentials.Errors --version 3.0.2
                    
NuGet\Install-Package CSharpEssentials.Errors -Version 3.0.2
                    
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="CSharpEssentials.Errors" Version="3.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CSharpEssentials.Errors" Version="3.0.2" />
                    
Directory.Packages.props
<PackageReference Include="CSharpEssentials.Errors" />
                    
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 CSharpEssentials.Errors --version 3.0.2
                    
#r "nuget: CSharpEssentials.Errors, 3.0.2"
                    
#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 CSharpEssentials.Errors@3.0.2
                    
#: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=CSharpEssentials.Errors&version=3.0.2
                    
Install as a Cake Addin
#tool nuget:?package=CSharpEssentials.Errors&version=3.0.2
                    
Install as a Cake Tool

CSharpEssentials.Errors

CSharpEssentials.Errors provides a structured, type-safe error handling system for .NET applications. It encapsulates errors as data rather than exceptions, promoting the Result pattern and making failure flows explicit and manageable.

🚀 Features

  • Structured Errors: Error struct with unique codes, descriptions, and types (NotFound, Validation, Conflict, etc.).
  • Rich Metadata: Attach additional context (ErrorMetadata) to errors.
  • Standard Error Types: predefined types like Failure, Unexpected, Validation, Conflict, NotFound, Unauthorized, Forbidden.
  • Exception Integration: Convert exceptions to Error objects or wrap Error objects in DomainException.

📦 Installation

dotnet add package CSharpEssentials.Errors

🛠 Usage

1. Creating Errors

Use the static factory methods on the Error struct to create standard errors.

using CSharpEssentials.Errors;

// Common error types
var notFound = Error.NotFound("User.NotFound", "The user with the specified ID was not found.");
var validation = Error.Validation("User.EmailInvalid", "The email address is in an invalid format.");
var conflict = Error.Conflict("User.AlreadyExists", "A user with this email already exists.");
var failure = Error.Failure("Payment.Failed", "The payment gateway rejected the transaction.");

2. Error Metadata

You can attach extra information to an error for debugging or client responses.

var metadata = new ErrorMetadata 
{
    { "RetryCount", 3 },
    { "Gateway", "Stripe" }
};

var error = Error.Failure(
    code: "Payment.Failed", 
    description: "Transaction failed", 
    metadata: metadata
);

3. Converting Exceptions to Errors

Capture exception details into a structured error.

try 
{
    // ... risky code ...
}
catch (Exception ex)
{
    var error = Error.Exception(
        code: "System.Unhandled", 
        exception: ex
    );
    // error.Description will be ex.Message
    // error.Metadata will contain exception details
}

4. Domain Exceptions

If you must throw an exception but want to keep the structured error format (e.g., to be caught by a global handler), use DomainException.

using CSharpEssentials.Exceptions;

public void DoSomething()
{
    var error = Error.Validation("Input.Invalid", "Value cannot be negative");
    throw new DomainException(error);
}

5. Error Equality

Errors are value types and check for equality based on their content (Code, Description, Type, Metadata).

var e1 = Error.NotFound("Item.Missing");
var e2 = Error.NotFound("Item.Missing");

bool areEqual = (e1 == e2); // True
Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (6)

Showing the top 5 NuGet packages that depend on CSharpEssentials.Errors:

Package Downloads
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, advanced Swagger/OpenAPI configuration, 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.

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.

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
3.0.5 25 5/7/2026
3.0.4 133 5/6/2026
3.0.3 180 5/5/2026
3.0.2 232 5/5/2026
3.0.1 315 5/3/2026
3.0.0 297 5/3/2026
2.1.0 376 11/26/2025
2.0.9 315 9/30/2025
2.0.8 304 9/29/2025
2.0.7 292 9/29/2025
2.0.6 316 9/29/2025
2.0.5 302 9/29/2025
2.0.4 326 9/28/2025
2.0.3 308 9/28/2025
2.0.2 308 9/28/2025
2.0.1 316 9/28/2025
2.0.0 313 9/28/2025