gleyre

Package Version Hex Docs

Pronounced like the English word “glare”, this gleyre is based on the error-handling model from the Rust crate eyre. (eyre is, itself, a fork of anyhow.)

I chose not to alias the built-in Result type (it’s not necessary to get Gleam’s version of early-return-sugar), so we have the Outcome type instead:

pub type Outcome(a) {
  Okay(a)
  Err(Report)
}

Where Report is the opaque error message vehicle.

In your gleam.toml:

gleyre = { git = "https://codeberg.org/d2718/gleyre", ref = "main" }

In your application:

import gleyre.{type Outcome}
import your_mod.{type Config, type Thing}

fn lowest_level(path: String) -> Outcome(Thing) {
  your_mod.load_thing(path) |> gleyre.from()
}

fn middle_level(cfg: Config) -> Outcome(Thing) {
  let path = your_mod.get_path(cfg)
  lowest_level(path)
  |> gleyre.wrap(string.append("attempting to load Thing from ", path))
}

fn high_level(config_str: String) -> Outcome(Thing) {
  use cfg <- gleyre.try_or_wrap(
    your_mod.parse_config(config_str),
    string.append("parsing config string: ", config_str)
  )

  middle_level(cfg)
  |> gleyre.wrap("middle_level operation failed")
}

pub fn highest_level() {
  case high_level(config_str) {
    Okay(thing) -> {
      do_thing(thing)
      io.println("Success! You did the thing.")
    Err(e) -> {
      gleyre.to_string(e)
      |> io.println_error()
    }
  }
}
Search Document