Skip to content

Overview

Haitch.Results provides Result, Result<T>, and Result<T, TError> types for representing operation outcomes without exceptions. It includes a structured Error model with categorised error types.

Targets .NET 10+.

Terminal window
dotnet add package Haitch.Results
using Haitch.Results;
Result<int> Parse(string input)
{
if (int.TryParse(input, out var value))
return value; // implicit conversion to success
return Error.Validation("parse.failed", $"'{input}' is not a valid integer.");
}
var result = Parse("42");
var message = result.Match(
onSuccess: v => $"Parsed: {v}",
onFailure: e => $"Error: {e.Message}"
);

Result types represent success or failure:

TypeDescription
ResultNo value on success. Carries an Error on failure.
Result<T>Carries a T on success, an Error on failure.
Result<T, TError>Carries a T on success, a custom TError on failure.

Error is a record with a machine-readable Code, a human-readable Message, an ErrorType category, and optional Metadata.

ErrorType categorises errors: Failure, Validation, NotFound, Conflict, Unauthorized, Forbidden, Unexpected.

All generic result types support implicit conversion from both success values and errors, so you can return values or errors directly without calling factory methods.

Result<User> GetUser(int id)
{
var user = db.Find(id);
if (user is null)
return Error.NotFound("user.not_found", $"No user with id {id}.");
return user; // implicit Success
}