Class ResultCatchExtensions

Namespace
REslava.Result.Extensions
Assembly
REslava.Result.dll

Extension methods for inline typed exception handling on Task<TResult> result pipelines.

public static class ResultCatchExtensions
Inheritance
ResultCatchExtensions
Inherited Members

Methods

CatchAsync<T, TException>(Task<Result<T>>, Func<TException, Task<IError>>, CancellationToken)

Handles a specific exception type in a Task<TResult> result pipeline using an async handler.

public static Task<Result<T>> CatchAsync<T, TException>(this Task<Result<T>> resultTask, Func<TException, Task<IError>> handler, CancellationToken cancellationToken = default) where TException : Exception

Parameters

resultTask Task<Result<T>>

The result task to await.

handler Func<TException, Task<IError>>

Async function that maps the caught exception to a replacement IError.

cancellationToken CancellationToken

Optional cancellation token.

Returns

Task<Result<T>>

The original result if successful or no matching exception is found; otherwise a failed result with the exception converted to the handler's error.

Type Parameters

T

The result value type.

TException

The exception type to handle. Subclasses are matched.

Catch<T, TException>(Task<Result<T>>, Func<TException, IError>)

Handles a specific exception type in a Task<TResult> result pipeline. Covers two cases:

  • The task throws TException directly — the exception is caught and converted.
  • The task returns a failed result containing an ExceptionError wrapping TException — the error is replaced.
public static Task<Result<T>> Catch<T, TException>(this Task<Result<T>> resultTask, Func<TException, IError> handler) where TException : Exception

Parameters

resultTask Task<Result<T>>

The result task to await.

handler Func<TException, IError>

Maps the caught exception to a replacement IError.

Returns

Task<Result<T>>

The original result if successful or no matching exception is found; otherwise a failed result with the exception converted to the handler's error.

Type Parameters

T

The result value type.

TException

The exception type to handle. Subclasses are matched.

Examples

Result<User> result = await service.GetUserAsync(id)
    .Catch<User, HttpRequestException>(ex => new NotFoundError("User", id));