ProductPromotion
Logo

Elixir

made by https://0x3d.site

GitHub - MikaAK/elixir_error_message: Elixir error messages to make the errors within the system easier to expect and predict, as well as read
Elixir error messages to make the errors within the system easier to expect and predict, as well as read  - GitHub - MikaAK/elixir_error_message: Elixir error messages to make the errors within the...
Visit Site

GitHub - MikaAK/elixir_error_message: Elixir error messages to make the errors within the system easier to expect and predict, as well as read

GitHub - MikaAK/elixir_error_message: Elixir error messages to make the errors within the system easier to expect and predict, as well as read

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

Articles
to learn more about the elixir concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about Elixir.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory