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)
Functions
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 extract_error(
outcome: Result(a, Problem(b)),
) -> Result(a, b)
Remove the Problem wrapping, returning just your error.
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_defect(
outcome: Result(a, Problem(b)),
mapper: fn(b) -> b,
) -> Result(a, Problem(b))
pub fn map_error(
outcome: Result(a, Problem(b)),
mapper: fn(b) -> b,
) -> Result(a, Problem(b))
Map the error value inside a Problem
pub fn map_failure(
outcome: Result(a, Problem(b)),
mapper: fn(b) -> b,
) -> Result(a, Problem(b))
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 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:
More context
In find user function
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 << In find user function
pub fn replace_with_defect(
result: Result(a, b),
e: c,
) -> Result(a, Problem(c))
Replaces an Error(t)
with an Error(Defect)
Example
Error(Nil)
|> replace_with_defect("Something went wrong")
pub fn replace_with_failure(
result: Result(a, b),
e: c,
) -> Result(a, Problem(c))
Replaces any Error(t) with an Error(Failure)
Example
Error(Nil)
|> replace_with_failure("Invalid input")
pub fn tap_defect(
outcome: Result(a, Problem(b)),
tap: fn(b) -> Nil,
) -> Result(a, Problem(b))
pub fn tap_error(
outcome: Result(a, Problem(b)),
tap: fn(b) -> Nil,
) -> Result(a, Problem(b))
Use tap functions to log the errors
pub fn tap_failure(
outcome: Result(a, Problem(b)),
tap: fn(b) -> Nil,
) -> Result(a, Problem(b))
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")