Exordia v0.8.2 Exordia View Source

Exordia defines some convenience macros/functions.

Add use Exordia to modules in which you would like to use them.

Link to this section Summary

Functions

Similar to the ~> operator but passes non-errors through and calls the function on error values unless the :handled key of the Exordia.Error struct is true

Converts an :error, {:error, r}, or {:error, r, d} tuple into an Exordia.Error.t() struct. Any othre value is passed through unmodified

Like exordize/1 but pipes the value into the provided function

Converts value to {:ok, value}

Converts value to {:ok, value} and pipes this as an argument into the provided function. Useful when you want to pipe an :ok tuple using ~> (which automatically unwraps these tuples)

Rescues exceptions, converting them into Exordia.Error.t() structs

Rescues exceptions, converting them into Exordia.Error.t() structs. Passes the first argument into the second argument’s function call making it useful for piping

Passes the argument to the provided function, ignoring the return value of the function and returning the first argument, making it useful for piping

Passes the argument to the provided function. Returns the first argument unless the function returns an error in which case the error is returned, making it useful for piping

Converts an Exordia.Error.t() struct into an :error, {:error, r}, or {:error, r, d} tuple. Any other value is passed through unmodified

Like un_exordize/1 but pipes the value into the provided function

Pipe-like operator which converts :error or error tuples to Exordia.Error structs, skips to the end of the chain on errors, and passes only value when it encounters {:ok, value}

Link to this section Functions

Link to this macro arg <~ ast View Source (macro)
any() <~ {atom(), [{atom(), any()}], [...] | nil} ::
  {:case, [], [[{any(), any()}, ...] | {:exordize, [], [any(), ...]}, ...]}

Similar to the ~> operator but passes non-errors through and calls the function on error values unless the :handled key of the Exordia.Error struct is true.

Examples

iex> 1 <~ Tuple.to_list()
  1

  iex> {:ok, 1} <~ Tuple.to_list(_)
  1

    iex> :error <~ Kernel.inspect()
    "%Exordia.Error{handled: false, meta: nil, reason: nil}"
Link to this function exordize(value) View Source
exordize(x | {:ok, x}) :: x | Exordia.Error.t() when x: any()

Converts an :error, {:error, r}, or {:error, r, d} tuple into an Exordia.Error.t() struct. Any othre value is passed through unmodified.

Link to this macro exordize(arg, fla) View Source (macro)
exordize(any(), {atom(), [{atom(), any()}], [...] | nil}) ::
  {:exordize, [], [{any(), any(), [any(), ...]}, ...]}

Like exordize/1 but pipes the value into the provided function.

Link to this macro ok_ify(arg) View Source (macro)
ok_ify(any()) :: {:ok, any()}

Converts value to {:ok, value}.

Link to this macro ok_ify(arg, fla) View Source (macro)
ok_ify(any(), {atom(), [{atom(), any()}], [...] | nil}) ::
  {:exordize, [], [{any(), any(), [any(), ...]}, ...]}

Converts value to {:ok, value} and pipes this as an argument into the provided function. Useful when you want to pipe an :ok tuple using ~> (which automatically unwraps these tuples).

Link to this macro safe(arg) View Source (macro)
safe(any()) :: {:try, [], [[{any(), any()}, ...], ...]}

Rescues exceptions, converting them into Exordia.Error.t() structs.

Examples

iex> safe(div(10, 5))
  2

  iex> safe(div(10, 0))
  %Exordia.Error{
    handled: false,
    meta: nil,
    reason: %ArithmeticError{message: "bad argument in arithmetic expression"}
  }
Link to this macro safe(arg, ast) View Source (macro)
safe(any(), {atom(), [{atom(), any()}], [...] | nil}) ::
  {:try, [], [[{any(), any()}, ...], ...]}

Rescues exceptions, converting them into Exordia.Error.t() structs. Passes the first argument into the second argument’s function call making it useful for piping.

Examples

iex> 10 |> safe(div(5))
  2

  iex> 10 |> safe(div(0))
  %Exordia.Error{
    handled: false,
    meta: nil,
    reason: %ArithmeticError{message: "bad argument in arithmetic expression"}
  }
Link to this macro tee(arg, ast) View Source (macro)
tee(any(), {atom(), [{atom(), any()}], [...] | nil}) ::
  {:__block__, [], [any(), ...]}

Passes the argument to the provided function, ignoring the return value of the function and returning the first argument, making it useful for piping.

Link to this macro terror(arg, ast) View Source (macro)
terror(any(), {atom(), [{atom(), any()}], [...] | nil}) ::
  {:case, [], [[{any(), any()}, ...] | {:exordize, [], [any(), ...]}, ...]}

Passes the argument to the provided function. Returns the first argument unless the function returns an error in which case the error is returned, making it useful for piping.

Link to this function un_exordize(r) View Source
un_exordize(any()) :: any()

Converts an Exordia.Error.t() struct into an :error, {:error, r}, or {:error, r, d} tuple. Any other value is passed through unmodified.

Link to this macro un_exordize(arg, fla) View Source (macro)
un_exordize(any(), {atom(), [{atom(), any()}], [...] | nil}) ::
  {any(), any(), [any(), ...]}

Like un_exordize/1 but pipes the value into the provided function.

Link to this macro arg ~> ast View Source (macro)
any() ~> {atom(), [{atom(), any()}], [...] | nil} ::
  {:case, [], [[{any(), any()}, ...] | {:exordize, [], [any(), ...]}, ...]}

Pipe-like operator which converts :error or error tuples to Exordia.Error structs, skips to the end of the chain on errors, and passes only value when it encounters {:ok, value}.

An underscore can be used to pipe the argument to an arbitrary position.

Examples

iex> 1 ~> Kernel.to_string()
  "1"

  iex> {:ok, 1} ~> Kernel.to_string(_)
  "1"

  iex> {:error, :something_bad} ~> Kernel.to_string()
  %Exordia.Error{handled: false, meta: nil, reason: :something_bad}