outcome
Types
A list of contexts
pub type ContextStack =
List(String)
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 Error(err) {
Defect(value: err)
Failure(value: err)
}
Constructors
-
Defect(value: err)
-
Failure(value: err)
Alias to Result with Problem as error type.
pub type Outcome(t, err) =
Result(t, Problem(err))
Functions
pub fn as_defect(
result: Result(a, Nil),
e: b,
) -> Result(a, Problem(b))
Replaces an Error(Nil)
with an Error(Defect)
This is useful when you have a Result(t, Nil)
and
want to convert it into a Result(t, Problem)
Example
Error(Nil)
|> as_defect("Something went wrong")
pub fn as_failure(
result: Result(a, Nil),
e: b,
) -> Result(a, Problem(b))
Replaces an Error(Nil) with an Error(Failure)
This is useful when you have a Result(t, Nil)
and
want to convert it into a Result(t, Problem)
Example
Error(Nil)
|> as_failure("Invalid input")
pub fn error_with_defect(defect: a) -> Result(b, Problem(a))
Create a Defect wrapped in an Error
Example
case something {
True -> Ok("Yay")
False -> error_with_defect("Something went wrong")
}
pub fn error_with_failure(failure: a) -> Result(b, Problem(a))
Create Failure wrapped in an Error
Example
case something {
True -> Ok("Yay")
False -> error_with_failure("Invalid input")
}
pub fn into_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")
|> into_defect
pub fn into_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")
|> into_failure
pub fn map_into_defect(
result: Result(a, b),
mapper: fn(b) -> c,
) -> Result(a, Problem(c))
Convert an Error(t)
into a wrapped Defect, by using a mapping function
Similar to into_defect, but takes a function to map the error value to a string
pub fn map_into_failure(
result: Result(a, b),
mapper: fn(b) -> c,
) -> Result(a, Problem(c))
Convert an Error(t)
into a wrapped Failure, by using a mapping function
Similar to into_defect, but takes a function to map the error value to a string
pub fn map_value(
outcome: Result(a, Problem(b)),
mapper: fn(b) -> b,
) -> Result(a, Problem(b))
Map the value inside a Defect or Failure
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")
|> into_defect
|> with_context("In find user function")
|> with_context("More context")
|> pretty_print(function.identity)
Defect: Something went wrong
stack:
c: More context
c: In find user function
d: Something went wrong
pub fn print_line(
problem: Problem(a),
to_s: fn(a) -> String,
) -> String
Print problem in one line
Example
Error("Something went wrong")
|> into_defect
|> with_context("In find user function")
|> print_line(function.identity)
Defect: Something went wrong << c: In find user function < d: Something went wrong
pub fn to_defect(
outcome: Result(a, Problem(b)),
) -> Result(a, Problem(b))
Coherce the error into a Defect
Example
Error("Invalid Input")
|> into_failure
|> to_defect
pub fn to_failure(
outcome: Result(a, Problem(b)),
) -> Result(a, Problem(b))
Coherce the error into a Failure. The original entry in the stack remains unchanged.
Example
Error("Invalid Input")
|> into_defect
|> to_failure
pub fn unwrap_failure(problem: Problem(a), default_value: a) -> a
Use this to show a failure to a user. If the Problem is a failure, it will show that otherwise it will show the default message given. We don’t want to show defect messages to users.
Example
case result {
Ok(value) -> io.debug("Success")
Error(problem) -> io.error(unwrap_failure(problem, "Something went wrong"))
}
pub fn with_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")
|> into_defect
|> with_context("In find user function")