Table of Contents

Class TapOnFailureExtensions

Namespace
FunctionalDdd
Assembly
FunctionalDdd.RailwayOrientedProgramming.dll

Provides extension methods for executing side effects on failed Results without changing the Result.

public static class TapOnFailureExtensions
Inheritance
TapOnFailureExtensions
Inherited Members

Examples

var result = GetUser(id)
    .TapOnFailure(error => _logger.LogError("Failed to get user: {Error}", error.Detail))
    .TapOnFailure(() => _metrics.IncrementCounter("user.get.failed"));

Remarks

TapOnFailure is the counterpart to TapExtensions. It allows you to perform side effects (like logging, metrics, or debugging) when a Result is in a failed state, without altering the Result itself. The action is only executed if the Result is a failure, and the original Result is always returned unchanged.

This operation runs on the failure track - it only executes when the Result has failed. If the Result is successful, the operation is skipped entirely.

Methods

TapOnFailure<TValue>(Result<TValue>, Action)

Executes the given action if the result is a failure. Returns the original result unchanged.

[RailwayTrack(TrackBehavior.Failure)]
public static Result<TValue> TapOnFailure<TValue>(this Result<TValue> result, Action action)

Parameters

result Result<TValue>

The result to tap.

action Action

The action to execute if the result is a failure.

Returns

Result<TValue>

The original result unchanged.

Type Parameters

TValue

Type of the result value.

Remarks

This operation runs on the failure track only. If the result is successful, the action is not executed.

TapOnFailure<TValue>(Result<TValue>, Action<Error>)

Executes the given action with the error if the result is a failure. Returns the original result unchanged.

[RailwayTrack(TrackBehavior.Failure)]
public static Result<TValue> TapOnFailure<TValue>(this Result<TValue> result, Action<Error> action)

Parameters

result Result<TValue>

The result to tap.

action Action<Error>

The action to execute with the error if the result is a failure.

Returns

Result<TValue>

The original result unchanged.

Type Parameters

TValue

Type of the result value.

Remarks

This operation runs on the failure track only. If the result is successful, the action is not executed. The error object is passed to the action for inspection or logging.