dream_errors

The shared error type of the Dream ecosystem.

Each Dream package maps its own error to Error(cause), and keeps its own error whole as the cause. An app then matches on one type, and a log still gets the exact error of the package.

The message of each variant is text for developers and logs. It is never text for the users of an app: it can name tables, constraints, hosts and keys. Show your own text to users.

Map a package error Tested snippet

import dream_errors.{type Error, Conflict, NotFound, Unavailable}

/// The error of a store package.
pub type StoreError {
  KeyMissing(key: String)
  KeyTaken(key: String)
  StoreDown(reason: String)
}

/// Maps a store error to the shared error, with the store error as the cause.
pub fn to_error(error: StoreError) -> Error(StoreError) {
  case error {
    KeyMissing(key:) -> NotFound("no value for key " <> key)
    KeyTaken(key:) -> Conflict("the key " <> key <> " has a value", error)
    StoreDown(reason:) -> Unavailable("the store is down: " <> reason, error)
  }
}

Types

What went wrong, in terms every Dream package shares. cause is the package’s own error, kept whole for logs and for exact matches.

pub type Error(cause) {
  NotFound(message: String)
  Retry(
    message: String,
    cause: cause,
    after_seconds: option.Option(Int),
  )
  Conflict(message: String, cause: cause)
  Invalid(message: String, cause: cause)
  Unavailable(message: String, cause: cause)
  Timeout(message: String, cause: cause)
  Failed(message: String, cause: cause)
}

Constructors

  • NotFound(message: String)

    The thing asked for does not exist. Absence is a result, not a fault, so it has no cause.

  • Retry(
      message: String,
      cause: cause,
      after_seconds: option.Option(Int),
    )

    Nothing happened, and running the whole operation again can succeed (a deadlock, a serialization failure). after_seconds is the delay in seconds before the next try: Some(n) when the source knows it, else None. A delay is zero or more. Dream sends no retry-after header for None or for a negative delay.

  • Conflict(message: String, cause: cause)

    The write clashes with existing state (unique key, existing table).

  • Invalid(message: String, cause: cause)

    The caller’s input broke a rule.

  • Unavailable(message: String, cause: cause)

    A dependency could not be reached (refused, pool empty).

  • Timeout(message: String, cause: cause)

    A deadline passed.

  • Failed(message: String, cause: cause)

    Anything else.

Values

pub fn cause(error: Error(cause)) -> option.Option(cause)

The package’s own error, or None for NotFound, which has no cause.

pub fn kind_name(error: Error(cause)) -> String

The name of the variant in snake case, for logs and metrics: "not_found", "retry", "conflict", "invalid", "unavailable", "timeout" or "failed".

pub fn map_cause(error: Error(a), with: fn(a) -> b) -> Error(b)

Changes the cause of an error with with, and keeps the variant, the message and the after_seconds of a Retry. A NotFound stays the same, because it has no cause.

pub fn message(error: Error(cause)) -> String

The message of an error: text for developers and logs, never for the users of an app.

✨ Search Document