problem

Types

A list of context entries

pub type ContextStack =
  List(String)

The error type in a result ie. Result(t, Problem(e))

pub type Problem(err) {
  Problem(error: err, stack: List(String))
}

Constructors

  • Problem(error: err, stack: List(String))

Values

pub fn context(
  result result: Result(t, Problem(err)),
  context context: String,
) -> Result(t, Problem(err))

Add context to an Error. This will add a context entry to the stack.

Example

Error("Something went wrong")
|> problem.context("In find_user function")
pub fn map_error(
  result: Result(t, Problem(err)),
  mapper: fn(err) -> err,
) -> Result(t, Problem(err))

Map the error value

pub fn pretty_print(
  problem: Problem(err),
  to_s: fn(err) -> String,
) -> String

Pretty print a Problem, including the stack. The latest problem appears at the top of the stack.

Example

let result = Error("Something went wrong")
|> problem.wrap
|> problem.context("In find user function")
|> problem.context("More context")

case result {
  Error(problem) ->
    problem.pretty_print(function.identity)

  Ok(_) -> todo
}
Something went wrong

stack:
 In find user function
 More context
pub fn print_line(
  problem: Problem(err),
  to_s: fn(err) -> String,
) -> String

Print problem in one line

Example

let result = Error("Something went wrong")
|> problem.outcome
|> problem.context("In find user function")

case result {
  Error(problem) ->
    problem.print_line(function.identity)

  Ok(_) -> todo
}
Something went wrong < In find user function
pub fn unwrap(outcome: Result(t, Problem(err))) -> Result(t, err)

Remove the Problem wrapping from the error value

pub fn with_context(
  error_context: String,
  next: fn() -> Result(t, Problem(err)),
) -> Result(t, Problem(err))

Convenient function for adding context at the top of a function that returns a Result(a, Problem(e)).

Example

fn do_something(user_id: String) -> Result(AnswerType, Problem(ErrorType)) {
  use <- problem.with_context("user_id " <> user_id)

  ...
}

This is equivalent to adding `|> problem.context(...)` to each
of the results in the body of the function
pub fn wrap(result: Result(t, err)) -> Result(t, Problem(err))

Convert Result(a, e) to Result(a, Problem(e))

Example

Error("Something went wrong")
|> problem.wrap
Search Document