Skip to main content
The Cause module provides a lossless error model that captures all information about failures in Effect workflows, including parallel errors, sequential errors, defects, and interruptions.

Type

type Cause<E> =
  | Empty
  | Fail<E>
  | Die
  | Interrupt
  | Sequential<E>
  | Parallel<E>
Represents the full history of a failure within an Effect.
E
type parameter
The error type contained in the cause
Cause<E>
Cause
A data structure capturing all failure information

Cause Variants

Empty

Represents a lack of errors.
interface Empty extends Cause.Variance<never>

Fail

Represents an expected error.
interface Fail<out E> extends Cause.Variance<E> {
  readonly _tag: "Fail"
  readonly error: E
}
error
E
The error value

Die

Represents an unexpected defect.
interface Die extends Cause.Variance<never> {
  readonly _tag: "Die"
  readonly defect: unknown
}
defect
unknown
The defect (unrecoverable error)

Interrupt

Represents fiber interruption.
interface Interrupt extends Cause.Variance<never> {
  readonly _tag: "Interrupt"
  readonly fiberId: FiberId.FiberId
}
fiberId
FiberId.FiberId
The ID of the interrupted fiber

Sequential

Represents sequential composition of two causes.
interface Sequential<out E> extends Cause.Variance<E> {
  readonly _tag: "Sequential"
  readonly left: Cause<E>
  readonly right: Cause<E>
}

Parallel

Represents parallel composition of two causes.
interface Parallel<out E> extends Cause.Variance<E> {
  readonly _tag: "Parallel"
  readonly left: Cause<E>
  readonly right: Cause<E>
}

Constructors

empty

Creates an Empty cause.
const empty: Cause<never>
return
Cause<never>
A cause representing no error
import { Cause } from "effect"

const noCause = Cause.empty

fail

Creates a Fail cause from an expected error.
const fail: <E>(error: E) => Cause<E>
error
E
required
The error value
return
Cause<E>
A cause representing a typed error
import { Cause } from "effect"

const cause = Cause.fail("Something went wrong")

die

Creates a Die cause from an unexpected error.
const die: (defect: unknown) => Cause<never>
defect
unknown
required
The defect value
return
Cause<never>
A cause representing an unrecoverable defect
import { Cause } from "effect"

const cause = Cause.die(new Error("Unexpected crash"))

interrupt

Creates an Interrupt cause from a FiberId.
const interrupt: (fiberId: FiberId.FiberId) => Cause<never>
fiberId
FiberId.FiberId
required
The fiber ID that was interrupted
return
Cause<never>
A cause representing an interruption

parallel

Combines two causes in parallel.
const parallel: <E, E2>(left: Cause<E>, right: Cause<E2>) => Cause<E | E2>
left
Cause<E>
required
The first cause
right
Cause<E2>
required
The second cause
return
Cause<E | E2>
A cause combining both errors in parallel
import { Cause } from "effect"

const cause = Cause.parallel(
  Cause.fail("error1"),
  Cause.fail("error2")
)

sequential

Combines two causes sequentially.
const sequential: <E, E2>(left: Cause<E>, right: Cause<E2>) => Cause<E | E2>
left
Cause<E>
required
The first cause
right
Cause<E2>
required
The second cause
return
Cause<E | E2>
A cause combining both errors sequentially

Guards

isCause

Checks if a value is a Cause.
const isCause: (u: unknown) => u is Cause<unknown>

isEmptyType

Checks if a Cause is an Empty type.
const isEmptyType: <E>(self: Cause<E>) => self is Empty

isFailType

Checks if a Cause is a Fail type.
const isFailType: <E>(self: Cause<E>) => self is Fail<E>

isDieType

Checks if a Cause is a Die type.
const isDieType: <E>(self: Cause<E>) => self is Die

isInterruptType

Checks if a Cause is an Interrupt type.
const isInterruptType: <E>(self: Cause<E>) => self is Interrupt

Getters

isEmpty

Checks if a Cause is entirely empty.
const isEmpty: <E>(self: Cause<E>) => boolean
self
Cause<E>
required
The cause to check
return
boolean
true if the cause contains no errors

isFailure

Checks if a Cause contains a failure.
const isFailure: <E>(self: Cause<E>) => boolean
self
Cause<E>
required
The cause to check
return
boolean
true if the cause includes any Fail error

isDie

Checks if a Cause contains a defect.
const isDie: <E>(self: Cause<E>) => boolean

isInterrupted

Checks if a Cause contains an interruption.
const isInterrupted: <E>(self: Cause<E>) => boolean

failures

Extracts all recoverable errors from a Cause.
const failures: <E>(self: Cause<E>) => Chunk.Chunk<E>
self
Cause<E>
required
The cause to extract from
return
Chunk.Chunk<E>
A collection of all Fail values
import { Cause } from "effect"

const cause = Cause.parallel(
  Cause.fail("error1"),
  Cause.fail("error2")
)

const errors = Cause.failures(cause)
// Chunk.Chunk<string>

defects

Extracts all unrecoverable defects from a Cause.
const defects: <E>(self: Cause<E>) => Chunk.Chunk<unknown>

interruptors

Collects all FiberIds responsible for interrupting a fiber.
const interruptors: <E>(self: Cause<E>) => HashSet.HashSet<FiberId.FiberId>

Mapping

map

Transforms the errors in a Cause.
const map: {
  <E, E2>(f: (e: E) => E2): (self: Cause<E>) => Cause<E2>
  <E, E2>(self: Cause<E>, f: (e: E) => E2): Cause<E2>
}
self
Cause<E>
required
The cause to transform
f
(e: E) => E2
required
The transformation function
return
Cause<E2>
A cause with transformed errors
import { Cause } from "effect"

const cause = Cause.fail("error")
const mapped = Cause.map(cause, (e) => e.toUpperCase())

flatMap

Transforms errors in a Cause into new causes.
const flatMap: {
  <E, E2>(f: (e: E) => Cause<E2>): (self: Cause<E>) => Cause<E2>
  <E, E2>(self: Cause<E>, f: (e: E) => Cause<E2>): Cause<E2>
}

Matching

match

Transforms a Cause into a single value using custom handlers.
const match: {
  <Z, E>(options: {
    readonly onEmpty: Z
    readonly onFail: (error: E) => Z
    readonly onDie: (defect: unknown) => Z
    readonly onInterrupt: (fiberId: FiberId.FiberId) => Z
    readonly onSequential: (left: Z, right: Z) => Z
    readonly onParallel: (left: Z, right: Z) => Z
  }): (self: Cause<E>) => Z
  ...
}
self
Cause<E>
required
The cause to match on
options
object
required
Handlers for each cause type
return
Z
The result of applying the appropriate handler
import { Cause } from "effect"

const cause = Cause.fail("error")

const result = Cause.match(cause, {
  onEmpty: 0,
  onFail: (error) => 1,
  onDie: (defect) => 2,
  onInterrupt: (fiberId) => 3,
  onSequential: (left, right) => left + right,
  onParallel: (left, right) => left + right
})
// result: 1

Error Types

RuntimeException

An error representing a runtime error.
const RuntimeException: new(message?: string) => RuntimeException

InterruptedException

An error representing fiber interruption.
const InterruptedException: new(message?: string) => InterruptedException

IllegalArgumentException

An error representing an invalid argument.
const IllegalArgumentException: new(message?: string) => IllegalArgumentException

NoSuchElementException

An error that occurs when an expected element is missing.
const NoSuchElementException: new(message?: string) => NoSuchElementException

TimeoutException

An error representing a computation that timed out.
const TimeoutException: new(message?: string) => TimeoutException

UnknownException

A checked exception for handling unknown errors.
const UnknownException: new(error: unknown, message?: string) => UnknownException
error
unknown
required
The unknown error to wrap
message
string
Optional error message
The error argument is stored in the error property and passed as the cause to preserve stack traces.

Formatting

pretty

Converts a Cause into a human-readable string.
const pretty: <E>(cause: Cause<E>, options?: {
  readonly renderErrorCause?: boolean
}) => string
cause
Cause<E>
required
The cause to format
options.renderErrorCause
boolean
Whether to render the error cause
return
string
A formatted string representation
import { Cause } from "effect"

const cause = Cause.fail("Something went wrong")
console.log(Cause.pretty(cause))

Build docs developers (and LLMs) love