ErrorOrX

May 25, 2026 · View on GitHub

NuGet NuGet Downloads License: MIT

Railway-Oriented Programming for .NET with source-generated ASP.NET Core Minimal API integration. Zero boilerplate, full Native AOT support.

Features

  • Discriminated UnionsErrorOr<T> represents success or a list of typed errors
  • Fluent APIThen, Else, Match, Switch, FailIf
  • Nullable ExtensionsOrNotFound(), OrValidation(), …
  • Source Generator — Auto-generates MapErrorOrEndpoints() from attributed static methods
  • Smart Binding — Automatic parameter inference based on HTTP method and type
  • OpenAPI Ready — Typed Results<…> unions for complete API documentation
  • Native AOT — Reflection-free code generation with JSON serialization contexts
  • Middleware Pass-Through[Authorize], [EnableRateLimiting], [OutputCache], [EnableCors] → fluent calls
  • API Versioning — Integrates with Asp.Versioning.Http for versioned endpoint groups
  • 36 Analyzers — Real-time IDE feedback for route conflicts, binding errors, AOT compatibility

For the full mental model of what your code becomes after the generator runs, see CLAUDE.md.

Installation

dotnet add package ErrorOrX.Generators

Includes the ErrorOrX runtime as a transitive dependency.

Quick Start

// Program.cs
var app = WebApplication.CreateSlimBuilder(args).Build();
app.MapErrorOrEndpoints();
app.Run();
// TodoApi.cs
[Get("/api/todos/{id:guid}")]
public static Task<ErrorOr<Todo>> GetById(Guid id, ITodoService svc, CancellationToken ct)
    => svc.GetByIdAsync(id, ct);

Full working example: samples/ErrorOrX.Samples.Api/Program.cs + TodoApi.cs.

Error → HTTP Mapping

Errors map to RFC 9457 ProblemDetails:

ErrorTypeHTTPTypedResult
Validation400ValidationProblem with field errors
Unauthorized401UnauthorizedHttpResult
Forbidden403ForbidHttpResult
NotFound404NotFound<ProblemDetails>
Conflict409Conflict<ProblemDetails>
Failure500InternalServerError<ProblemDetails>
Unexpected500InternalServerError<ProblemDetails>
Custom(422)422UnprocessableEntity<ProblemDetails>

Source: ErrorMapping.cs.

Error Types

Error.Validation("User.InvalidEmail", "Email format is invalid")   // 400
Error.Unauthorized("Auth.InvalidToken", "Token has expired")       // 401
Error.Forbidden("Auth.InsufficientRole", "Admin role required")    // 403
Error.NotFound("User.NotFound", "User does not exist")             // 404
Error.Conflict("User.Duplicate", "Email already registered")       // 409
Error.Failure("Db.ConnectionFailed", "Database unavailable")       // 500
Error.Unexpected("Unknown", "An unexpected error occurred")        // 500
Error.Custom(422, "Validation.Complex", "Complex validation failed")

Nullable → ErrorOr<T> Extensions

Error code is auto-generated from the type name (e.g., Todo.NotFound).

ExtensionError TypeHTTPDescription
.OrNotFound()NotFound404Resource not found
.OrValidation()Validation400Input validation failed
.OrUnauthorized()Unauthorized401Authentication required
.OrForbidden()Forbidden403Insufficient permissions
.OrConflict()Conflict409State conflict
.OrFailure()Failure500Operational failure
.OrUnexpected()Unexpected500Unexpected error
.OrError(Error)AnyAnyCustom error
.OrError(Func)AnyAnyLazy custom error

Usage in context: Domain/TodoService.cs.

Fluent API

Chain operations railway-style — errors short-circuit the pipeline. Each operator has a worked endpoint in the sample:

OperatorSample
Then/ThenAsyncAdvancedErrorHandlingApi.cs:10-12
FailIfAdvancedErrorHandlingApi.cs:17-21 (single) · :41-45 (chained)
ElseAdvancedErrorHandlingApi.cs:25-26
MatchAdvancedErrorHandlingApi.cs:31-34
SwitchAdvancedErrorHandlingApi.cs:93-97

Result Markers

For endpoints without a response body:

Result.Success   // 200 OK
Result.Created   // 201 Created
Result.Updated   // 204 No Content
Result.Deleted   // 204 No Content

[ReturnsError] on Interfaces

Document possible errors on interface methods — the generator reads them when building the Results<…> union for OpenAPI. See Domain/ITodoService.cs for five attribute usages spanning Failure, NotFound, and Validation.

Middleware Attributes

Standard ASP.NET Core attributes on handlers are translated to Minimal API fluent calls. The generator also auto-adds 401/403 (for [Authorize]) and 429 (for [EnableRateLimiting]) ProducesResponseTypeMetadata so OpenAPI documents the failure modes:

AttributeEmitted fluent call
[Authorize(...)].RequireAuthorization(...)
[EnableRateLimiting].RequireRateLimiting(...)
[OutputCache].CacheOutput(p => p.Expire(TimeSpan.From...))
[EnableCors].RequireCors(...)

Four worked endpoints (one per attribute, including stacked combinations): samples/ErrorOrX.Samples.Api/AdminApi.cs. Service wiring (AddAuthorizationBuilder, AddRateLimiter, AddOutputCache, AddCors) lives in Program.cs.

Native AOT

Fully compatible with PublishAot=true. Define a JsonSerializerContext covering your request/response/problem types:

[JsonSerializable(typeof(Todo))]
[JsonSerializable(typeof(CreateTodoRequest))]
[JsonSerializable(typeof(ProblemDetails))]
[JsonSerializable(typeof(HttpValidationProblemDetails))]
internal sealed partial class AppJsonSerializerContext : JsonSerializerContext;

Wire it via the builder — WithCamelCase() / WithIgnoreNulls() override at runtime, or set them once on the context via [JsonSourceGenerationOptions]:

builder.Services.AddErrorOrEndpoints()
    .UseJsonContext<AppJsonSerializerContext>()
    .WithCamelCase()
    .WithIgnoreNulls();

Working setup: AppJsonSerializerContext.cs + Program.cs.

Analyzers (36 Diagnostics)

CategoryDiagnosticsExamples
CoreEOE001-006Invalid return type, non-static handler, unbound route param
BindingEOE008-021Multiple body sources, invalid [FromRoute] type, ambiguous binding
ResultsEOE022-024Too many result types, unknown error factory, undocumented interface
AOT/JSONEOE007, EOE025-026, EOE034-036Not AOT-serializable, missing camelCase, missing context, validation reflection
VersioningEOE027-031Version-neutral conflict, undeclared version, invalid format
NamingEOE032-033Duplicate route binding, non-PascalCase handler

Source: Descriptors.*.cs. Diagnostics-in-action walkthrough: samples/ErrorOrX.Samples.Diagnostics/README.md.

Packages

PackageTargetDescription
ErrorOrX.Generatorsnetstandard2.0Source generator (pulls in ErrorOrX)
ErrorOrXnet10.0Runtime library (auto-referenced)

Changelog

See CHANGELOG.md.