Hyperlinkv0.8.0-beta.28

Effect

Effect.catchDefectconsteffect/Effect.ts:3249
<A2, E2, R2>(f: (defect: unknown) => Effect<A2, E2, R2>): <A, E, R>(
  self: Effect<A, E, R>
) => Effect<A2 | A, E2 | E, R2 | R>
<A, E, R, A2, E2, R2>(
  self: Effect<A, E, R>,
  f: (defect: unknown) => Effect<A2, E2, R2>
): Effect<A | A2, E | E2, R | R2>

Recovers from defects using a provided recovery function.

When to use

Use when you need to report or translate defects at integration boundaries.

Details

catchDefect handles unexpected defects, such as thrown exceptions or values passed to die, without catching typed failures or interruptions.

When to Recover from Defects:

Defects are unexpected errors that typically should not be recovered from, as they often indicate serious issues. In some cases, such as dynamically loaded plugins, controlled recovery may be needed.

Example (Recovering from defects)

import { Console, Effect } from "effect"

// An effect that might throw an unexpected error (defect)
const program = Effect.sync(() => {
  throw new Error("Unexpected error")
})

// Recover from defects only
const recovered = Effect.catchDefect(program, (defect) => {
  return Console.log(`Caught defect: ${defect}`).pipe(
    Effect.as("Recovered from defect")
  )
})
error handling
Source effect/Effect.ts:32499 lines
export const catchDefect: {
  <A2, E2, R2>(
    f: (defect: unknown) => Effect<A2, E2, R2>
  ): <A, E, R>(self: Effect<A, E, R>) => Effect<A2 | A, E2 | E, R2 | R>
  <A, E, R, A2, E2, R2>(
    self: Effect<A, E, R>,
    f: (defect: unknown) => Effect<A2, E2, R2>
  ): Effect<A | A2, E | E2, R | R2>
} = internal.catchDefect
Referenced by 1 symbols