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
resultTaskTask<Result<TValue>>The task producing the result to extract a value from.
errorMessagestringOptional custom exception message used when the result is a failure. When null, a default message is synthesized from
typeof(, Code, and GetDisplayMessage().TValue).Name
Returns
- Task<TValue>
The success value.
Type Parameters
TValueThe type of the value in the Result.
Exceptions
- ArgumentNullException
Thrown when
resultTaskis 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
resultTaskValueTask<Result<TValue>>The value-task producing the result to extract a value from.
errorMessagestringOptional custom exception message used when the result is a failure. When null, a default message is synthesized from
typeof(, Code, and GetDisplayMessage().TValue).Name
Returns
- ValueTask<TValue>
The success value.
Type Parameters
TValueThe 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
resultResult<TValue>The result to extract a value from.
errorMessagestringOptional custom exception message used when the result is a failure. When null, a default message is synthesized from
typeof(, Code, and GetDisplayMessage().TValue).Name
Returns
- TValue
The success value.
Type Parameters
TValueThe type of the value in the Result.
Exceptions
- InvalidOperationException
Thrown when
resultis a failure.