geny
(WIP.)
Inspired by rust’s anyhow
gleam add geny
import geny.{type GResult}
import geny.ext.ensure
pub fn user_allowed(user_id: Int) -> GResult(String, String) {
use <- ensure(must: user_id == 0, error: "Only user 0 is allowed")
// ...
// ...
// ...
Ok("Good")
}
user_allowed(0) |> should.be_ok
user_allowed(1) |> should.be_error
user_allowed(2) |> should.be_error
You could add context for GResult
and print them:
import geny.{type GResult}
import geny/ext.{context, from, to_string}
import gleam/io
let result = Error("This is a error") |> from // <-- This convert/wrap normal `Result` into `GResult`
let result = result |> context("The context A")
let result = result |> context("The context B")
let result = result |> context("The context C")
case result {
Ok(_) -> io.println("Good ok!")
Error(geny) -> io.println(geny |> to_string)
}
The output is:
error: This is a error
caused by:
0: The context A
1: The context B
2: The context C
Further documentation can be found at https://hexdocs.pm/geny.
Development
gleam run # Run the project
gleam test # Run the tests
gleam shell # Run an Erlang shell