ErrorOrX.Generators 2.5.0

dotnet add package ErrorOrX.Generators --version 2.5.0
                    
NuGet\Install-Package ErrorOrX.Generators -Version 2.5.0
                    
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="ErrorOrX.Generators" Version="2.5.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ErrorOrX.Generators" Version="2.5.0" />
                    
Directory.Packages.props
<PackageReference Include="ErrorOrX.Generators">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
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 ErrorOrX.Generators --version 2.5.0
                    
#r "nuget: ErrorOrX.Generators, 2.5.0"
                    
#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 ErrorOrX.Generators@2.5.0
                    
#: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=ErrorOrX.Generators&version=2.5.0
                    
Install as a Cake Addin
#tool nuget:?package=ErrorOrX.Generators&version=2.5.0
                    
Install as a Cake Tool

ErrorOrX

NuGet NuGet Downloads License: MIT

Discriminated unions for .NET with source-generated ASP.NET Core Minimal API integration. Zero boilerplate, full AOT support.

Installation

dotnet add package ErrorOrX.Generators

Quick Start

// Program.cs
var app = WebApplication.CreateSlimBuilder(args).Build();
app.MapErrorOrEndpoints();
app.Run();
// TodoApi.cs
using ErrorOr;

public static class TodoApi
{
    [Get("/todos/{id}")]
    public static ErrorOr<Todo> GetById(int id, ITodoService svc)
        => svc.GetById(id).OrNotFound($"Todo {id} not found");

    [Post("/todos")]
    public static ErrorOr<Todo> Create(CreateTodoRequest req, ITodoService svc)
        => svc.Create(req);  // 201 Created

    [Delete("/todos/{id}")]
    public static ErrorOr<Deleted> Delete(int id, ITodoService svc)
        => svc.Delete(id) ? Result.Deleted : Error.NotFound();
}

Nullable-to-ErrorOr Extensions

Convert nullable values to ErrorOr<T> with fluent extensions:

// Auto-generates error code from type name (e.g., "Todo.NotFound")
return _todos.Find(t => t.Id == id).OrNotFound($"Todo {id} not found");
return user.OrUnauthorized("Invalid credentials");
return record.OrValidation("Record is invalid");

// Custom errors
return value.OrError(Error.Custom(422, "Custom.Code", "Custom message"));
return value.OrError(() => BuildExpensiveError());
Extension Error Type HTTP Description
.OrNotFound() NotFound 404 Resource not found
.OrValidation() Validation 400 Input validation failed
.OrUnauthorized() Unauthorized 401 Authentication required
.OrForbidden() Forbidden 403 Insufficient permissions
.OrConflict() Conflict 409 State conflict
.OrFailure() Failure 500 Operational failure
.OrUnexpected() Unexpected 500 Unexpected error
.OrError(Error) Any Any Custom error
.OrError(Func) Any Any Lazy custom error

Error Types

Error.Validation("User.InvalidEmail", "Email format is invalid")
Error.NotFound("User.NotFound", "User does not exist")
Error.Conflict("User.Duplicate", "Email already registered")
Error.Unauthorized("Auth.InvalidToken", "Token has expired")
Error.Forbidden("Auth.InsufficientRole", "Admin role required")
Error.Failure("Db.ConnectionFailed", "Database unavailable")
Error.Unexpected("Unknown", "An unexpected error occurred")
Error.Custom(422, "Validation.Complex", "Complex validation failed")
Factory HTTP Use Case
Error.Validation() 400 Input/request validation
Error.Unauthorized() 401 Authentication required
Error.Forbidden() 403 Insufficient permissions
Error.NotFound() 404 Resource doesn't exist
Error.Conflict() 409 State conflict (duplicate)
Error.Failure() 500 Known operational failure
Error.Unexpected() 500 Unhandled/unknown error
Error.Custom() Any Custom HTTP status code

Fluent API

// Chain operations (railway-oriented programming)
var result = ValidateOrder(request)
    .Then(order => ProcessPayment(order))
    .Then(order => CreateShipment(order))
    .FailIf(order => order.Total <= 0, Error.Validation("Order.InvalidTotal", "Total must be positive"));

// Handle both cases
return result.Match(
    order => Ok(order),
    errors => BadRequest(errors.First().Description));

// Provide fallback on error
var user = GetUser(id).Else(errors => DefaultUser);

// Side effects
GetUser(id).Switch(
    user => Console.WriteLine($"Found: {user.Name}"),
    errors => Logger.LogError(errors.First().Description));

Result Markers

Result.Success   // 200 OK (no body)
Result.Created   // 201 Created (no body)
Result.Updated   // 204 No Content
Result.Deleted   // 204 No Content

Smart Parameter Binding

The generator automatically infers parameter sources:

[Post("/todos")]
public static ErrorOr<Todo> Create(
    CreateTodoRequest req,    // -> Body (POST + complex type)
    ITodoService svc)         // -> Service (interface)
    => svc.Create(req);

[Get("/todos/{id}")]
public static ErrorOr<Todo> GetById(
    int id,                   // -> Route (matches {id})
    ITodoService svc)         // -> Service
    => svc.GetById(id).OrNotFound();

Middleware Attributes

[Post("/admin")]
[Authorize("Admin")]
[EnableRateLimiting("fixed")]
[OutputCache(Duration = 60)]
public static ErrorOr<User> CreateAdmin(CreateUserRequest req) { }
// Generates: .RequireAuthorization("Admin").RequireRateLimiting("fixed").CacheOutput(...)

Native AOT

Fully compatible with PublishAot=true. The generator produces reflection-free code with automatic JSON serialization context generation.

<PropertyGroup>
  <PublishAot>true</PublishAot>
</PropertyGroup>

Documentation

License

MIT

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.5.0 31 1/14/2026
2.4.0 40 1/13/2026
2.3.1 38 1/13/2026
2.3.0 35 1/13/2026
2.2.1 41 1/12/2026