Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Digillect.FP.Types/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,23 @@ public async Task<Result<TResult>> MapAsync<TResult>(Func<T, Task<TResult>> map)
return Result.Success(result);
}

/// <summary>
/// Converts the error of the current result into a new error using the specified mapping function.
/// </summary>
/// <param name="mapError">A function to transform the current error into a new error.</param>
/// <returns>A result with the current value and the transformed error.</returns>
public Result<T> MapError(Func<Error, Error> mapError)
{
if (_error is null)
{
return this;
}

var error = mapError(_error);

return Result.Error<T>(error);
}

/// <summary>
/// Chains the successful result to another operation that returns a <see cref="Result{TResult}"/> using the provided binding function.
/// </summary>
Expand Down
15 changes: 15 additions & 0 deletions src/Digillect.FP.Types/TaskResultExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,21 @@ public static async Task<Result<TResult>> Map<T, TResult>(this Task<Result<T>> r
return awaitedResult.Map(map);
}

/// <summary>
/// Maps an error from a failed <see cref="Result{T}"/> into a new <see cref="Result{T}"/> with the transformed error value.
/// </summary>
/// <typeparam name="T">The type of the value contained in the original result.</typeparam>
/// <param name="result">The original result to map the error from.</param>
/// <param name="mapError">A function to transform the error from the original result to the new error type.</param>
/// <returns>
/// A new <see cref="Result{T}"/> that contains either the original success value or the transformed error.
/// </returns>
public static async Task<Result<T>> MapError<T>(this Task<Result<T>> result, Func<Error, Error> mapError)
{
var awaitedResult = await result.ConfigureAwait(false);
return awaitedResult.MapError(mapError);
}

/// <summary>
/// Transforms the success value of the current asynchronous <see cref="Result{T}"/> instance using the specified
/// asynchronous mapping function <paramref name="map"/>.
Expand Down