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 {
Defect(message: String)
Failure(message: String)
}
Constructors
-
Defect(message: String)
-
Failure(message: String)
Functions
pub fn as_defect(
result: Result(a, Nil),
e: String,
) -> Result(a, Problem)
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: String,
) -> Result(a, Problem)
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: String) -> Result(a, Problem)
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: String) -> Result(a, Problem)
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, String),
) -> Result(a, Problem)
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, String),
) -> Result(a, Problem)
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) -> String,
) -> Result(a, Problem)
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) -> String,
) -> Result(a, Problem)
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_message(
outcome: Result(a, Problem),
mapper: fn(String) -> String,
) -> Result(a, Problem)
Map the message inside a Defect or Failure
pub fn pretty_print(problem: Problem) -> 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
Defect: Something went wrong
stack:
c: More context
c: In find user function
d: Something went wrong
pub fn print_line(problem: Problem) -> String
Print problem in one line
Example
Error("Something went wrong")
|> into_defect
|> with_context("In find user function")
|> print_line
Defect: Something went wrong << c: In find user function < d: Something went wrong
pub fn to_defect(
outcome: Result(a, Problem),
) -> Result(a, Problem)
Coherce the error into a Defect
Example
Error("Invalid Input")
|> into_failure
|> to_defect
pub fn to_failure(
outcome: Result(a, Problem),
) -> Result(a, Problem)
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,
default_message: String,
) -> String
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),
context context: String,
) -> Result(a, Problem)
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")