outcome

Types

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. Context is just information about the place in the application to build a stack trace.

pub type Error {
  Context(String)
  Defect(String)
  Failure(String)
}

Constructors

  • Context(String)
  • Defect(String)
  • Failure(String)

The error type. This is a stack that stores the latest problem and a list of previous problems.

pub type ErrorStack {
  ErrorStack(problem: Error, problems: List(Error))
}

Constructors

  • ErrorStack(problem: Error, problems: List(Error))

Alias to Result with ErrorStack as error type.

pub type Outcome(t) =
  Result(t, ErrorStack)

Functions

pub fn add_to_stack(
  stack: ErrorStack,
  new_problem: Error,
) -> ErrorStack
pub fn as_defect(
  result: Result(a, Nil),
  e: String,
) -> Result(a, ErrorStack)

Replaces an Error with a wrapped Defect

pub fn as_failure(
  result: Result(a, Nil),
  e: String,
) -> Result(a, ErrorStack)

Replaces an Error with a wrapped Failure

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

Context is not the same as the error Add context to an Outcome

pub fn defect(defect: String) -> Result(a, ErrorStack)

Create a wrapped Defect

pub fn failure(failure: String) -> Result(a, ErrorStack)

Create a wrapped Failure

pub fn into_defect(
  result: Result(a, String),
) -> Result(a, ErrorStack)

Convert an Error into a wrapped Defect

pub fn into_failure(
  result: Result(a, String),
) -> Result(a, ErrorStack)

Convert an Error into a wrapped Failure

pub fn map_into_defect(
  result: Result(a, b),
  mapper: fn(b) -> String,
) -> Result(a, ErrorStack)

Convert an Error into a wrapped Defect, by using a mapping function

pub fn stack_to_problems(stack: ErrorStack) -> List(Error)

Get a list of problems for a ErrorStack

pub fn unwrap_failure(
  stack: ErrorStack,
  default: String,
) -> String

Get the failure at the top of the stack. If the top is not a failure, then return the given default. Use this for showing an error message to users.

Search Document