Rat Error v0.0.1 RatError View Source

Rat Error

Provides helper functions for error handling:

  • detailed error description.
  • default and configurable error fields.

Installation

def deps do
  [
    {:rat_error, "~> 0.0.1"}
  ]
end

Configuration

config :rat_error, RatError.Structure,
  # Node Name, the default value is 'error'.
  #
  # If node is 'err', the structure is,
  #
  # %{
  #   err:
  #   %{
  #     code:     :invalid_argument,
  #     file:     "/home/dummy/my_app/web/user_controller.ex",
  #     function: {:authenticate, 1},
  #     line:     123,
  #     message:  "wrong token!",
  #     module:   Elixir.MyApp.Registration.UserController
  #   }
  # }
  #
  # If node is nil or an empty string, the node is removed. The fields are
  # exposed outside, and the below configuration 'prefix' could be set to
  # distinguish with other caller's keys.
  node: :error,

  # Field Prefix, the default value is nil (NO prefix).
  #
  # If node is nil and prefix is 'err_', the structure is,
  #
  # %{
  #   err_code:     :invalid_argument,
  #   err_file:     "/home/dummy/my_app/web/user_controller.ex",
  #   err_function: {:authenticate, 1},
  #   err_line:     123,
  #   err_message:  "wrong token!",
  #   err_module:   Elixir.MyApp.Registration.UserController
  # }
  #
  prefix: nil,

  # Support Keys
  keys:
  [
    # Error code defined by caller, e.g. an atom :no_entry, an integer 9 or a
    # string "unexpected".
    :code,

    # Error file path.
    :file,

    # Error function name.
    :function,

    # Error file line.
    :line,

    # Error message of string passed in by caller.
    :message,

    # Error module.
    :module
  ]

Link to this section Summary

Functions

Retrieves a RAT error with the specified parameters

Link to this section Functions

Link to this macro rat_error(error_code \\ nil, error_message \\ "", opts \\ []) View Source (macro)

Retrieves a RAT error with the specified parameters.

This function is the main API of the plugin.

Parameters ‘error_code’ and ‘error_message’ correspond to the support keys :code and :message (see ‘config/*.exs’ for detail). Both could be ignored by using the default values if keys :code and :message are disabled.

Parameter ‘opts’ corresponds the Keyword configuration of ‘RatError.Structure’ (see ‘config/*.exs’ for detail). If this parameter is passed in, it merges with the configuration of ‘RatError.Structure’.

Examples

Parameter ‘opts’ merges with the configuration in ‘config/test.exs’.

iex> opts = [keys: [:code, :message]]
iex> rat_error(:wrong_password, "Wrong password!", opts)
%{error: %{code: :wrong_password, message: "Wrong password!"}}