Repository navigation
OnixLabs.Core.Failure T
Represents a failed generic result that carries the underlying exception.
| Namespace | OnixLabs.Core |
| Assembly | OnixLabs.Core.dll (NuGet) |
| Inheritance |
object → Result<T> → Failure<T>
|
public sealed class Failure<T> : Result<T>| Parameter | Description |
|---|---|
T |
The type of the value that would have been carried had the operation succeeded. |
Failure<T> has no public constructor. Obtain an instance via Result<T>.Failure(Exception) or the implicit conversion from Exception to Result<T>.
Failure<T> is the failure rail of the railway-oriented Result<T> pattern. It is one of exactly two sealed derivations of the closed base class Result<T> (the other being Success<T>), so a C# 15 switch over Success<T> and Failure<T> is exhaustive without a discard arm.
Key behaviours:
-
Construction. The constructor is
internal; instances can only be created byResult<T>.Failure(Exception exception)or the implicit conversionException → Result<T>. -
Exception access. The
Exceptionproperty exposes the captured exception directly. For safe, fluent access use base-class accessors such asGetExceptionOrDefault()andGetExceptionOrThrow(). -
Re-typing.
ToTypedResult<TResult>()re-wraps the same exception in aFailure<TResult>, andToUntypedResult()converts it to the non-genericFailure. Both are useful when propagating failures across type boundaries inside combinators. -
Short-circuit. Every combinator on
Result<T>(Select,SelectMany,Match, and their async variants) short-circuits immediately when the instance is aFailure<T>, passing the original failure through without re-evaluating any delegate. -
Disposal.
Dispose()andDisposeAsync()are no-ops for aFailure<T>; there is no value to dispose. -
Equality. Two
Failure<T>instances are equal when theirExceptionreferences are equal (reference equality viaobject.Equals). AFailure<T>is never equal to aSuccess<T>. -
ToString. Returns
Exception.Message. -
Rethrow. Call
Throw()orGetValueOrThrow()to leave the railway and rethrow the captured exception with its original stack trace preserved viaExceptionDispatchInfo.
| Property | Type | Description |
|---|---|---|
Exception |
Exception |
Gets the underlying exception representing the failed result. |
public Exception Exception { get; }Gets the underlying exception representing the failed result. Set once at construction and never null.
| Method | Returns | Description |
|---|---|---|
ToTypedResult<TResult>() |
Failure<TResult> |
Re-types this failure to carry the same exception under a different result type. |
ToUntypedResult() |
Failure |
Converts this failure to a non-generic Failure carrying the same exception. |
public Failure<TResult> ToTypedResult<TResult>()Obtains a new Failure<TResult> containing the current Exception, re-typing a failure from one value type to another without wrapping or re-throwing.
| Type Parameter | Description |
|---|---|
TResult |
The underlying type of the new Failure<TResult>. |
Returns — A new Failure<TResult> carrying Exception.
public Failure ToUntypedResult()Obtains a new non-generic Failure containing the current Exception.
Returns — A new Failure carrying Exception.
Failure<T> inherits all public members of Result<T>, including:
- Static factories:
Of,OfAsync,Success,Failure - Properties:
IsSuccess(alwaysfalse),IsFailure(alwaystrue) - Exception accessors:
GetExceptionOrDefault(),GetExceptionOrNone(),GetExceptionOrThrow() - Value accessors:
GetValueOrDefault()(returnsdefault),GetValueOrThrow()(rethrows) - Combinators:
Match,MatchAsync,Select,SelectAsync,SelectMany,SelectManyAsync - Operators:
==,!=,implicit T → Result<T>,implicit Exception → Result<T>,explicit Result<T> → T -
Dispose(),DisposeAsync(),Throw(),Equals(),GetHashCode(),ToString()
using OnixLabs.Core;
// Via static factory
Result<int> a = Result<int>.Failure(new InvalidOperationException("oops"));
// Via implicit conversion from Exception
Exception ex = new FormatException("bad input");
Result<int> b = ex;
// Inspect the failure
if (b is Failure<int> f)
Console.WriteLine(f.Exception.Message); // "bad input"
// Combinators short-circuit on failure
Result<string> c = b.Select(v => v.ToString()); // still a Failure<string>// Inside a combinator that needs to propagate a failure as a different type:
Failure<Order> orderFailure = someFailure.ToTypedResult<Order>();
// Or discard the type parameter altogether:
Failure untypedFailure = someFailure.ToUntypedResult();Result<int> result = Result<int>.Of(() => int.Parse("abc"));
// Option 1 — leave the railway explicitly
result.Throw();
// Option 2 — extract the value (throws on failure)
int value = result.GetValueOrThrow();
// Option 3 — explicit cast (equivalent to GetValueOrThrow)
int value2 = (int)result;ONIXLabs .NET Library · Repository · NuGet · License (Apache-2.0)
Copyright © ONIXLabs 2020. Licensed under the Apache License, Version 2.0.
- Enumeration<T>
- Optional<T>
- OptionalEqualityComparer<T>
- OptionalExtensions
- Result
- Result<T>
- ResultEqualityComparer<T>
- ResultExtensions
- Specification<T>
- Preconditions
- IValueEquatable<T>
- IValueComparable<T>
- IBinaryConvertible
- ISpanBinaryConvertible
- IMemoryBinaryConvertible
- ArrayExtensions
- ComparableExtensions
- DateTimeExtensions
- ISpanParsableExtensions
- ObjectExtensions
- RandomExtensions
- ReadOnlySequenceExtensions
- ReadOnlySpanExtensions
- StringExtensions
- Base16
- Base32
- Base58
- Base64
- IBaseCodec
- IBaseValue
- IBaseValue<T>
- Base16Codec
- Base32Codec
- Base58Codec
- Base64Codec
- Base16FormatProvider
- Base32FormatProvider
- Base58FormatProvider
- Base64FormatProvider
- EncodingExtensions
- ReadOnlySpanExtensions
- StringBuilderExtensions
- BigDecimal
- BigDecimalEqualityComparer
- BigDecimalOrdinalityComparer
- NumberInfo
- NumberInfoEqualityComparer
- NumberInfoOrdinalityComparer
- Int256
- Int512
- UInt256
- UInt512
- Float128
- Float256
- GenericMath
- NumericsExtensions
- ConversionMode
- ScaleMode
- Hash
- NamedHash
- Sha3
- Sha3Hash224
- Sha3Hash256
- Sha3Hash384
- Sha3Hash512
- Sha3Shake128
- Sha3Shake256
- MerkleTree
- MerkleTree<T>
- PrivateKey
- PublicKey
- NamedPrivateKey
- NamedPublicKey
- EcdhPrivateKey
- EcdhPublicKey
- EcdsaPrivateKey
- EcdsaPublicKey
- EddsaPrivateKey
- EddsaPublicKey
- RsaPrivateKey
- RsaPublicKey
- DigitalSignature
- DigitalSignatureAndPublicKey
- Salt
- Secret
- ICryptoPrimitive<T>
- IHashable
- IEcdhPrivateKey
- IEcdsaPrivateKey
- IEddsaPrivateKey
- IRsaPrivateKey
- IPrivateKeyDerivable<T>
- IPrivateKeyExportable
- IPrivateKeyImportable<T>
- IPrivateKeyPemImportable<T>
- IPrivateKeyPkcs8Exportable
- IPrivateKeyPkcs8Importable<T>
- IPrivateKeyPkcs8PemExportable
- IPrivateKeyRawExportable
- IPrivateKeyRawImportable<T>
- IPrivateKeyRawPemExportable
- IEcdhPublicKey
- IEcdsaPublicKey
- IEddsaPublicKey
- IRsaPublicKey
- IPublicKeyExportable
- IPublicKeyImportable<T>
- IPublicKeyPemExportable
- IPublicKeyPemImportable<T>
- IPublicKeyRawExportable
- IPublicKeyRawImportable<T>
- HashAlgorithmExtensions
- ReadOnlySpanExtensions