effect-io-ai

Package: effect
Module: Effect

Effect.tapCauseIf

Executes a side effect conditionally when a failed effect’s cause matches a predicate.

Details

This function allows you to tap into the cause of an effect’s failure only when the cause matches a specific predicate. This is useful for conditional logging, monitoring, or other side effects based on the type of failure.

Example (Observing selected failure causes)

import { Cause, Console, Effect } from "effect"

const task = Effect.fail("Network timeout")

// Only log causes that contain failures (not interrupts or defects)
const program = Effect.tapCauseIf(
  task,
  Cause.hasFails,
  (cause) => Console.log(`Logging failure cause: ${Cause.squash(cause)}`)
)

Effect.runPromiseExit(program).then(console.log)
// Output: "Logging failure cause: Network timeout"
// Then: { _id: 'Exit', _tag: 'Failure', cause: ... }

Signature

declare const tapCauseIf: { <E, B, E2, R2>(predicate: Predicate.Predicate<Cause.Cause<E>>, f: (cause: Cause.Cause<E>) => Effect<B, E2, R2>): <A, R>(self: Effect<A, E, R>) => Effect<A, E | E2, R | R2>; <A, E, R, B, E2, R2>(self: Effect<A, E, R>, predicate: Predicate.Predicate<Cause.Cause<E>>, f: (cause: Cause.Cause<E>) => Effect<B, E2, R2>): Effect<A, E | E2, R | R2>; }

Source

Since v4.0.0