Hyperlinkv0.8.0-beta.28

Cause

Cause.Reasontypeeffect/Cause.ts:146
Reason<E>

A single entry inside a Cause's reasons array.

Details

Narrow to a concrete type with isFailReason, isDieReason, or isInterruptReason.

  • Fail<E> — typed error, access via .error
  • Die — untyped defect, access via .defect
  • Interrupt — fiber interruption, access via .fiberId

Every reason carries an annotations map and an annotate method for attaching tracing metadata.

Example (Narrowing a reason)

import { Cause } from "effect"

const reason = Cause.fail("error").reasons[0]
if (Cause.isFailReason(reason)) {
  console.log(reason.error) // "error"
}
Source effect/Cause.ts:146149 lines
export type Reason<E> = Fail<E> | Die | Interrupt

/**
 * Narrows a `Reason` to `Fail`.
 *
 * **When to use**
 *
 * Use as a predicate for `Array.filter` to pick out typed `Fail` reasons when
 * iterating over `cause.reasons`.
 *
 * **Example** (Filtering fail reasons)
 *
 * ```ts
 * import { Cause } from "effect"
 *
 * const cause = Cause.fail("error")
 * const fails = cause.reasons.filter(Cause.isFailReason)
 * console.log(fails[0].error) // "error"
 * ```
 *
 * @see {@link isDieReason} — narrow to `Die`
 * @see {@link isInterruptReason} — narrow to `Interrupt`
 *
 * @category guards
 * @since 4.0.0
 */
export const isFailReason: <E>(self: Reason<E>) => self is Fail<E> = core.isFailReason

/**
 * Narrows a `Reason` to `Die`.
 *
 * **When to use**
 *
 * Use as a predicate for `Array.filter` to pick out `Die` (defect) reasons when
 * iterating over `cause.reasons`.
 *
 * **Example** (Filtering die reasons)
 *
 * ```ts
 * import { Cause } from "effect"
 *
 * const cause = Cause.die("defect")
 * const dies = cause.reasons.filter(Cause.isDieReason)
 * console.log(dies[0].defect) // "defect"
 * ```
 *
 * @see {@link isFailReason} — narrow to `Fail`
 * @see {@link isInterruptReason} — narrow to `Interrupt`
 *
 * @category guards
 * @since 4.0.0
 */
export const isDieReason: <E>(self: Reason<E>) => self is Die = core.isDieReason

/**
 * Narrows a `Reason` to `Interrupt`.
 *
 * **When to use**
 *
 * Use as a predicate for `Array.filter` to pick out `Interrupt` reasons when
 * iterating over `cause.reasons`.
 *
 * **Example** (Filtering interrupt reasons)
 *
 * ```ts
 * import { Cause } from "effect"
 *
 * const cause = Cause.interrupt(123)
 * const interrupts = cause.reasons.filter(Cause.isInterruptReason)
 * console.log(interrupts[0].fiberId) // 123
 * ```
 *
 * @see {@link isFailReason} — narrow to `Fail`
 * @see {@link isDieReason} — narrow to `Die`
 *
 * @category guards
 * @since 4.0.0
 */
export const isInterruptReason: <E>(self: Reason<E>) => self is Interrupt = core.isInterruptReason

/**
 * Companion namespace for the `Cause` interface.
 *
 * @since 2.0.0
 */
export declare namespace Cause {
  /**
   * Extracts the error type `E` from a `Cause<E>`.
   *
   * **Example** (Extracting the error type)
   *
   * ```ts
   * import type { Cause } from "effect"
   *
   * // string
   * type E = Cause.Cause.Error<Cause.Cause<string>>
   * ```
   *
   * @category models
   * @since 4.0.0
   */
  export type Error<T> = T extends Cause<infer E> ? E : never

  /**
   * Base interface shared by all reason types (`Fail`, `Die`, `Interrupt`).
   *
   * **Details**
   *
   * Every reason carries:
   * - `_tag` — discriminant string (`"Fail"`, `"Die"`, or `"Interrupt"`)
   * - `annotations` — tracing metadata attached by the runtime
   * - `annotate()` — returns a copy with additional annotations
   *
   * @category models
   * @since 4.0.0
   */
  export interface ReasonProto<Tag extends string> extends Inspectable, Equal {
    readonly [ReasonTypeId]: typeof ReasonTypeId
    readonly _tag: Tag
    readonly annotations: ReadonlyMap<string, unknown>
    annotate(annotations: Context.Context<never> | ReadonlyMap<string, unknown>, options?: {
      readonly overwrite?: boolean | undefined
    }): this
  }
}

/**
 * Companion namespace for the `Reason` type.
 *
 * @since 4.0.0
 */
export declare namespace Reason {
  /**
   * Extracts the error type `E` from a `Reason<E>`.
   *
   * **Example** (Extracting the error type)
   *
   * ```ts
   * import type { Cause } from "effect"
   *
   * // string
   * type E = Cause.Reason.Error<Cause.Reason<string>>
   * ```
   *
   * @category models
   * @since 4.0.0
   */
  export type Error<T> = T extends Reason<infer E> ? E : never
}
Referenced by 9 symbols