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
resultTaskTask<Result<T>>The result task to await.
handlerFunc<TException, Task<IError>>Async function that maps the caught exception to a replacement IError.
cancellationTokenCancellationTokenOptional 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
TThe result value type.
TExceptionThe 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
TExceptiondirectly — 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
resultTaskTask<Result<T>>The result task to await.
handlerFunc<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
TThe result value type.
TExceptionThe exception type to handle. Subclasses are matched.
Examples
Result<User> result = await service.GetUserAsync(id)
.Catch<User, HttpRequestException>(ex => new NotFoundError("User", id));