Table of Contents

Class MapOnFailureExtensions

Namespace
FunctionalDdd
Assembly
FunctionalDdd.RailwayOrientedProgramming.dll

Provides extension methods for transforming errors in failed Results while leaving successful Results unchanged.

public static class MapOnFailureExtensions
Inheritance
MapOnFailureExtensions
Inherited Members

Examples

// Convert a generic error to a more specific one
var result = GetUser(id)
    .MapOnFailure(err => Error.NotFound($"User {id} not found", id));

// Add context to an error
var result = ValidateEmail(email)
    .MapOnFailure(err => Error.Validation($"Email validation failed: {err.Detail}", "email"));

// Async with CancellationToken using closure capture
var ct = cancellationToken;
var result = await GetUserAsync(id)
    .MapOnFailureAsync(err => TransformErrorAsync(err, ct));

Remarks

MapOnFailure is useful when you need to convert, enrich, or translate errors between different layers of your application. For example, converting domain errors to API-specific errors, or adding context to generic errors.

The error transformation only occurs if the Result is a failure; successful Results pass through unchanged.

This operation runs on the failure track - it only executes when the Result has failed.

Users should capture CancellationToken in their lambda closures when cancellation support is needed.

Methods

MapOnFailureAsync<T>(Result<T>, Func<Error, Task<Error>>)

Asynchronously transforms the error of a failed Result using the provided async mapping function.

[RailwayTrack(TrackBehavior.Failure)]
public static Task<Result<T>> MapOnFailureAsync<T>(this Result<T> result, Func<Error, Task<Error>> mapAsync)

Parameters

result Result<T>

The result whose error to potentially transform.

mapAsync Func<Error, Task<Error>>

The async function to transform the error.

Returns

Task<Result<T>>

The original result if successful; otherwise a new failure with the transformed error.

Type Parameters

T

Type of the result value.

MapOnFailureAsync<T>(Result<T>, Func<Error, ValueTask<Error>>)

Asynchronously transforms the error of a failed Result using the provided async mapping function.

[RailwayTrack(TrackBehavior.Failure)]
public static ValueTask<Result<T>> MapOnFailureAsync<T>(this Result<T> result, Func<Error, ValueTask<Error>> mapAsync)

Parameters

result Result<T>

The result whose error to potentially transform.

mapAsync Func<Error, ValueTask<Error>>

The async function to transform the error.

Returns

ValueTask<Result<T>>

The original result if successful; otherwise a new failure with the transformed error.

Type Parameters

T

Type of the result value.

MapOnFailureAsync<T>(Task<Result<T>>, Func<Error, Error>)

Asynchronously transforms the error of a failed Result using the provided mapping function.

[RailwayTrack(TrackBehavior.Failure)]
public static Task<Result<T>> MapOnFailureAsync<T>(this Task<Result<T>> resultTask, Func<Error, Error> map)

Parameters

resultTask Task<Result<T>>

The task containing the result whose error to potentially transform.

map Func<Error, Error>

The function to transform the error.

Returns

Task<Result<T>>

The original result if successful; otherwise a new failure with the transformed error.

Type Parameters

T

Type of the result value.

MapOnFailureAsync<T>(Task<Result<T>>, Func<Error, Task<Error>>)

Asynchronously transforms the error of a failed Result using the provided async mapping function.

[RailwayTrack(TrackBehavior.Failure)]
public static Task<Result<T>> MapOnFailureAsync<T>(this Task<Result<T>> resultTask, Func<Error, Task<Error>> mapAsync)

Parameters

resultTask Task<Result<T>>

The task containing the result whose error to potentially transform.

mapAsync Func<Error, Task<Error>>

The async function to transform the error.

Returns

Task<Result<T>>

The original result if successful; otherwise a new failure with the transformed error.

Type Parameters

T

Type of the result value.

MapOnFailureAsync<T>(ValueTask<Result<T>>, Func<Error, Error>)

Asynchronously transforms the error of a failed Result using the provided mapping function.

[RailwayTrack(TrackBehavior.Failure)]
public static ValueTask<Result<T>> MapOnFailureAsync<T>(this ValueTask<Result<T>> resultTask, Func<Error, Error> map)

Parameters

resultTask ValueTask<Result<T>>

The ValueTask containing the result whose error to potentially transform.

map Func<Error, Error>

The function to transform the error.

Returns

ValueTask<Result<T>>

The original result if successful; otherwise a new failure with the transformed error.

Type Parameters

T

Type of the result value.

MapOnFailureAsync<T>(ValueTask<Result<T>>, Func<Error, ValueTask<Error>>)

Asynchronously transforms the error of a failed Result using the provided async mapping function.

[RailwayTrack(TrackBehavior.Failure)]
public static ValueTask<Result<T>> MapOnFailureAsync<T>(this ValueTask<Result<T>> resultTask, Func<Error, ValueTask<Error>> mapAsync)

Parameters

resultTask ValueTask<Result<T>>

The ValueTask containing the result whose error to potentially transform.

mapAsync Func<Error, ValueTask<Error>>

The async function to transform the error.

Returns

ValueTask<Result<T>>

The original result if successful; otherwise a new failure with the transformed error.

Type Parameters

T

Type of the result value.

MapOnFailure<T>(Result<T>, Func<Error, Error>)

Transforms the error of a failed Result using the provided mapping function. If the Result is successful, it is returned unchanged.

[RailwayTrack(TrackBehavior.Failure)]
public static Result<T> MapOnFailure<T>(this Result<T> result, Func<Error, Error> map)

Parameters

result Result<T>

The result whose error to potentially transform.

map Func<Error, Error>

The function to transform the error.

Returns

Result<T>

The original result if successful; otherwise a new failure with the transformed error.

Type Parameters

T

Type of the result value.

Remarks

This operation runs on the failure track only. If the result is successful, the mapping function is not called.