Hyperlinkv0.8.0-beta.28

Effect

Effect.matchCauseEffectconsteffect/Effect.ts:5567
<E, A2, E2, R2, A, A3, E3, R3>(options: {
  readonly onFailure: (cause: Cause.Cause<E>) => Effect<A2, E2, R2>
  readonly onSuccess: (a: A) => Effect<A3, E3, R3>
}): <R>(self: Effect<A, E, R>) => Effect<A2 | A3, E2 | E3, R2 | R3 | R>
<A, E, R, A2, E2, R2, A3, E3, R3>(
  self: Effect<A, E, R>,
  options: {
    readonly onFailure: (cause: Cause.Cause<E>) => Effect<A2, E2, R2>
    readonly onSuccess: (a: A) => Effect<A3, E3, R3>
  }
): Effect<A2 | A3, E2 | E3, R2 | R3 | R>

Handles failures with access to the cause and allows performing side effects.

When to use

Use when you need to fold an Effect with effectful success handlers and Cause-aware failure handlers.

Details

The matchCauseEffect function works similarly to matchCause, but it also allows you to perform additional side effects based on the failure cause. This function provides access to the complete cause of the failure, making it possible to differentiate between various failure types, and allows you to respond accordingly while performing side effects (like logging or other operations).

Example (Effectfully matching on causes)

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

class TaskError extends Data.TaggedError("TaskError")<{ readonly message: string }> {}

const task = Effect.fail(new TaskError({ message: "Task failed" }))

const program = Effect.matchCauseEffect(task, {
  onFailure: (cause) =>
    Effect.gen(function*() {
      if (Cause.hasFails(cause)) {
        const error = Cause.findError(cause)
        if (Result.isSuccess(error)) {
          yield* Console.log(`Handling error: ${error.success.message}`)
        }
        return "recovered from error"
      } else {
        yield* Console.log("Handling interruption or defect")
        return "recovered from interruption/defect"
      }
    }),
  onSuccess: (value) =>
    Effect.gen(function*() {
      yield* Console.log(`Success: ${value}`)
      return `processed ${value}`
    })
})

Effect.runPromise(program).then(console.log)
// Output:
// Handling error: Task failed
// recovered from error
pattern matchingmatchCausematchEffect
Source effect/Effect.ts:556713 lines
export const matchCauseEffect: {
  <E, A2, E2, R2, A, A3, E3, R3>(options: {
    readonly onFailure: (cause: Cause.Cause<E>) => Effect<A2, E2, R2>
    readonly onSuccess: (a: A) => Effect<A3, E3, R3>
  }): <R>(self: Effect<A, E, R>) => Effect<A2 | A3, E2 | E3, R2 | R3 | R>
  <A, E, R, A2, E2, R2, A3, E3, R3>(
    self: Effect<A, E, R>,
    options: {
      readonly onFailure: (cause: Cause.Cause<E>) => Effect<A2, E2, R2>
      readonly onSuccess: (a: A) => Effect<A3, E3, R3>
    }
  ): Effect<A2 | A3, E2 | E3, R2 | R3 | R>
} = internal.matchCauseEffect
Referenced by 2 symbols