Hyperlinkv0.8.0-beta.28

Effect

Effect.matchCauseconsteffect/Effect.ts:5416
<E, A2, A, A3>(options: {
  readonly onFailure: (cause: Cause.Cause<E>) => A2
  readonly onSuccess: (a: A) => A3
}): <R>(self: Effect<A, E, R>) => Effect<A2 | A3, never, R>
<A, E, R, A2, A3>(
  self: Effect<A, E, R>,
  options: {
    readonly onFailure: (cause: Cause.Cause<E>) => A2
    readonly onSuccess: (a: A) => A3
  }
): Effect<A2 | A3, never, R>

Handles failures by matching the cause of failure.

When to use

Use when you need to fold an Effect while the failure handler inspects the full Cause.

Details

The matchCause function allows you to handle failures with access to the full cause of the failure within a fiber.

Example (Matching on success or failure causes)

import { Cause, Effect } from "effect"

const task = Effect.fail("Something went wrong")

const program = Effect.matchCause(task, {
  onFailure: (cause) => `Failed: ${Cause.squash(cause)}`,
  onSuccess: (value) => `Success: ${value}`
})

Effect.runPromise(program).then(console.log)
// Output: "Failed: Error: Something went wrong"
pattern matchingmatchCauseEffectmatch
Source effect/Effect.ts:541613 lines
export const matchCause: {
  <E, A2, A, A3>(options: {
    readonly onFailure: (cause: Cause.Cause<E>) => A2
    readonly onSuccess: (a: A) => A3
  }): <R>(self: Effect<A, E, R>) => Effect<A2 | A3, never, R>
  <A, E, R, A2, A3>(
    self: Effect<A, E, R>,
    options: {
      readonly onFailure: (cause: Cause.Cause<E>) => A2
      readonly onSuccess: (a: A) => A3
    }
  ): Effect<A2 | A3, never, R>
} = internal.matchCause
Referenced by 1 symbols