outcome

Types

A list of context entries

pub type ContextStack =
  List(String)

Alias to Result with Problem as error type.

pub type Outcome(t, err) =
  Result(t, Problem(err))

The error type ie. Result(t, Problem) This contains the error, the severity and the context stack.

pub type Problem(err) {
  Problem(error: err, severity: Severity, stack: ContextStack)
}

Constructors

  • Problem(error: err, severity: Severity, stack: ContextStack)

An application error is either a Defect or a Failure. A Defect is an unexpected application error, which shouldn’t be shown to the user. A Failure is an expected error.

pub type Severity {
  Defect
  Failure
}

Constructors

  • Defect
  • Failure

Functions

pub fn context(
  outcome outcome: Result(a, Problem(b)),
  context context: String,
) -> Result(a, Problem(b))

Add context to an Outcome This will add a Context entry to the stack

Example

Error("Something went wrong")
|> result_to_defect
|> context("In find user function")
pub fn get_failure_in_problem(
  problem: Problem(a),
  default_value: a,
) -> a

Use this to show a failure to a user. Extracts the Error value from a Problem when the severity is Failure. otherwise it will return the default value given.

Example

case result {
 Ok(value) -> io.debug("Success")
 Error(problem) -> io.error(get_failure_in_problem(problem, "Something went wrong"))
}
pub fn map_error(
  outcome: Result(a, Problem(b)),
  mapper: fn(b) -> b,
) -> Result(a, Problem(b))

Map the error value

pub fn new_defect(error: a) -> Problem(a)

Create a Defect Use this if you need the Problem type only. Usually you will use result_to_defect instead.

Example

new_defect("Something went wrong")
pub fn new_failure(error: a) -> Problem(a)

Create a Failure Use this if you need the Problem type only. Usually you will use result_to_failure instead.

Example

new_failure("Something went wrong")
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

Error("Something went wrong")
|> result_to_defect
|> context("In find user function")
|> context("More context")
|> pretty_print(function.identity)
Defect: 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

Error("Something went wrong")
|> result_to_defect
|> context("In find user function")
|> print_line(function.identity)
Defect: Something went wrong << In find user function
pub fn result_to_defect(
  result: Result(a, b),
) -> Result(a, Problem(b))

Convert an Error(String) into an Error(Defect) This is useful when you have a Result(t, String) and want to convert it into a Result(t, Problem)

Example

Error("Something went wrong")
|> result_to_defect
pub fn result_to_failure(
  result: Result(a, b),
) -> Result(a, Problem(b))

Convert an Error(String) into an Error(Failure) This is useful when you have a Result(t, String) and want to convert it into a Result(t, Problem)

Example

Error("Invalid input")
|> result_to_failure
pub fn tap(
  outcome: Result(a, Problem(b)),
  fun: fn(Problem(b)) -> c,
) -> Result(a, Problem(b))

Use tap functions to log the errors This yields the Problem type.

pub fn tap_defect(
  outcome: Result(a, Problem(b)),
  fun: fn(b) -> c,
) -> Result(a, Problem(b))

Yield your error type Only called if the severity is Defect

pub fn tap_error(
  outcome: Result(a, Problem(b)),
  fun: fn(b) -> c,
) -> Result(a, Problem(b))

This yields your error type.

pub fn tap_failure(
  outcome: Result(a, Problem(b)),
  fun: fn(b) -> c,
) -> Result(a, Problem(b))

Yield your error type Only called if the severity is Failure

pub fn to_simple_result(
  outcome: Result(a, Problem(b)),
) -> Result(a, b)

Remove the Problem wrapping, returning just your error.

Example

let outcome = Error("Fail") |> result_to_defect

to_simple_result(outcome) == Error("Fail")
Search Document