Skip to content

sporto/gleam-problem

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

problem

Package Version Hex Docs

An Error Handling library for Gleam. Inspired by https://github.com/lpil/snag but with your own error type.

Problem gives you:

  • An error stack that makes it easier to track the path of an error.
  • Use your error type (instead of String).
gleam add problem
import gleam/function
import problem.{type Outcome}

fn run_program(email) {
  case signup(email) {
    Error(problem) -> {
      problem
      |> problem.pretty_print(function.identity)
      |> echo
    }
    Ok(user) -> {
      todo
    }
  }
}

fn signup(email: String) -> Outcome(User, String) {
  use valid_email <- result.try(
    validate_email(email)
    |> problem.context("in signup")
  )

  todo
}

fn validate_email(email: String) -> Outcome(String, String) {
  Error("Invalid email")
    |> problem.outcome
    |> problem.context("in validate_email")
}
run_program("invalid email")

Invalid email

stack:
  in validate_email
  in signup

About

Error handling for Gleam

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors