or_error
Types
The sole inhabitant of Error
.
error
is the result of string.inspect
on the error value.
context
is an optional human-readable value for providing information about the error.
pub type ErrorInfo {
ErrorInfo(error: String, context: option.Option(String))
}
Constructors
-
ErrorInfo(error: String, context: option.Option(String))
Values
pub fn all(
or_errors: List(Result(a, ErrorInfo)),
) -> Result(List(a), ErrorInfo)
Combines a list of or_errors into a single or_error.
If all elements in the list are Ok
then returns an Ok
holding the list of values.
If any element is Error
then returns the first error.
pub fn bind(
or_error: Result(a, ErrorInfo),
f: fn(a) -> Result(b, ErrorInfo),
) -> Result(b, ErrorInfo)
The monadic bind function.
Updates a value held within the Ok
constructor by calling the given OrError
-returning function on it.
If the or_error is an Error
rather than Ok
the function is not called and the or_error stays the same.
pub fn bind_() -> fn(Result(a, ErrorInfo)) -> fn(
fn(a) -> Result(b, ErrorInfo),
) -> Result(b, ErrorInfo)
A version of bind
for piping to for use
.
pub fn fail(e: e, context: String) -> Result(a, ErrorInfo)
Allows construction of an Error
value as the ErrorInfo
type.
The error
field is constructed by calling string.inspect
on the passed error value.
The optional context
field is to allow developers to provide information on the error occurred.
If the empty string is passed, the context field will be set to None
.
pub fn flatten(
result: Result(Result(a, ErrorInfo), ErrorInfo),
) -> Result(a, ErrorInfo)
Merges a nested OrError
into a single layer.
pub fn is_error(or_error: Result(a, ErrorInfo)) -> Bool
Checks whether the or_error is an Error
value.
pub fn is_ok(or_error: Result(a, ErrorInfo)) -> Bool
Checks whether the or_error is an Ok
value.
pub fn lazy_or(
first: Result(a, ErrorInfo),
second: fn() -> Result(a, ErrorInfo),
) -> Result(a, ErrorInfo)
Returns the first value if it is Ok
, otherwise evaluates the given function for a fallback value.
pub fn lazy_unwrap(
or_error: Result(a, ErrorInfo),
default: fn() -> a,
) -> a
Extracts the Ok
value from an or_error, evaluating the default function if the or_error is an Error
.
pub fn map(
or_error: Result(a, ErrorInfo),
f: fn(a) -> b,
) -> Result(b, ErrorInfo)
The monadic map function.
Updates a value held within the Ok
constructor by calling the given function on it.
If the or_error is an Error
rather than Ok
the function is not called and the or_error stays the same.
pub fn map_() -> fn(Result(a, ErrorInfo)) -> fn(fn(a) -> b) -> Result(
b,
ErrorInfo,
)
A version of map
for piping to for use
.
pub fn of_result(
result: Result(a, e),
context: String,
) -> Result(a, ErrorInfo)
A helper function to allow easy construction of OrError
from a Result
.
If the result is an Error
, the ErrorInfo
will be construced by fail
.
pub fn or(
first: Result(a, ErrorInfo),
second: Result(a, ErrorInfo),
) -> Result(a, ErrorInfo)
Returns the first value if it is Ok, otherwise returns the second value.
pub fn partition(
or_errors: List(Result(a, ErrorInfo)),
) -> #(List(a), List(ErrorInfo))
Given a list of or_errors, returns a pair where the first element is a list
of all the values inside Ok
and the second element is a list with all the
values inside Error
.
The values in both lists appear in reverse order with respect to their position in the original list of or_errors.
pub fn pretty_print(
or_error: Result(a, ErrorInfo),
using: fn(a) -> String,
) -> String
A function to prettily print the or_error.
pub fn replace(
or_error: Result(a, ErrorInfo),
value: b,
) -> Result(b, ErrorInfo)
Replace the value inside Ok
. Does nothing if the or_error is an Error
.
pub fn return(a: a) -> Result(a, ErrorInfo)
The monad return function.
Allows construction of an Ok
value.
pub fn unwrap(or_error: Result(a, ErrorInfo), default: a) -> a
Extracts the Ok
value from an or_error, returning a default value if the or_error is an Error
.
pub fn unwrap_panic(or_error: Result(a, ErrorInfo)) -> a
Extracts the Ok
value from an or_error, panicing if the or_error is an Error
.