Skip to content

Parallel and Sequential Errors

An operation can fail for more than one reason—concurrent fibers can fail together, or cleanup can fail after the main work stops. Effect 4 collects every reason in a single flat Cause.Cause<E> rather than a nested tree.

MechanismHoldsUse when
Cause.reasonsFlat readonly array of Fail, Die, and InterruptInspecting every failure, defect, or cancellation at a boundary
Cause.findError / Cause.findDefectResult with one matching entryExtracting a single failure or defect
validate / partitionTyped array of domain errorsCollecting all domain validation errors as ordinary E / A data

Representing multiple failures

function failureKinds() {
  const cause = Cause.combine(Cause.fail("delivery rejected"), Cause.die(new Error("release invariant failed")))
  return cause.reasons.map((reason) => reason._tag)
}
// ["Fail", "Die"]
// Run in an effect function: try Console.log(try Effect.try(() => JSON.stringify(failureKinds())))
["Fail","Die"]

A Cause.Cause<E> stores a flat readonly array of Cause.Reason<E> entries: Fail (carrying error), Die (carrying defect), or Interrupt. There is no sequential or parallel tree to walk.

Combining concurrent or sequential reasons

effect function operationWithBrokenCleanup() {
  return try ensuring(
    effect { throw "delivery rejected" },
    die(new Error("release invariant failed"))
  )
}
 
effect function inspectDelivery() {
  const outcome = try exit(operationWithBrokenCleanup())
  if (Exit.isFailure(outcome)) {
    return {
      expected: Cause.hasFails(outcome.cause),
      defective: Cause.hasDies(outcome.cause),
      interrupted: Cause.hasInterrupts(outcome.cause)
    }
  }
  return { expected: false, defective: false, interrupted: false }
}
// Run in an effect function:
//   {
//     const jsonValue = try inspectDelivery()
//     try Console.log(try Effect.try(() => JSON.stringify(jsonValue)))
//   }
{"expected":false,"defective":true,"interrupted":false}

A cleanup failure is recorded alongside the original failure; when either is a defect, every expected failure in that cause becomes a defect carrying the same error. Concurrent combinators normally interrupt siblings after the first failure, yet overlapping failures can still contribute multiple reasons, without preserving a causal tree or execution order.

A catch expression (like Effect.catch) cannot recover a cause containing a defect, and when a typed failure accompanies an interruption it recovers the failure and drops the interruption. When every reason is an expected failure—for example, when both sides of Effect.race fail—catch receives the first error and handles the whole cause; use exit or catchCause whenever recovery must see every reason.

Runtime causes and domain error collections

error InvalidPort { port: number }
 
effect function validatePorts() {
  return try validate([8080, -1, 70000], effect (port) => {
    if (port < 1 || port > 65535) throw new InvalidPort({ port })
    return port
  })
}
// number[]![InvalidPort, ...InvalidPort[]]
// Run in an effect function:
//   {
//     const jsonValue = (try flip(validatePorts())).map((error) => error.port)
//     try Console.log(try Effect.try(() => JSON.stringify(jsonValue)))
//   }
[-1,70000]

validate produces a single typed failure containing an array of domain errors, not multiple runtime cause reasons. Reach for partition when valid outputs must be kept alongside the rejected ones.

Coming from Effect

Effect 4's flat cause model applies unchanged. Attach cleanup with ensuring so cleanup failures join the primary cause, as inspectDelivery demonstrates. Note that a timeout boundary currently drops cleanup failures from the interrupted source; see Timing Out.


The matching Effect page: effect.website/docs/v4/error-management/parallel-and-sequential-errors