The shared error type of the Dream ecosystem
dream_errors
dream_errors is the shared error type of the Dream packages. Each package, for example dream_postgres, dream_ets, dream_http_client and dream_opensearch, maps its own error to dream_errors.Error(cause). The package keeps its own error whole as the cause. Your app matches on one type for every package, and your logs still get the exact error.
dream_errors depends only on the Gleam standard library.
Features
- ✅ Seven variants that every package shares:
NotFound,Retry,Conflict,Invalid,Unavailable,TimeoutandFailed - ✅ The package’s own error as a typed
cause, for logs and for exact matches - ✅
message,cause,map_causeandkind_name - ✅ No dependencies other than
gleam_stdlib
Installation
gleam add dream_errors
Quick Start
The type:
The shared error type

/// 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) {
/// The thing asked for does not exist. Absence is a result, not a fault, so it has no cause.
NotFound(message: String)
/// 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.
Retry(message: String, cause: cause, after_seconds: Option(Int))
/// The write clashes with existing state (unique key, existing table).
Conflict(message: String, cause: cause)
/// The caller's input broke a rule.
Invalid(message: String, cause: cause)
/// A dependency could not be reached (refused, pool empty).
Unavailable(message: String, cause: cause)
/// A deadline passed.
Timeout(message: String, cause: cause)
/// Anything else.
Failed(message: String, cause: cause)
}
The message is text for developers and logs. Never show it to the users of your app: it can name tables, constraints, hosts and keys.
A package maps its own error with a to_error function:
Map a package error

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)
}
}
An app matches on the variant, and shows its own text:
Answer with a status and your own text

import dream_errors.{
type Error, Conflict, Failed, Invalid, NotFound, Retry, Timeout, Unavailable,
}
/// The status code and the text that an app shows for a shared error.
/// The text is the app's own text, never the message of the error.
pub fn answer(error: Error(cause)) -> #(Int, String) {
case error {
NotFound(..) -> #(404, "We could not find that.")
Retry(..) -> #(503, "Please try again.")
Conflict(..) -> #(409, "That exists already.")
Invalid(..) -> #(400, "Please check your input.")
Unavailable(..) | Timeout(..) -> #(503, "Please try again soon.")
Failed(..) -> #(500, "Something went wrong.")
}
}
In a Dream app, error.from_shared of Dream 3.0.0 gives the HTTP status of the error.
Guides
- Using dream_errors: what each variant means, why the message is not for users, how a package maps its error, and how an app matches on it
Requirements
- Gleam 1.19.2 or newer.
About Dream
dream_errors is part of Dream, a web toolkit for Gleam. It is built with the same quality standards as Dream.
Contributing
See CONTRIBUTING.md.