MikaAK / elixir_error_message

Elixir error messages to make the errors within the system easier to expect and predict, as well as read

Home Page:https://learn-elixir.dev/blogs/safer-error-systems-in-elixir

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ErrorMessage

Coverage Test Dialyzer Credo codecov 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"}
  ]
end

Documentation can be found at https://hexdocs.pm/error_message.

Usage Example

iex> id = 1
iex> ErrorMessage.not_found("no user with id #{id}", %{user_id: id})
%ErrorMessage{
  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
...> })
%ErrorMessage{
  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"})
end

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)
    end
  end

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

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}")
    Logger.warn(to_string(e))

    res
end

About

Elixir error messages to make the errors within the system easier to expect and predict, as well as read

https://learn-elixir.dev/blogs/safer-error-systems-in-elixir

License:MIT License


Languages

Language:Elixir 100.0%