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(t, Problem(err)),
context context: String,
) -> Result(t, Problem(err))
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(t, Problem(err)),
mapper: fn(err) -> err,
) -> Result(t, Problem(err))
Map the error value
pub fn new_problem(error: err) -> Problem(err)
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(t, err)) -> Result(t, Problem(err))
Convert Result(a, e)
to Result(a, Problem(e))
Example
Error("Something went wrong")
|> outcome.outcome
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")
|> 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(err),
to_s: fn(err) -> 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(t, Problem(err)),
) -> Result(t, err)
Remove the Problem
wrapping in the error value
Example
let outcome = Error("Fail") |> outcome.outcome
outcome.remove_problem(outcome) == Error("Fail")
pub fn with_context(
error_context: String,
next: fn() -> Result(a, Problem(b)),
) -> Result(a, Problem(b))
Convenient function for adding context at the top of a function that returns an Outcome.
Example
fn do_something(user_id: String) -> Outcome(AnswerType, ErrorType) {
use <- with_context("user_id " <> user_id)
...
}
This is equivalent to adding `|> outcome.context(...)` to each
of the results in the body of the function