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

Link to this section Functions

Link to this function

convert_error(ma, p) View Source (since 0.1.0)
convert_error(t(a), (any() -> boolean()) | any()) :: t(a)

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
Link to this macro

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
Link to this macro

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

log_error(ma, chardata_or_fun, opts \\ []) View Source (since 0.1.0)
log_error(t(a), String.t() | (any() -> any()), Keyword.t()) :: t(a)

Logs on error, does nothing on success.

Example:

{:error, :not_found}
|> log_error("There was an error", level: :info, metadata: "some meta")
Link to this function

map_error(ma, f) View Source (since 0.1.0)
map_error(t(a), (any() -> any())) :: t(a)

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"}}
Link to this macro

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

normalize_error(x, reason \\ :normalized) View Source (since 0.3.0)
normalize_error(any(), any()) :: t()

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}