Your own Result type¶
This page shows how to declare a Result<T> whose error is your own type, what the generator writes for it, and how it behaves.
Declaring the type¶
Give your error type a way to represent an unexpected exception, then mark a readonly partial struct:
using Fluent.Unions;
using Fluent.Unions.Abstractions;
namespace Shop;
public class DomainException(string message, Exception? inner = null)
: Exception(message, inner), IFromException<DomainException>
{
public static DomainException FromException(Exception exception) => exception switch
{
DomainException domain => domain,
ValidationError or ValidationErrors => new InvalidInputException(exception),
_ => new UnexpectedDomainException(exception)
};
}
public sealed class NotFoundException(string message) : DomainException(message);
public sealed class InvalidInputException(Exception validation) : DomainException("The input is invalid.", validation);
public sealed class UnexpectedDomainException(Exception inner) : DomainException("An unexpected error occurred.", inner);
[FluentResult<DomainException>]
public readonly partial struct Result<T>;
Your methods now return Result<T> from Shop. Only a DomainException can be an error:
static Result<Customer> GetCustomer(Guid id) =>
customers.TryGetValue(id, out var customer) ? customer : new NotFoundException($"No customer {id}.");
If the error type cannot implement IFromException<TError>, declare the mapping on the struct instead. It must be a public or internal static method with a single Exception parameter that returns the error type:
[FluentResult<PaymentException>]
public readonly partial struct PaymentResult<T>
{
public static PaymentException FromException(Exception exception) => new PaymentException("Unexpected failure.", exception);
}
Choosing the shape of the error¶
The error type can take three shapes, from open to closed:
| Shape | catch at a boundary |
Exhaustive switch |
A new error needs |
|---|---|---|---|
| A base exception, as above | catch (DomainException) |
no, a switch needs a fallback arm |
a subclass |
| A base exception and a union of its cases | catch (ShopException) |
yes | a subclass and a case in the union |
| Errors as data: a union of records | no, Check() throws a ResultErrorException |
yes | a case in the union |
Start with a base exception. It is the smallest step from the default Result<T>, keeps working in code that throws, and lets you add errors without touching every switch. Move to a union when the list of errors is closed and the compiler should make every caller handle each one.
A C# union is always a struct, and only an Exception can be thrown, so a union cannot be an exception itself. Its cases can be.
A base exception and a union of its cases¶
Derive every case from one abstract exception, and list the cases in a union that is the error type:
public abstract class ShopException(string message, Exception? inner = null) : Exception(message, inner);
public sealed class NotFoundException(string message) : ShopException(message);
public sealed class InvalidInputException(Exception validation) : ShopException("The input is invalid.", validation);
public sealed class UnexpectedShopException(Exception inner) : ShopException("An unexpected error occurred.", inner);
public union ShopError(NotFoundException, InvalidInputException, UnexpectedShopException) : IFromException<ShopError>
{
public static ShopError FromException(Exception exception) => exception switch
{
NotFoundException notFound => notFound,
InvalidInputException invalid => invalid,
UnexpectedShopException unexpected => unexpected,
ValidationError or ValidationErrors => new InvalidInputException(exception),
_ => new UnexpectedShopException(exception)
};
}
[FluentResult<ShopError>]
public readonly partial struct Result<T>;
You get the strengths of both shapes:
- A
catchof the base works.Check()rethrows the case itself, socatch (ShopException e)at a boundary catches every case, with its stack trace. - One handler for every case.
.Catch((ShopException e) => ...)handles all cases inside a chain, because the typedCatchlooks inside the union. - The compiler checks the cases.
result.GetError() switch { NotFoundException => ..., InvalidInputException => ..., UnexpectedShopException => ... }needs no fallback arm, and a new case fails everyswitchthat does not handle it. - A thrown case stays itself. A
NotFoundExceptionthrown inside a step becomes theNotFoundExceptioncase, not an unexpected error. - Shared members live on the base.
Message, or aCodeyou add, is available without aswitch.
The base class is open and the union is closed, so they can drift apart. A subclass of ShopException that is not a case of ShopError still compiles, and when it is thrown, FromException turns it into UnexpectedShopException. With Capture = ExceptionCapture.Declared, it escapes instead. Keep them in sync:
- Make the base
abstract, seal every case, and keep them in one file next to the union. - List every case in
FromException. C# has no conversion from the base class to the union, so each case needs its own arm. - Guard the list with a test:
[Test]
public void EveryShopExceptionIsACaseOfShopError()
{
var cases = UnionCaseNames.For(typeof(ShopError)).CaseTypes;
var exceptions = typeof(ShopException).Assembly.GetTypes()
.Where(type => type.IsSubclassOf(typeof(ShopException)) && !type.IsAbstract);
Assert.That(exceptions, Is.SubsetOf(cases));
}
UnionCaseNames is in Fluent.Unions.Serialization.
What the generator writes¶
| Part | Members |
|---|---|
| The union | Constructors for the value and the error, implicit conversions, Value, IsSuccess, IsError, TryGetValue, TryGetError, GetValue, GetError, MapError, Deconstruct, ToString, Ok(value), Error(error), and an implicit conversion to Result that drops the value |
Result without a value |
The same members without GetValue and Deconstruct, with the static property Ok as the success case, plus Result.From, Try, TryAsync, WhenAll, Retry, RetryAsync, CombineAll and CombineAllAsync |
| Operators | Every operator of the default type: Select, Chain, Ensure, Validate, Zip, ChainAppend, Do, OnErrorDo, Tap, TapError, TapAwait, Finally, Out, Catch, MapError, Match, GetValueOrDefault, Check, Combine, CombineAll, Partition, Values, Errors, ToOption, ToLoadable, Transpose, Unwrap, WithValue, DiscardValue, ValidateAnnotations, Timeout, RecordResult, query syntax, every Task overload and every ...Await operator |
Type inference works as with the default type, so no operator needs explicit type arguments:
Result<string> label = await GetCustomerAsync(id)
.Select(customer => customer.Name)
.Ensure(name => name.Length > 0, "A customer needs a name.")
.ChainAwait(LoadLabelAsync);
The operators are one-line forwarders to the library. Behaviour lives in one place, so your type and the default type cannot drift apart.
Options¶
| Property | Default | Effect |
|---|---|---|
CaseConversions |
true |
When the error type is a union, each case converts straight to the result. See Errors as data. |
Capture |
ExceptionCapture.All |
Which exceptions thrown inside an operator become an error. See Capture policy. |
Propagate |
none | Exception types that operators never capture. See Capture policy. |
The value-less companion¶
The generator writes Result without a value next to Result<T>. To add members to it, declare it yourself with the same attribute and the same error type:
[FluentResult<DomainException>]
public readonly partial struct Result<T>;
[FluentResult<DomainException>]
public readonly partial struct Result
{
public static Result NotImplemented => Error(new DomainException("Not implemented."));
}
A [FluentResult] struct without a type parameter needs the generic one next to it, with the same name, namespace and error type. Without it, FLU0101 is reported.
Names of the generated classes¶
The operators of Result<T> are extension methods in static classes named {TypeName}{Group}Operators, such as ResultOperators, ResultQueryOperators and ResultTaskOperators. They sit in the namespace of your type. A class of your own with another name never collides with them.
What ToString returns¶
| Case | Text |
|---|---|
| A value | value.ToString(), or an empty string for a value that prints null |
| An error that is an exception | TypeName: Message, without stack trace and without inner exceptions |
| An error that is not an exception | error.ToString() |
Ok of the result without a value |
Ok |
A default result |
The text of the error that FromException makes of the "holds neither a value nor an error" exception |
The text is safe to log or to return in a response. An error union with an exception case prints that exception.
Unexpected exceptions¶
- An exception thrown inside a step that already is a
DomainExceptionis kept as it is. - Any other exception goes through
FromException. That includes theValidationErrorfromEnsure(predicate, "message")and theValidationErrorsfromValidate, soFromExceptionis the place to map them to your own validation type. - An
OperationCanceledExceptionis never turned into an error. It propagates. - With a capture policy, exceptions that the policy excludes propagate as well.
- A
defaultresult counts as an error:FromExceptionreceives anInvalidOperationExceptionthat says the result is empty. Native pattern matching sees no case, as with the default type; see The default result.
FromException must not throw and must not return null. Test it with an exception that is not yours. When it returns null, the operator throws an InvalidOperationException with the original exception as InnerException. When it throws, the operator throws an InvalidOperationException whose InnerException is an AggregateException of the original exception and the failure of FromException. Analyzer FLU0009 reports a FromException that throws or returns null.
Capture policy¶
By default every exception thrown inside an operator becomes an error. That keeps a chain from throwing, but a bug such as a NullReferenceException then looks like any other failure. Two options on the attribute let such exceptions escape instead:
[FluentResult<DomainException>(Capture = ExceptionCapture.Declared)]
public readonly partial struct Result<T>;
[FluentResult<DomainException>(Propagate = [typeof(NullReferenceException), typeof(ArgumentException)])]
public readonly partial struct Result<T>;
| Setting | Captured | Propagates |
|---|---|---|
| default | every exception | OperationCanceledException |
Capture = ExceptionCapture.Declared |
the error type and its subtypes, and each case of an error union that is an exception | everything else |
Propagate = [...] |
every other exception | the listed types and their subtypes |
The two combine: Declared with Propagate captures declared exceptions that are not listed. An error union whose cases are all data, such as records, declares no exception, so Declared captures nothing and every thrown exception escapes.
An exception that is not captured is never caught. It propagates from the operator with its original stack trace, and an asynchronous operator returns a faulted task. Errors that the library creates itself, such as the ValidationError from Ensure(predicate, "message") or the error of a default result, are not thrown and still go through FromException.
Use Declared when your error type lists every failure you expect, so that anything else is a bug that should crash loudly. Use Propagate when you capture almost everything but want certain programming errors to escape.
The setting applies to every operator, to Try, TryAsync, Retry and RetryAsync, and to the MediatR, Mediator, Polly and FluentValidation integrations. A declared companion without a value must use the same settings as Result<T>, otherwise FLU0101 is reported. Declared on a type whose error type is Exception captures everything anyway and gives warning FLU0104. A type in Propagate that is not an exception gives error FLU0105.
Give UnexpectedDomainException a fixed message and keep the original as InnerException. Responses and logs often show the message of the error, and the message of an unexpected exception can hold connection strings or other internals. The ASP.NET Core integration also recognizes a ValidationError or ValidationErrors as the inner exception, as in InvalidInputException, and still writes a validation problem.
Namespaces¶
Declare the type in your root namespace, such as Shop. C# looks for a type in the enclosing namespaces before it looks at using directives, so code in Shop and in every namespace below it uses your Result<T>, even with using Fluent.Unions;.
A file in another root namespace that imports both Shop and Fluent.Unions sees two Result<T> types and reports CS0104. Write Shop.Result<T> there, or keep Fluent.Unions out of that file.
Performance¶
Generated types store the value and the error in fields instead of an object, so value types are not boxed. The operators take the struct directly and catch exceptions without closures:
| Operator call | Hand-written type on IResult (0.3.0) |
Generated type |
|---|---|---|
Create a Result<int> |
24 B | 0 B |
Chain |
56 B | 0 B |
Select on an int |
80 B | 0 B |
Ensure |
152 B | 0 B |
Match |
24 B | 0 B |
The default Result<T> uses the same layout and operators. See bench/results in the repo.
Diagnostics¶
FLU0101, FLU0102 and FLU0105 are errors, and the generator writes nothing for the type until they are fixed.
| Rule | Meaning |
|---|---|
FLU0101 |
The declaration is not supported. The message names the reason: not a readonly partial struct, a record struct, a ref struct, a file type, a nested type, more than one type parameter, a constrained type parameter or one that allows ref structs, an error type that is less accessible than the result, a name that the generator uses for a member, a value-less struct without a generic one next to it, or a mismatch between Result<T> and its value-less companion. |
FLU0102 |
The error type cannot represent an unexpected exception: implement IFromException<TError> for the error type itself, or declare a public or internal static TError FromException(Exception exception) on the struct. |
FLU0103 |
Info: a case of the error union gets no conversion to the result. The message names the reason: an interface, object or ValueType, a type that is neither a class nor a struct, or a case that is less accessible than the result. |
FLU0104 |
Warning: Capture = ExceptionCapture.Declared has no effect, because the error type is Exception. |
FLU0105 |
A type in Propagate is not an exception. |
FLU0004 |
Result<X> where X is the error type or one of its cases: a value of X would become an error. |
The other analyzers, such as FLU0001 for an ignored result and FLU0010 for an unchecked GetValue, treat your type like the default one.
Integration packages¶
The integration packages accept your type wherever they accept the default one: ASP.NET Core maps it to HTTP responses, the assertion packages assert on it, Polly and FluentValidation work with it, Serilog and Verify write it, and every serializer refuses it like the default type. See each package's page for details.
Migrating from 0.3.0¶
0.3.0 had two ways to get your own error type. Both are gone, and [FluentResult<TError>] replaces them. Do not implement IResult<TSelf, T, TError> by hand: declared types implement it, and the integration packages accept any type that does.
From a hand-written union that implemented IResult<TSelf, T, TError>, with wrapper methods for Select and the Task overloads:
- Implement
IFromException<TError>on the error type, or keepFromExceptionon the struct. - Replace
public union Result<T>(T, DomainException) : IResult<...> { ... }with[FluentResult<DomainException>] public readonly partial struct Result<T>;. - Delete the wrapper class. Every operator is generated now.
From Result<T, TError>:
- Declare a type for the error:
[FluentResult<CheckoutError>] public readonly partial struct Outcome<T>;. - Replace
Result<Order, CheckoutError>withOutcome<Order>. The(CheckoutError)casts on errors are no longer needed. MapErrornow names the target type:outcome.MapError<ClientResult<Order>, ClientError>(...), see Errors as data.- Replace
Catch<TCase>on a record case with the generalCatch. The typedCatch<TCase>takes exception cases only.
The generated type does not box and has the Task overloads.
Pitfalls¶
- A
FromExceptionthat returnsnullor throws breaks the rule that chains do not throw. - Mixing your
Result<T>andFluent.Unions.Result<T>in one file outside your namespace needs the namespace written out. Result<DomainException>is reported byFLU0004: aDomainExceptionvalue could not be told apart from an error.
See also¶
- Errors as data: a union as the error type, checked by an exhaustive
switch. - Error mapping: map your error types to HTTP status codes.
test/Fluent.Unions.Tests/OwnResultin the repo: a generated type with tests for every operator family.
Back to: Errors ยท Next: Errors as data