Table of Contents

Class GetValueOrThrowExtensions

Namespace
Trellis
Assembly
Trellis.Core.dll

Production-safe throwing terminal extraction for Result<TValue>, intended for the narrow case where a failure represents a broken invariant the caller cannot act on — typically the persistence DTO → entity rehydration seam, where write-path validation guarantees the row is valid and any TryCreate failure on read indicates database corruption or migration drift.

public static class GetValueOrThrowExtensions
Inheritance
GetValueOrThrowExtensions
Inherited Members

Remarks

Mirrors GetValueOrThrow(string?) for symmetric ergonomics across the two ROP types: an explicit, named "this throws" contract gated by the verb in the method name, not an ambient property accessor (Result<T>.Value was removed in v3 because the ambient accessor was the primary cause of unsafe value access).

Prefer TryGetValue, Match, Bind, GetValueOrDefault, or destructuring for normal Result-track flow. GetValueOrThrow<TValue>(Result<TValue>, string?) is the escape hatch for trust-boundary crossings where failure means "the world is not as expected" — not for request validation where the caller should surface a typed Error.InvalidInput on the Result track.

Cookbook Recipe 30 covers the persistence-rehydration use case end to end, including the decision criteria for picking this throwing pattern versus a Result<TEntity> end-to-end shape for legacy / corruptible data paths.

Methods

GetValueOrThrowAsync<TValue>(Task<Result<TValue>>, string?)

Awaits the task and returns the success value, or throws InvalidOperationException if the result is a failure. This is a terminal operator that exits the Result railway.

public static Task<TValue> GetValueOrThrowAsync<TValue>(this Task<Result<TValue>> resultTask, string? errorMessage = null)

Parameters

resultTask Task<Result<TValue>>

The task producing the result to extract a value from.

errorMessage string

Optional custom exception message used when the result is a failure. When null, a default message is synthesized from typeof(TValue).Name, Code, and GetDisplayMessage().

Returns

Task<TValue>

The success value.

Type Parameters

TValue

The type of the value in the Result.

Exceptions

ArgumentNullException

Thrown when resultTask is null.

InvalidOperationException

Thrown when the awaited result is a failure.

GetValueOrThrowAsync<TValue>(ValueTask<Result<TValue>>, string?)

Awaits the value-task and returns the success value, or throws InvalidOperationException if the result is a failure. This is a terminal operator that exits the Result railway.

public static ValueTask<TValue> GetValueOrThrowAsync<TValue>(this ValueTask<Result<TValue>> resultTask, string? errorMessage = null)

Parameters

resultTask ValueTask<Result<TValue>>

The value-task producing the result to extract a value from.

errorMessage string

Optional custom exception message used when the result is a failure. When null, a default message is synthesized from typeof(TValue).Name, Code, and GetDisplayMessage().

Returns

ValueTask<TValue>

The success value.

Type Parameters

TValue

The type of the value in the Result.

Exceptions

InvalidOperationException

Thrown when the awaited result is a failure.

GetValueOrThrow<TValue>(Result<TValue>, string?)

Returns the success value, or throws InvalidOperationException if the result is a failure. This is a terminal operator that exits the Result railway.

public static TValue GetValueOrThrow<TValue>(this Result<TValue> result, string? errorMessage = null)

Parameters

result Result<TValue>

The result to extract a value from.

errorMessage string

Optional custom exception message used when the result is a failure. When null, a default message is synthesized from typeof(TValue).Name, Code, and GetDisplayMessage().

Returns

TValue

The success value.

Type Parameters

TValue

The type of the value in the Result.

Exceptions

InvalidOperationException

Thrown when result is a failure.