[![Hex pm](](

This library exists to simplify error systems in a code base
and allow for a simple unified experience when using and reading
error messages around the code base

This creates one standard, that all errors should fit into the context
of HTTP error codes, if they don't `:internal_server_error` should
be used and you can use the message and details to provide a further
level of depth

### Installation

The package can be installed by adding `error_message` to your list of dependencies in `mix.exs`:

def deps do
    {:error_message, "~> 0.2.0"}

Documentation can be found at [](

### Usage Example

iex> id = 1
iex> ErrorMessage.not_found("no user with id #{id}", %{user_id: id})
  code: :not_found,
  message: "no user with id 1",
  details: %{user_id: 1}

iex> ErrorMessage.internal_server_error("critical internal error", %{
...>   reason: :massive_issue_with_x
...> })
  code: :internal_server_error,
  message: "critical internal error",
  details: %{reason: :massive_issue_with_x}

### Why is this important
If we want to have a good way to catch errors around our system as well as be able to
display errors that happen throughout our system, it's useful to have a common error
api so we can predict what will come out of a system

For example if we used elixir through our server, we would be able to catch a not found
pretty easily since we can predict the error code coming in without needing to
know the message. This leads to more resilliant code since message changes won't break
the functionality of your application

# Because our error system is setup with `find_user` we can easily
# catch no users and have a solid backup plan
with {:error, %ErrorMessage{code: :not_found}} <- find_user(%{name: "bill"}) do
  create_user(%{name: "bill"})

### Usage with Phoenix
Another benefit is error rendering to the frontend, because all our errors are part of
the HTTP error system, it's quite easy to now return the proper status codes and messages
to our frontend clients. For example:

defmodule MyController do
  def index(conn, param) do
    case find_thing(params) do
      {:ok, res} -> json(conn, res)
      {:error, e} -> json_error(conn, e)

  defp json_error(conn, %ErrorMessage{code: code} = e) do
      |> put_status(code) # Plug.Conn
      |> json(ErrorMessage.to_jsonable_map(e))

*This will also add a request_id from `Logger.metadata[:request_id]` when found*

### Usage with Logger
Ontop of being useful for Phoenix we can also find some good use from this
system and Logger, since `ErrorMessage` implements `String.Chars` protocol

case do_thing() do
  {:ok, value} -> {:ok, do_other_thing(value)}
  {:error, e} = res ->
    Logger.error("[MyModule] \#{e}")