outcome
Types
A list of context entries
pub type ContextStack =
List(String)
An alias for the Result
type. Where the error type is Problem
.
pub type Outcome(t, err) =
Result(t, Problem(err))
Values
pub fn context(
outcome outcome: Result(a, Problem(b)),
context context: String,
) -> Result(a, Problem(b))
Add context to a Problem. This will add a context entry to the stack.
Example
Error("Something went wrong")
|> outcome.outcome
|> outcome.context("In find user function")
pub fn map_error(
outcome: Result(a, Problem(b)),
mapper: fn(b) -> b,
) -> Result(a, Problem(b))
Map the error value
pub fn new_problem(error: a) -> Problem(a)
Create a Problem
Use this if you need the Problem
type only.
Usually you will use outcome
instead.
Example
outcome.new_problem("Something went wrong")
pub fn outcome(result: Result(a, b)) -> Result(a, Problem(b))
Convert Result(a, e)
to Result(a, Problem(e))
Example
Error("Something went wrong")
|> outcome.outcome
pub fn pretty_print(
problem: Problem(a),
to_s: fn(a) -> 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")
|> outcome.outcome
|> outcome.context("In find user function")
|> outcome.context("More context")
case result {
Error(problem) ->
outcome.pretty_print(function.identity)
Ok(_) -> todo
}
Something went wrong
stack:
In find user function
More context
pub fn print_line(
problem: Problem(a),
to_s: fn(a) -> String,
) -> String
Print problem in one line
Example
let result = Error("Something went wrong")
|> outcome.outcome
|> outcome.context("In find user function")
case result {
Error(problem) ->
outcome.print_line(function.identity)
Ok(_) -> todo
}
Something went wrong < In find user function
pub fn remove_problem(
outcome: Result(a, Problem(b)),
) -> Result(a, b)
Remove the Problem
wrapping in the error value
Example
let outcome = Error("Fail") |> outcome.outcome
outcome.remove_problem(outcome) == Error("Fail")