View Source Errata.Cause (Errata v0.10.0)

A struct that holds the original error wrapped (chained) by an Errata error.

When an Errata error is created with a :cause (typically via the generated Errata.Error.wrap/2 macro), the original error is stored in the error's :cause field as an Errata.Cause struct. This preserves the context of the underlying failure without losing the benefits of a structured Errata error.

The struct has the following fields:

  • kind - the kind of the wrapped error, one of :error, :throw, or :exit, mirroring the first argument of Exception.format/3. Defaults to :error.
  • value - the wrapped error itself. This can be any term: another Errata error, a standard exception struct, or an arbitrary value such as the reason from an {:error, reason} tuple.
  • stacktrace - the stacktrace captured at the point the original error was caught (typically __STACKTRACE__ from a rescue/catch clause), or nil if none was provided.

Whereas the :env field of an Errata error records where the error was wrapped, the :stacktrace of the cause records where the original error occurred.

Use Errata.cause/1 and Errata.root_cause/1 to access the wrapped value(s), and Errata.format_chain/1 to render a full cause chain for logging.

Summary

Types

Type to represent the kind of a wrapped error.

t()

Type to represent a wrapped (chained) error.

Functions

Normalizes a value into an Errata.Cause struct.

Types

@type kind() :: :error | :throw | :exit

Type to represent the kind of a wrapped error.

Mirrors the kinds accepted by Exception.format/3.

@type t() :: %Errata.Cause{
  kind: kind(),
  stacktrace: Exception.stacktrace() | nil,
  value: term()
}

Type to represent a wrapped (chained) error.

Functions

@spec new(
  term(),
  keyword()
) :: t()

Normalizes a value into an Errata.Cause struct.

If value is already an Errata.Cause, it is returned unchanged. Otherwise a new struct is built, wrapping value. The following options are supported:

  • :kind - the kind of the wrapped error (see kind/0); defaults to :error
  • :stacktrace - the stacktrace captured where the original error occurred