Cause: inspect defects

When an effect fails with an unexpected error Cause defines everything that may have caused the issue:

  • Fail<E>: when we do not recover from an expected error
  • Die: when an effect cannot recover or continue, hitting a dead end (for example when using throw or by manually calling Effect.die or Effect.dieMessage)
  • Interrupt: when an interrupt signal stops the execution of the effect (for example by using Effect.interrupt)
  • Sequential<E> | Parallel<E>: collects multiple errors in sequential or parallel effect executions
  • Empty: usually seen as one of the members of Sequential or Parallel when one side succeeded and the other failed
export type Cause<E> = Empty | Fail<E> | Die | Interrupt | Sequential<E> | Parallel<E>

Example API where Cause.Empty is returned when no interrupt has occurred
Example API where Cause.Empty is returned when no interrupt has occurred

There are multiple APIs in effect to cause, recover, and inspect defects. Some of the most commons are:

  • Effect.die
  • Effect.interrupt
  • Layer.orDie
  • Effect.catchAllCause
  • Effect.catchAllDefect
  • Effect.tapDefect

From a software design standpoint, defect are unrecoverable. Some examples are:

  • Out of memory
  • Power outage
  • Stack overflow

Unlike failures, defects should never be designed as part of a normal flow, and instead you should allow the program to crash.

I encourage you to take a look at them yourself (the API reference is your best friend).