outcome
Types
The error type ie. Result(t, Problem)
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 Problem {
Defect(message: String, stack: Stack)
Failure(message: String, stack: Stack)
}
Constructors
-
Defect(message: String, stack: Stack)
-
Failure(message: String, stack: Stack)
Stack entries Context is just information about the place in the application to build a stack trace.
pub type StackEntry {
StackEntryContext(String)
StackEntryDefect(String)
StackEntryFailure(String)
}
Constructors
-
StackEntryContext(String)
-
StackEntryDefect(String)
-
StackEntryFailure(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_failure("Another failure")
|> pretty_print
Defect: Something went wrong
stack:
Failure: Another failure
Context: In find user function
Defect: 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")