Brex.Result v0.4.0 Brex.Result.Helpers View Source
Tools for modifying the reason in error
tuples.
Link to this section Summary
Functions
Converts a matching error to :ok
An error matches if the reason is equal to the supplied atom or the reason passes the predicate.
Leaves success and other errors unchanged.
Converts a matching error to a success with the given value or function. An error matches if the reason is equal to the supplied atom or the reason passes the predicate. Leaves success and other errors unchanged. Lazy. Only evaluates the second argument if necessary.
Lifts a value into a success tuple unless:
1) the value matches the second argument
2) when applied to the value, the second argument function returns true
Logs on error
, does nothing on success.
Applies the function to the reason in an error
tuple.
Leaves success unchanged.
Replaces the reason in an error
tuple.
Leaves success unchanged.
Lazy. Only evaluates the second argument if necessary.
Wraps a naked :error
atom in a tuple with the given reason.
Leaves success values and error
tuples unchanged.
Link to this section Types
p()
View Source
p() :: Brex.Result.Base.p()
p() :: Brex.Result.Base.p()
s()
View Source
s() :: Brex.Result.Base.s()
s() :: Brex.Result.Base.s()
s(x)
View Source
s(x) :: Brex.Result.Base.s(x)
s(x) :: Brex.Result.Base.s(x)
t()
View Source
t() :: Brex.Result.Base.t()
t() :: Brex.Result.Base.t()
t(x)
View Source
t(x) :: Brex.Result.Base.t(x)
t(x) :: Brex.Result.Base.t(x)
Link to this section Functions
convert_error(ma, p) View Source (since 0.1.0)
Converts a matching error to :ok
An error matches if the reason is equal to the supplied atom or the reason passes the predicate.
Leaves success and other errors unchanged.
Examples:
iex> {:error, :already_completed}
...> |> convert_error(:already_completed)
:ok
iex> {:error, :already_completed}
...> |> convert_error(fn r -> r == :already_completed end)
:ok
convert_error(ma, p, f) View Source (macro) (since 0.1.0)
Converts a matching error to a success with the given value or function. An error matches if the reason is equal to the supplied atom or the reason passes the predicate. Leaves success and other errors unchanged. Lazy. Only evaluates the second argument if necessary.
Examples:
iex> {:error, :already_completed}
...> |> convert_error(:already_completed, "submitted")
{:ok, "submitted"}
iex> {:error, "test"}
...> |> convert_error(&(&1 == "test"), fn r -> {:ok, r <> "ed"} end)
{:ok, "tested"}
Typespec:
convert_error(t(), (any -> boolean) | any, b | (any -> t(b))) :: t(b) when b: var
lift(val, p, f) View Source (macro) (since 0.1.0)
Lifts a value into a success tuple unless:
1) the value matches the second argument
2) when applied to the value, the second argument function returns true
In those cases an error
tuple is returned with either
1) the third argument as the reason
2) the third argument function applied to the value, as the reason
lift/3
is lazy, this means third argument will only be evaluated when necessary.
Examples:
iex> nil
...> |> lift(nil, :not_found)
{:error, :not_found}
iex> 2
...> |> lift(nil, :not_found)
{:ok, 2}
iex> "test"
...> |> lift(&(&1 == "test"), fn x -> {:oops, x <> "ed"} end)
{:error, {:oops, "tested"}}
Typespec:
lift(a | b, b | (a | b -> boolean), c | (a | b -> c)) :: s(a) when a: var, b: var, c: var
log_error(ma, chardata_or_fun, opts \\ []) View Source (since 0.1.0)
Logs on error
, does nothing on success.
Example:
{:error, :not_found}
|> log_error("There was an error", level: :info, metadata: "some meta")
map_error(ma, f) View Source (since 0.1.0)
Applies the function to the reason in an error
tuple.
Leaves success unchanged.
Example:
iex> account_name = "test"
...> {:error, :not_found}
...> |> map_error(fn r -> {r, account_name} end)
{:error, {:not_found, "test"}}
mask_error(ma, term) View Source (macro) (since 0.1.0)
Replaces the reason in an error
tuple.
Leaves success unchanged.
Lazy. Only evaluates the second argument if necessary.
Example:
iex> account_name = "test"
...> {:error, :not_found}
...> |> mask_error({:nonexistent_account, account_name})
{:error, {:nonexistent_account, "test"}}
Typespec:
mask_error(t(a), any) :: t(a) when a: var
normalize_error(x, reason \\ :normalized) View Source (since 0.3.0)
Wraps a naked :error
atom in a tuple with the given reason.
Leaves success values and error
tuples unchanged.
Examples:
iex> :error
...> |> normalize_error(:parsing_failure)
{:error, :parsing_failure}
iex> {:ok, 2}
...> |> normalize_error()
{:ok, 2}