https://github.com/MikaAK/elixir_error_message
Elixir error messages to make the errors within the system easier to expect and predict, as well as read
https://github.com/MikaAK/elixir_error_message
Last synced: 5 months ago
JSON representation
Elixir error messages to make the errors within the system easier to expect and predict, as well as read
- Host: GitHub
- URL: https://github.com/MikaAK/elixir_error_message
- Owner: MikaAK
- License: mit
- Created: 2021-11-05T06:09:53.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-09-28T10:10:28.000Z (over 1 year ago)
- Last Synced: 2024-11-17T20:10:38.432Z (5 months ago)
- Language: Elixir
- Homepage: https://learn-elixir.dev/blogs/safer-error-systems-in-elixir
- Size: 69.3 KB
- Stars: 33
- Watchers: 1
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-elixir - elixir_error_message - Simple error helpers to make errors in your system predictable and easy to render to JSON or in logs. (Errors and Exception Handling)
README
ErrorMessage
===
[](https://github.com/MikaAK/elixir_error_message/actions/workflows/coverage.yml)
[](https://github.com/MikaAK/elixir_error_message/actions/workflows/test.yml)
[](https://github.com/MikaAK/elixir_error_message/actions/workflows/dialyzer.yml)
[](https://github.com/MikaAK/elixir_error_message/actions/workflows/credo.yml)
[](https://codecov.io/gh/MikaAK/elixir_error_message)
[](https://hex.pm/packages/error_message)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 baseThis 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`:
```elixir
def deps do
[
{:error_message, "~> 0.2.0"}
]
end
```Documentation can be found at [https://hexdocs.pm/error_message](https://hexdocs.pm/error_message).
### Usage Example
```elixir
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 systemFor 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```elixir
# 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:```elixir
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
enddefp 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```elixir
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```