monex v0.1.11 MonEx.Result

Result module provides Result type with utility functions.

Link to this section Summary

Types

Result type. ok(x) or error(err) unwraps into {:ok, x} or {:error, err}

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

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

Returns value x if argument is ok(x), raises e if error(e)

5 == unwrap(ok(5))
10 == unwrap(error(:uh_oh), 10)

Link to this section Types

Link to this type

t(res, err)
t(res, err) :: {:ok, res} | {:error, err}

Result type. ok(x) or error(err) unwraps into {:ok, x} or {:error, err}

Link to this section Functions

Link to this function

collect_error(results)
collect_error([t(res, err)]) :: [err] when res: any(), err: any()

Filters collection of results, leaving only errors:

[ok(1), error("oops")] |> collect_error == [error("oops")]
Link to this function

collect_ok(results)
collect_ok([t(res, any())]) :: [res] when res: any()

Filters collection of results, leaving only ok's

[ok(1), error("oops")] |> collect_ok == [ok(1)]
Link to this macro

error(err) (macro)

Link to this function

fallback(arg1, f)
fallback(t(res, err), t(res, err) | (err -> t(res, err))) ::
  t(res, err)
when res: any(), err: any()

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)
Link to this function

is_error(x)
is_error(t(any(), any())) :: boolean()

Returns true if argument is error(), false if ok()

is_error(error("Error")) == true
Link to this function

is_ok(arg)
is_ok(t(any(), any())) :: boolean()

Returns true if argument is ok(), false if error()

is_ok(ok(5)) == true
Link to this macro

ok(res) (macro)

Link to this macro

retry(opts \\ [], list) (macro)

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.

Link to this macro

try_result(mode \\ :full, list) (macro)

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")
Link to this function

unwrap(result, fallback \\ nil)
unwrap(t(res, err), res | (err -> res)) :: res when res: any(), err: any()

Returns value x if argument is ok(x), raises e if error(e)

5 == unwrap(ok(5))
10 == unwrap(error(:uh_oh), 10)