monex v0.1.10 MonEx.Result
Result module provides Result type with utility functions.
Summary
Functions
Filters collection of results, leaving only errors:
[ok(1), error("oops")] |> collect_error == [error("oops")]
Filters collection of results, leaving only ok’s
[ok(1), error("oops")] |> collect_ok == [ok(1)]
Returns monad if it is ok(), or evaluates supplied lambda that expected
to return another result. Returns supplied fallback value, if it’s not a function.
ok(5) |> fallback(fn _ -> 1 end) == ok(5)
error("WTF") |> fallback(fn m -> ok("#{m}LOL") end) == ok("WTFLOL")
error("WTF") |> fallback(ok(5)) == ok(5)
error("WTF") |> fallback(5) == ok(5)
Returns true if argument is error(), false if ok()
is_error(error("Error")) == true
Returns true if argument is ok(), false if error()
is_ok(ok(5)) == true
Returns value x if argument is ok(x), raises e if error(e)
5 == unwrap(ok(5))
10 == unwrap(error(:uh_oh), 10)
Macros
Retry in case of error
Wraps expression and returns exception wrapped into error() if it happens,
otherwise ok(result of expression), in case if expression returns result
type, it won’t be wrapped
Types
Functions
Filters collection of results, leaving only errors:
[ok(1), error("oops")] |> collect_error == [error("oops")]
Filters collection of results, leaving only ok’s
[ok(1), error("oops")] |> collect_ok == [ok(1)]
Returns monad if it is ok(), or evaluates supplied lambda that expected
to return another result. Returns supplied fallback value, if it’s not a function.
ok(5) |> fallback(fn _ -> 1 end) == ok(5)
error("WTF") |> fallback(fn m -> ok("#{m}LOL") end) == ok("WTFLOL")
error("WTF") |> fallback(ok(5)) == ok(5)
error("WTF") |> fallback(5) == ok(5)
Returns true if argument is error(), false if ok()
is_error(error("Error")) == true
Returns true if argument is ok(), false if error()
is_ok(ok(5)) == true
Returns value x if argument is ok(x), raises e if error(e)
5 == unwrap(ok(5))
10 == unwrap(error(:uh_oh), 10)
Macros
Retry in case of error.
Possible options:
:n- times to retry:delay— delay between retries
##Example
result = retry n: 3, delay: 3000 do
remote_service()
end
This will call remove_service() 4 times (1 time + 3 retries) with an interval of 3 seconds.
Wraps expression and returns exception wrapped into error() if it happens,
otherwise ok(result of expression), in case if expression returns result
type, it won’t be wrapped.
Possible modes:
:full- returns exception struct intact (default):message— returns error message only:module— returns error module only
##Example
try_result do
5 + 5
end == ok(10)
try_result do
5 / 0
end == error(%ArithmeticError{message: "bad argument in arithmetic expression"})
try_result :message do
5 / 0
end == error("bad argument in arithmetic expression")