Hyperlinkv0.8.0-beta.28

Effect

Effect.tapErrorconsteffect/Effect.ts:3656
<E, X, E2, R2>(f: (e: NoInfer<E>) => Effect<X, E2, R2>): <A, R>(
  self: Effect<A, E, R>
) => Effect<A, E | E2, R2 | R>
<A, E, R, X, E2, R2>(
  self: Effect<A, E, R>,
  f: (e: E) => Effect<X, E2, R2>
): Effect<A, E | E2, R | R2>

Runs an effectful operation when the source effect fails, while preserving the original failure when the operation succeeds.

Details

Use this for logging, metrics, or other failure-side observations. If the operation passed to tapError fails, that error is also represented in the returned effect's error channel.

Example (Running effects on failure)

import { Console, Effect } from "effect"

// Simulate a task that fails with an error
const task: Effect.Effect<number, string> = Effect.fail("NetworkError")

// Use tapError to log the error message when the task fails
const tapping = Effect.tapError(
  task,
  (error) => Console.log(`expected error: ${error}`)
)

Effect.runFork(tapping)
// Output:
// expected error: NetworkError
sequencing
Source effect/Effect.ts:36569 lines
export const tapError: {
  <E, X, E2, R2>(
    f: (e: NoInfer<E>) => Effect<X, E2, R2>
  ): <A, R>(self: Effect<A, E, R>) => Effect<A, E | E2, R2 | R>
  <A, E, R, X, E2, R2>(
    self: Effect<A, E, R>,
    f: (e: E) => Effect<X, E2, R2>
  ): Effect<A, E | E2, R | R2>
} = internal.tapError
Referenced by 2 symbols