Repository navigation
OnixLabs.Core.Failure
Represents a failed, non-generic result that carries the underlying exception.
| Namespace | OnixLabs.Core |
| Assembly | OnixLabs.Core.dll (NuGet) |
| Inheritance |
object → Result → Failure
|
| Implements | IValueEquatable<Result> |
public sealed class Failure : ResultFailure has no public constructor; obtain an instance via Result.Failure(Exception) or the implicit conversion from Exception.
Failure is one of exactly two sealed derivations of Result. The other is Success. Because Result is a closed class, a C# 15 switch over Success and Failure is exhaustive without a discard arm.
Failure represents the outcome of an operation that threw an exception. Rather than propagating the exception up the call stack, Result.Of and Result.OfAsync catch it and wrap it in a Failure, making the error an explicit, inspectable value.
Key behaviours:
-
Short-circuiting. Once a
Resultis aFailure, every combinator —Select,SelectMany, and their async variants — passes theFailurethrough without executing its delegate. The captured exception is never lost or re-evaluated. -
Equality. Two
Failureinstances are equal when theirExceptionreferences are the same object.Result.GetHashCodereturnsException.GetHashCode(). -
ToString.Result.ToStringreturnsException.Messagefor aFailure. -
Implicit conversion from
Exception. AnyExceptionis implicitly convertible to aFailureviaimplicit operator Result(Exception), which is why combinatorcatchblocks can writereturn exception;. -
Re-throwing.
Result.Throwrethrows the captured exception with its original stack trace preserved viaExceptionDispatchInfo.
Failure inherits the full combinator and exception-accessor API from Result. See the Result page for the complete inherited member list.
To promote an untyped Failure to a typed Failure<TResult>, use ToTypedResult<TResult>().
using OnixLabs.Core;
// Via the factory method.
Failure failure = Result.Failure(new InvalidOperationException("Something went wrong."));
// Via Result.Of — returns Failure when the action throws.
Result result = Result.Of(() => throw new IOException("Disk full."));
Console.WriteLine(result.IsFailure); // True
// Via implicit conversion from Exception.
Result implicit = new ArgumentNullException(nameof(implicit));
Console.WriteLine(implicit.IsFailure); // Trueusing OnixLabs.Core;
Result result = Result.Of(() => throw new TimeoutException("Timed out."));
if (result is Failure f)
Console.WriteLine(f.Exception.Message); // Timed out.
// Or via the base-class accessor:
Exception? ex = result.GetExceptionOrDefault();
Console.WriteLine(ex?.Message); // Timed out.using OnixLabs.Core;
Failure untyped = Result.Failure(new Exception("Oops."));
Failure<int> typed = untyped.ToTypedResult<int>();
Console.WriteLine(typed.IsFailure); // True
Console.WriteLine(typed.Exception.Message); // Oops.| Property | Type | Description |
|---|---|---|
Exception |
Exception |
Gets the underlying exception representing the failed result. |
public Exception Exception { get; }Gets the underlying exception that was captured when this Failure was created. This is the same Exception reference that Result.GetExceptionOrDefault(), Result.GetExceptionOrThrow(), and Result.Throw() operate on.
| Method | Returns | Description |
|---|---|---|
ToTypedResult<TResult>() |
Failure<TResult> |
Promotes this failure to a typed failure carrying the same exception. |
public Failure<TResult> ToTypedResult<TResult>()Obtains a new Failure<TResult> containing the current Exception, promoting an untyped failure to a typed one. This is the bridge from the non-generic result model to the generic Result<T> model.
Returns — A new Failure<TResult> carrying Exception.
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