effect-io-ai

Package: effect
Module: Effect

Effect.catchCause

Handles both recoverable and unrecoverable errors by providing a recovery effect.

When to use

Use when you need to recover from an Effect by inspecting the full Cause, including recoverable failures, defects, and interruptions, instead of only the typed error value.

Details

When to Recover from Defects:

Defects are unexpected errors that typically shouldn’t be recovered from, as they often indicate serious issues. However, in some cases, such as dynamically loaded plugins, controlled recovery might be needed.

Example (Recovering from full failure causes)

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

// An effect that might fail in different ways
const program = Effect.die("Something went wrong")

// Recover from any cause (including defects)
const recovered = Effect.catchCause(program, (cause) => {
  if (Cause.hasDies(cause)) {
    return Console.log("Caught defect").pipe(
      Effect.as("Recovered from defect")
    )
  }
  return Effect.succeed("Unknown error")
})

Signature

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

Source

Since v4.0.0