ErrorMessage (error_message v0.3.2) View Source

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

Link to this section Summary

Functions

Create bad_gateway error message for status code 502

Create bad_gateway error message for status code 502 with a details item which is passed in as the details key under the ErrorMessage struct

Create bad_request error message for status code 400

Create bad_request error message for status code 400 with a details item which is passed in as the details key under the ErrorMessage struct

Create conflict error message for status code 409

Create conflict error message for status code 409 with a details item which is passed in as the details key under the ErrorMessage struct

Create expectation_failed error message for status code 417

Create expectation_failed error message for status code 417 with a details item which is passed in as the details key under the ErrorMessage struct

Create failed_dependency error message for status code 424

Create failed_dependency error message for status code 424 with a details item which is passed in as the details key under the ErrorMessage struct

Create forbidden error message for status code 403

Create forbidden error message for status code 403 with a details item which is passed in as the details key under the ErrorMessage struct

Create found error message for status code 302

Create found error message for status code 302 with a details item which is passed in as the details key under the ErrorMessage struct

Create gateway_timeout error message for status code 504

Create gateway_timeout error message for status code 504 with a details item which is passed in as the details key under the ErrorMessage struct

Create gone error message for status code 410

Create gone error message for status code 410 with a details item which is passed in as the details key under the ErrorMessage struct

Returns the http code for an error message or error code atom

Returns the http reason as an atom for the http error code

Create http_version_not_supported error message for status code 505

Create http_version_not_supported error message for status code 505 with a details item which is passed in as the details key under the ErrorMessage struct

Create im_a_teapot error message for status code 418

Create im_a_teapot error message for status code 418 with a details item which is passed in as the details key under the ErrorMessage struct

Create insufficient_storage error message for status code 507

Create insufficient_storage error message for status code 507 with a details item which is passed in as the details key under the ErrorMessage struct

Create internal_server_error error message for status code 500

Create internal_server_error error message for status code 500 with a details item which is passed in as the details key under the ErrorMessage struct

Create length_required error message for status code 411

Create length_required error message for status code 411 with a details item which is passed in as the details key under the ErrorMessage struct

Create locked error message for status code 423

Create locked error message for status code 423 with a details item which is passed in as the details key under the ErrorMessage struct

Create loop_detected error message for status code 508

Create loop_detected error message for status code 508 with a details item which is passed in as the details key under the ErrorMessage struct

Create method_not_allowed error message for status code 405

Create method_not_allowed error message for status code 405 with a details item which is passed in as the details key under the ErrorMessage struct

Create misdirected_request error message for status code 421

Create misdirected_request error message for status code 421 with a details item which is passed in as the details key under the ErrorMessage struct

Create moved_permanently error message for status code 301

Create moved_permanently error message for status code 301 with a details item which is passed in as the details key under the ErrorMessage struct

Create multiple_choices error message for status code 300

Create multiple_choices error message for status code 300 with a details item which is passed in as the details key under the ErrorMessage struct

Create network_authentication_required error message for status code 511

Create network_authentication_required error message for status code 511 with a details item which is passed in as the details key under the ErrorMessage struct

Create not_acceptable error message for status code 406

Create not_acceptable error message for status code 406 with a details item which is passed in as the details key under the ErrorMessage struct

Create not_extended error message for status code 510

Create not_extended error message for status code 510 with a details item which is passed in as the details key under the ErrorMessage struct

Create not_found error message for status code 404

Create not_found error message for status code 404 with a details item which is passed in as the details key under the ErrorMessage struct

Create not_implemented error message for status code 501

Create not_implemented error message for status code 501 with a details item which is passed in as the details key under the ErrorMessage struct

Create not_modified error message for status code 304

Create not_modified error message for status code 304 with a details item which is passed in as the details key under the ErrorMessage struct

Create payment_required error message for status code 402

Create payment_required error message for status code 402 with a details item which is passed in as the details key under the ErrorMessage struct

Create permanent_redirect error message for status code 308

Create permanent_redirect error message for status code 308 with a details item which is passed in as the details key under the ErrorMessage struct

Create precondition_failed error message for status code 412

Create precondition_failed error message for status code 412 with a details item which is passed in as the details key under the ErrorMessage struct

Create precondition_required error message for status code 428

Create precondition_required error message for status code 428 with a details item which is passed in as the details key under the ErrorMessage struct

Create proxy_authentication_required error message for status code 407

Create proxy_authentication_required error message for status code 407 with a details item which is passed in as the details key under the ErrorMessage struct

Create request_entity_too_large error message for status code 413

Create request_entity_too_large error message for status code 413 with a details item which is passed in as the details key under the ErrorMessage struct

Create request_header_fields_too_large error message for status code 431

Create request_header_fields_too_large error message for status code 431 with a details item which is passed in as the details key under the ErrorMessage struct

Create request_timeout error message for status code 408

Create request_timeout error message for status code 408 with a details item which is passed in as the details key under the ErrorMessage struct

Create request_uri_too_long error message for status code 414

Create request_uri_too_long error message for status code 414 with a details item which is passed in as the details key under the ErrorMessage struct

Create requested_range_not_satisfiable error message for status code 416

Create requested_range_not_satisfiable error message for status code 416 with a details item which is passed in as the details key under the ErrorMessage struct

Create see_other error message for status code 303

Create see_other error message for status code 303 with a details item which is passed in as the details key under the ErrorMessage struct

Create service_unavailable error message for status code 503

Create service_unavailable error message for status code 503 with a details item which is passed in as the details key under the ErrorMessage struct

Create switch_proxy error message for status code 306

Create switch_proxy error message for status code 306 with a details item which is passed in as the details key under the ErrorMessage struct

Create temporary_redirect error message for status code 307

Create temporary_redirect error message for status code 307 with a details item which is passed in as the details key under the ErrorMessage struct

Converts an %ErrorMessage{} struct to a map and makes sure that the contents of the details map can be converted to json

Converts an %ErrorMessage{} struct to a string formatted error message

Create too_early error message for status code 425

Create too_early error message for status code 425 with a details item which is passed in as the details key under the ErrorMessage struct

Create too_many_requests error message for status code 429

Create too_many_requests error message for status code 429 with a details item which is passed in as the details key under the ErrorMessage struct

Create unauthorized error message for status code 401

Create unauthorized error message for status code 401 with a details item which is passed in as the details key under the ErrorMessage struct

Create unavailable_for_legal_reasons error message for status code 451

Create unavailable_for_legal_reasons error message for status code 451 with a details item which is passed in as the details key under the ErrorMessage struct

Create unprocessable_entity error message for status code 422

Create unprocessable_entity error message for status code 422 with a details item which is passed in as the details key under the ErrorMessage struct

Create unsupported_media_type error message for status code 415

Create unsupported_media_type error message for status code 415 with a details item which is passed in as the details key under the ErrorMessage struct

Create upgrade_required error message for status code 426

Create upgrade_required error message for status code 426 with a details item which is passed in as the details key under the ErrorMessage struct

Create use_proxy error message for status code 305

Create use_proxy error message for status code 305 with a details item which is passed in as the details key under the ErrorMessage struct

Create variant_also_negotiates error message for status code 506

Create variant_also_negotiates error message for status code 506 with a details item which is passed in as the details key under the ErrorMessage struct

Link to this section Types

Specs

code() ::
  :multiple_choices
  | :moved_permanently
  | :found
  | :see_other
  | :not_modified
  | :use_proxy
  | :switch_proxy
  | :temporary_redirect
  | :permanent_redirect
  | :bad_request
  | :unauthorized
  | :payment_required
  | :forbidden
  | :not_found
  | :method_not_allowed
  | :not_acceptable
  | :proxy_authentication_required
  | :request_timeout
  | :conflict
  | :gone
  | :length_required
  | :precondition_failed
  | :request_entity_too_large
  | :request_uri_too_long
  | :unsupported_media_type
  | :requested_range_not_satisfiable
  | :expectation_failed
  | :im_a_teapot
  | :misdirected_request
  | :unprocessable_entity
  | :locked
  | :failed_dependency
  | :too_early
  | :upgrade_required
  | :precondition_required
  | :too_many_requests
  | :request_header_fields_too_large
  | :unavailable_for_legal_reasons
  | :internal_server_error
  | :not_implemented
  | :bad_gateway
  | :service_unavailable
  | :gateway_timeout
  | :http_version_not_supported
  | :variant_also_negotiates
  | :insufficient_storage
  | :loop_detected
  | :not_extended
  | :network_authentication_required

Specs

t() :: %ErrorMessage{code: code(), details: any(), message: String.t()}

Specs

t(details) :: %ErrorMessage{code: code(), details: details, message: String.t()}

Specs

t_map() ::
  %{code: code(), message: String.t(), details: any(), request_id: String.t()}
  | %{code: code(), message: String.t(), details: any()}

Specs

t_map(details) ::
  %{code: code(), message: String.t(), details: details, request_id: String.t()}
  | %{code: code(), message: String.t(), details: details}

Specs

t_ok_res() :: :ok | {:error, t()}
Link to this type

t_ok_res(details_type)

View Source

Specs

t_ok_res(details_type) :: :ok | {:error, t(details_type)}

Specs

t_res() :: {:ok, term()} | {:error, t()}

Specs

t_res(result_type) :: {:ok, result_type} | {:error, t()}
Link to this type

t_res(result_type, details_type)

View Source

Specs

t_res(result_type, details_type) ::
  {:ok, result_type} | {:error, t(details_type)}

Link to this section Functions

Specs

bad_gateway(message :: String.t()) :: t()

Create bad_gateway error message for status code 502

Example

iex> ErrorMessage.bad_gateway("error message")
%ErrorMessage{code: :bad_gateway, message: "error message"}
Link to this function

bad_gateway(message, details)

View Source

Specs

bad_gateway(message :: String.t(), details :: any()) :: t()

Create bad_gateway error message for status code 502 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.bad_gateway("error message", %{item: 1234})
%ErrorMessage{code: :bad_gateway, message: "error message", details: %{item: 1234}}

Specs

bad_request(message :: String.t()) :: t()

Create bad_request error message for status code 400

Example

iex> ErrorMessage.bad_request("error message")
%ErrorMessage{code: :bad_request, message: "error message"}
Link to this function

bad_request(message, details)

View Source

Specs

bad_request(message :: String.t(), details :: any()) :: t()

Create bad_request error message for status code 400 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.bad_request("error message", %{item: 1234})
%ErrorMessage{code: :bad_request, message: "error message", details: %{item: 1234}}

Specs

conflict(message :: String.t()) :: t()

Create conflict error message for status code 409

Example

iex> ErrorMessage.conflict("error message")
%ErrorMessage{code: :conflict, message: "error message"}
Link to this function

conflict(message, details)

View Source

Specs

conflict(message :: String.t(), details :: any()) :: t()

Create conflict error message for status code 409 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.conflict("error message", %{item: 1234})
%ErrorMessage{code: :conflict, message: "error message", details: %{item: 1234}}
Link to this function

expectation_failed(message)

View Source

Specs

expectation_failed(message :: String.t()) :: t()

Create expectation_failed error message for status code 417

Example

iex> ErrorMessage.expectation_failed("error message")
%ErrorMessage{code: :expectation_failed, message: "error message"}
Link to this function

expectation_failed(message, details)

View Source

Specs

expectation_failed(message :: String.t(), details :: any()) :: t()

Create expectation_failed error message for status code 417 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.expectation_failed("error message", %{item: 1234})
%ErrorMessage{code: :expectation_failed, message: "error message", details: %{item: 1234}}
Link to this function

failed_dependency(message)

View Source

Specs

failed_dependency(message :: String.t()) :: t()

Create failed_dependency error message for status code 424

Example

iex> ErrorMessage.failed_dependency("error message")
%ErrorMessage{code: :failed_dependency, message: "error message"}
Link to this function

failed_dependency(message, details)

View Source

Specs

failed_dependency(message :: String.t(), details :: any()) :: t()

Create failed_dependency error message for status code 424 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.failed_dependency("error message", %{item: 1234})
%ErrorMessage{code: :failed_dependency, message: "error message", details: %{item: 1234}}

Specs

forbidden(message :: String.t()) :: t()

Create forbidden error message for status code 403

Example

iex> ErrorMessage.forbidden("error message")
%ErrorMessage{code: :forbidden, message: "error message"}
Link to this function

forbidden(message, details)

View Source

Specs

forbidden(message :: String.t(), details :: any()) :: t()

Create forbidden error message for status code 403 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.forbidden("error message", %{item: 1234})
%ErrorMessage{code: :forbidden, message: "error message", details: %{item: 1234}}

Specs

found(message :: String.t()) :: t()

Create found error message for status code 302

Example

iex> ErrorMessage.found("error message")
%ErrorMessage{code: :found, message: "error message"}

Specs

found(message :: String.t(), details :: any()) :: t()

Create found error message for status code 302 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.found("error message", %{item: 1234})
%ErrorMessage{code: :found, message: "error message", details: %{item: 1234}}
Link to this function

gateway_timeout(message)

View Source

Specs

gateway_timeout(message :: String.t()) :: t()

Create gateway_timeout error message for status code 504

Example

iex> ErrorMessage.gateway_timeout("error message")
%ErrorMessage{code: :gateway_timeout, message: "error message"}
Link to this function

gateway_timeout(message, details)

View Source

Specs

gateway_timeout(message :: String.t(), details :: any()) :: t()

Create gateway_timeout error message for status code 504 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.gateway_timeout("error message", %{item: 1234})
%ErrorMessage{code: :gateway_timeout, message: "error message", details: %{item: 1234}}

Specs

gone(message :: String.t()) :: t()

Create gone error message for status code 410

Example

iex> ErrorMessage.gone("error message")
%ErrorMessage{code: :gone, message: "error message"}

Specs

gone(message :: String.t(), details :: any()) :: t()

Create gone error message for status code 410 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.gone("error message", %{item: 1234})
%ErrorMessage{code: :gone, message: "error message", details: %{item: 1234}}

Specs

http_code(error_code :: code()) :: non_neg_integer()
http_code(error_message :: t()) :: non_neg_integer()

Returns the http code for an error message or error code atom

Example

iex> ErrorMessage.http_code(:internal_server_error)
500

iex> ErrorMessage.http_code(ErrorMessage.not_found("some_message"))
404
Link to this function

http_code_reason_atom(error_code)

View Source

Specs

http_code_reason_atom(error_code :: non_neg_integer()) :: code()

Returns the http reason as an atom for the http error code

Example

iex> ErrorMessage.http_code_reason_atom(500)
:internal_server_error
Link to this function

http_version_not_supported(message)

View Source

Specs

http_version_not_supported(message :: String.t()) :: t()

Create http_version_not_supported error message for status code 505

Example

iex> ErrorMessage.http_version_not_supported("error message")
%ErrorMessage{code: :http_version_not_supported, message: "error message"}
Link to this function

http_version_not_supported(message, details)

View Source

Specs

http_version_not_supported(message :: String.t(), details :: any()) :: t()

Create http_version_not_supported error message for status code 505 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.http_version_not_supported("error message", %{item: 1234})
%ErrorMessage{code: :http_version_not_supported, message: "error message", details: %{item: 1234}}

Specs

im_a_teapot(message :: String.t()) :: t()

Create im_a_teapot error message for status code 418

Example

iex> ErrorMessage.im_a_teapot("error message")
%ErrorMessage{code: :im_a_teapot, message: "error message"}
Link to this function

im_a_teapot(message, details)

View Source

Specs

im_a_teapot(message :: String.t(), details :: any()) :: t()

Create im_a_teapot error message for status code 418 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.im_a_teapot("error message", %{item: 1234})
%ErrorMessage{code: :im_a_teapot, message: "error message", details: %{item: 1234}}
Link to this function

insufficient_storage(message)

View Source

Specs

insufficient_storage(message :: String.t()) :: t()

Create insufficient_storage error message for status code 507

Example

iex> ErrorMessage.insufficient_storage("error message")
%ErrorMessage{code: :insufficient_storage, message: "error message"}
Link to this function

insufficient_storage(message, details)

View Source

Specs

insufficient_storage(message :: String.t(), details :: any()) :: t()

Create insufficient_storage error message for status code 507 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.insufficient_storage("error message", %{item: 1234})
%ErrorMessage{code: :insufficient_storage, message: "error message", details: %{item: 1234}}
Link to this function

internal_server_error(message)

View Source

Specs

internal_server_error(message :: String.t()) :: t()

Create internal_server_error error message for status code 500

Example

iex> ErrorMessage.internal_server_error("error message")
%ErrorMessage{code: :internal_server_error, message: "error message"}
Link to this function

internal_server_error(message, details)

View Source

Specs

internal_server_error(message :: String.t(), details :: any()) :: t()

Create internal_server_error error message for status code 500 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.internal_server_error("error message", %{item: 1234})
%ErrorMessage{code: :internal_server_error, message: "error message", details: %{item: 1234}}
Link to this function

length_required(message)

View Source

Specs

length_required(message :: String.t()) :: t()

Create length_required error message for status code 411

Example

iex> ErrorMessage.length_required("error message")
%ErrorMessage{code: :length_required, message: "error message"}
Link to this function

length_required(message, details)

View Source

Specs

length_required(message :: String.t(), details :: any()) :: t()

Create length_required error message for status code 411 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.length_required("error message", %{item: 1234})
%ErrorMessage{code: :length_required, message: "error message", details: %{item: 1234}}

Specs

locked(message :: String.t()) :: t()

Create locked error message for status code 423

Example

iex> ErrorMessage.locked("error message")
%ErrorMessage{code: :locked, message: "error message"}
Link to this function

locked(message, details)

View Source

Specs

locked(message :: String.t(), details :: any()) :: t()

Create locked error message for status code 423 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.locked("error message", %{item: 1234})
%ErrorMessage{code: :locked, message: "error message", details: %{item: 1234}}

Specs

loop_detected(message :: String.t()) :: t()

Create loop_detected error message for status code 508

Example

iex> ErrorMessage.loop_detected("error message")
%ErrorMessage{code: :loop_detected, message: "error message"}
Link to this function

loop_detected(message, details)

View Source

Specs

loop_detected(message :: String.t(), details :: any()) :: t()

Create loop_detected error message for status code 508 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.loop_detected("error message", %{item: 1234})
%ErrorMessage{code: :loop_detected, message: "error message", details: %{item: 1234}}
Link to this function

method_not_allowed(message)

View Source

Specs

method_not_allowed(message :: String.t()) :: t()

Create method_not_allowed error message for status code 405

Example

iex> ErrorMessage.method_not_allowed("error message")
%ErrorMessage{code: :method_not_allowed, message: "error message"}
Link to this function

method_not_allowed(message, details)

View Source

Specs

method_not_allowed(message :: String.t(), details :: any()) :: t()

Create method_not_allowed error message for status code 405 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.method_not_allowed("error message", %{item: 1234})
%ErrorMessage{code: :method_not_allowed, message: "error message", details: %{item: 1234}}
Link to this function

misdirected_request(message)

View Source

Specs

misdirected_request(message :: String.t()) :: t()

Create misdirected_request error message for status code 421

Example

iex> ErrorMessage.misdirected_request("error message")
%ErrorMessage{code: :misdirected_request, message: "error message"}
Link to this function

misdirected_request(message, details)

View Source

Specs

misdirected_request(message :: String.t(), details :: any()) :: t()

Create misdirected_request error message for status code 421 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.misdirected_request("error message", %{item: 1234})
%ErrorMessage{code: :misdirected_request, message: "error message", details: %{item: 1234}}
Link to this function

moved_permanently(message)

View Source

Specs

moved_permanently(message :: String.t()) :: t()

Create moved_permanently error message for status code 301

Example

iex> ErrorMessage.moved_permanently("error message")
%ErrorMessage{code: :moved_permanently, message: "error message"}
Link to this function

moved_permanently(message, details)

View Source

Specs

moved_permanently(message :: String.t(), details :: any()) :: t()

Create moved_permanently error message for status code 301 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.moved_permanently("error message", %{item: 1234})
%ErrorMessage{code: :moved_permanently, message: "error message", details: %{item: 1234}}
Link to this function

multiple_choices(message)

View Source

Specs

multiple_choices(message :: String.t()) :: t()

Create multiple_choices error message for status code 300

Example

iex> ErrorMessage.multiple_choices("error message")
%ErrorMessage{code: :multiple_choices, message: "error message"}
Link to this function

multiple_choices(message, details)

View Source

Specs

multiple_choices(message :: String.t(), details :: any()) :: t()

Create multiple_choices error message for status code 300 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.multiple_choices("error message", %{item: 1234})
%ErrorMessage{code: :multiple_choices, message: "error message", details: %{item: 1234}}
Link to this function

network_authentication_required(message)

View Source

Specs

network_authentication_required(message :: String.t()) :: t()

Create network_authentication_required error message for status code 511

Example

iex> ErrorMessage.network_authentication_required("error message")
%ErrorMessage{code: :network_authentication_required, message: "error message"}
Link to this function

network_authentication_required(message, details)

View Source

Specs

network_authentication_required(message :: String.t(), details :: any()) :: t()

Create network_authentication_required error message for status code 511 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.network_authentication_required("error message", %{item: 1234})
%ErrorMessage{code: :network_authentication_required, message: "error message", details: %{item: 1234}}

Specs

not_acceptable(message :: String.t()) :: t()

Create not_acceptable error message for status code 406

Example

iex> ErrorMessage.not_acceptable("error message")
%ErrorMessage{code: :not_acceptable, message: "error message"}
Link to this function

not_acceptable(message, details)

View Source

Specs

not_acceptable(message :: String.t(), details :: any()) :: t()

Create not_acceptable error message for status code 406 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.not_acceptable("error message", %{item: 1234})
%ErrorMessage{code: :not_acceptable, message: "error message", details: %{item: 1234}}

Specs

not_extended(message :: String.t()) :: t()

Create not_extended error message for status code 510

Example

iex> ErrorMessage.not_extended("error message")
%ErrorMessage{code: :not_extended, message: "error message"}
Link to this function

not_extended(message, details)

View Source

Specs

not_extended(message :: String.t(), details :: any()) :: t()

Create not_extended error message for status code 510 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.not_extended("error message", %{item: 1234})
%ErrorMessage{code: :not_extended, message: "error message", details: %{item: 1234}}

Specs

not_found(message :: String.t()) :: t()

Create not_found error message for status code 404

Example

iex> ErrorMessage.not_found("error message")
%ErrorMessage{code: :not_found, message: "error message"}
Link to this function

not_found(message, details)

View Source

Specs

not_found(message :: String.t(), details :: any()) :: t()

Create not_found error message for status code 404 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.not_found("error message", %{item: 1234})
%ErrorMessage{code: :not_found, message: "error message", details: %{item: 1234}}
Link to this function

not_implemented(message)

View Source

Specs

not_implemented(message :: String.t()) :: t()

Create not_implemented error message for status code 501

Example

iex> ErrorMessage.not_implemented("error message")
%ErrorMessage{code: :not_implemented, message: "error message"}
Link to this function

not_implemented(message, details)

View Source

Specs

not_implemented(message :: String.t(), details :: any()) :: t()

Create not_implemented error message for status code 501 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.not_implemented("error message", %{item: 1234})
%ErrorMessage{code: :not_implemented, message: "error message", details: %{item: 1234}}

Specs

not_modified(message :: String.t()) :: t()

Create not_modified error message for status code 304

Example

iex> ErrorMessage.not_modified("error message")
%ErrorMessage{code: :not_modified, message: "error message"}
Link to this function

not_modified(message, details)

View Source

Specs

not_modified(message :: String.t(), details :: any()) :: t()

Create not_modified error message for status code 304 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.not_modified("error message", %{item: 1234})
%ErrorMessage{code: :not_modified, message: "error message", details: %{item: 1234}}
Link to this function

payment_required(message)

View Source

Specs

payment_required(message :: String.t()) :: t()

Create payment_required error message for status code 402

Example

iex> ErrorMessage.payment_required("error message")
%ErrorMessage{code: :payment_required, message: "error message"}
Link to this function

payment_required(message, details)

View Source

Specs

payment_required(message :: String.t(), details :: any()) :: t()

Create payment_required error message for status code 402 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.payment_required("error message", %{item: 1234})
%ErrorMessage{code: :payment_required, message: "error message", details: %{item: 1234}}
Link to this function

permanent_redirect(message)

View Source

Specs

permanent_redirect(message :: String.t()) :: t()

Create permanent_redirect error message for status code 308

Example

iex> ErrorMessage.permanent_redirect("error message")
%ErrorMessage{code: :permanent_redirect, message: "error message"}
Link to this function

permanent_redirect(message, details)

View Source

Specs

permanent_redirect(message :: String.t(), details :: any()) :: t()

Create permanent_redirect error message for status code 308 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.permanent_redirect("error message", %{item: 1234})
%ErrorMessage{code: :permanent_redirect, message: "error message", details: %{item: 1234}}
Link to this function

precondition_failed(message)

View Source

Specs

precondition_failed(message :: String.t()) :: t()

Create precondition_failed error message for status code 412

Example

iex> ErrorMessage.precondition_failed("error message")
%ErrorMessage{code: :precondition_failed, message: "error message"}
Link to this function

precondition_failed(message, details)

View Source

Specs

precondition_failed(message :: String.t(), details :: any()) :: t()

Create precondition_failed error message for status code 412 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.precondition_failed("error message", %{item: 1234})
%ErrorMessage{code: :precondition_failed, message: "error message", details: %{item: 1234}}
Link to this function

precondition_required(message)

View Source

Specs

precondition_required(message :: String.t()) :: t()

Create precondition_required error message for status code 428

Example

iex> ErrorMessage.precondition_required("error message")
%ErrorMessage{code: :precondition_required, message: "error message"}
Link to this function

precondition_required(message, details)

View Source

Specs

precondition_required(message :: String.t(), details :: any()) :: t()

Create precondition_required error message for status code 428 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.precondition_required("error message", %{item: 1234})
%ErrorMessage{code: :precondition_required, message: "error message", details: %{item: 1234}}
Link to this function

proxy_authentication_required(message)

View Source

Specs

proxy_authentication_required(message :: String.t()) :: t()

Create proxy_authentication_required error message for status code 407

Example

iex> ErrorMessage.proxy_authentication_required("error message")
%ErrorMessage{code: :proxy_authentication_required, message: "error message"}
Link to this function

proxy_authentication_required(message, details)

View Source

Specs

proxy_authentication_required(message :: String.t(), details :: any()) :: t()

Create proxy_authentication_required error message for status code 407 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.proxy_authentication_required("error message", %{item: 1234})
%ErrorMessage{code: :proxy_authentication_required, message: "error message", details: %{item: 1234}}
Link to this function

request_entity_too_large(message)

View Source

Specs

request_entity_too_large(message :: String.t()) :: t()

Create request_entity_too_large error message for status code 413

Example

iex> ErrorMessage.request_entity_too_large("error message")
%ErrorMessage{code: :request_entity_too_large, message: "error message"}
Link to this function

request_entity_too_large(message, details)

View Source

Specs

request_entity_too_large(message :: String.t(), details :: any()) :: t()

Create request_entity_too_large error message for status code 413 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.request_entity_too_large("error message", %{item: 1234})
%ErrorMessage{code: :request_entity_too_large, message: "error message", details: %{item: 1234}}
Link to this function

request_header_fields_too_large(message)

View Source

Specs

request_header_fields_too_large(message :: String.t()) :: t()

Create request_header_fields_too_large error message for status code 431

Example

iex> ErrorMessage.request_header_fields_too_large("error message")
%ErrorMessage{code: :request_header_fields_too_large, message: "error message"}
Link to this function

request_header_fields_too_large(message, details)

View Source

Specs

request_header_fields_too_large(message :: String.t(), details :: any()) :: t()

Create request_header_fields_too_large error message for status code 431 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.request_header_fields_too_large("error message", %{item: 1234})
%ErrorMessage{code: :request_header_fields_too_large, message: "error message", details: %{item: 1234}}
Link to this function

request_timeout(message)

View Source

Specs

request_timeout(message :: String.t()) :: t()

Create request_timeout error message for status code 408

Example

iex> ErrorMessage.request_timeout("error message")
%ErrorMessage{code: :request_timeout, message: "error message"}
Link to this function

request_timeout(message, details)

View Source

Specs

request_timeout(message :: String.t(), details :: any()) :: t()

Create request_timeout error message for status code 408 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.request_timeout("error message", %{item: 1234})
%ErrorMessage{code: :request_timeout, message: "error message", details: %{item: 1234}}
Link to this function

request_uri_too_long(message)

View Source

Specs

request_uri_too_long(message :: String.t()) :: t()

Create request_uri_too_long error message for status code 414

Example

iex> ErrorMessage.request_uri_too_long("error message")
%ErrorMessage{code: :request_uri_too_long, message: "error message"}
Link to this function

request_uri_too_long(message, details)

View Source

Specs

request_uri_too_long(message :: String.t(), details :: any()) :: t()

Create request_uri_too_long error message for status code 414 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.request_uri_too_long("error message", %{item: 1234})
%ErrorMessage{code: :request_uri_too_long, message: "error message", details: %{item: 1234}}
Link to this function

requested_range_not_satisfiable(message)

View Source

Specs

requested_range_not_satisfiable(message :: String.t()) :: t()

Create requested_range_not_satisfiable error message for status code 416

Example

iex> ErrorMessage.requested_range_not_satisfiable("error message")
%ErrorMessage{code: :requested_range_not_satisfiable, message: "error message"}
Link to this function

requested_range_not_satisfiable(message, details)

View Source

Specs

requested_range_not_satisfiable(message :: String.t(), details :: any()) :: t()

Create requested_range_not_satisfiable error message for status code 416 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.requested_range_not_satisfiable("error message", %{item: 1234})
%ErrorMessage{code: :requested_range_not_satisfiable, message: "error message", details: %{item: 1234}}

Specs

see_other(message :: String.t()) :: t()

Create see_other error message for status code 303

Example

iex> ErrorMessage.see_other("error message")
%ErrorMessage{code: :see_other, message: "error message"}
Link to this function

see_other(message, details)

View Source

Specs

see_other(message :: String.t(), details :: any()) :: t()

Create see_other error message for status code 303 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.see_other("error message", %{item: 1234})
%ErrorMessage{code: :see_other, message: "error message", details: %{item: 1234}}
Link to this function

service_unavailable(message)

View Source

Specs

service_unavailable(message :: String.t()) :: t()

Create service_unavailable error message for status code 503

Example

iex> ErrorMessage.service_unavailable("error message")
%ErrorMessage{code: :service_unavailable, message: "error message"}
Link to this function

service_unavailable(message, details)

View Source

Specs

service_unavailable(message :: String.t(), details :: any()) :: t()

Create service_unavailable error message for status code 503 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.service_unavailable("error message", %{item: 1234})
%ErrorMessage{code: :service_unavailable, message: "error message", details: %{item: 1234}}

Specs

switch_proxy(message :: String.t()) :: t()

Create switch_proxy error message for status code 306

Example

iex> ErrorMessage.switch_proxy("error message")
%ErrorMessage{code: :switch_proxy, message: "error message"}
Link to this function

switch_proxy(message, details)

View Source

Specs

switch_proxy(message :: String.t(), details :: any()) :: t()

Create switch_proxy error message for status code 306 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.switch_proxy("error message", %{item: 1234})
%ErrorMessage{code: :switch_proxy, message: "error message", details: %{item: 1234}}
Link to this function

temporary_redirect(message)

View Source

Specs

temporary_redirect(message :: String.t()) :: t()

Create temporary_redirect error message for status code 307

Example

iex> ErrorMessage.temporary_redirect("error message")
%ErrorMessage{code: :temporary_redirect, message: "error message"}
Link to this function

temporary_redirect(message, details)

View Source

Specs

temporary_redirect(message :: String.t(), details :: any()) :: t()

Create temporary_redirect error message for status code 307 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.temporary_redirect("error message", %{item: 1234})
%ErrorMessage{code: :temporary_redirect, message: "error message", details: %{item: 1234}}
Link to this function

to_jsonable_map(error_message)

View Source

Specs

to_jsonable_map(error_message :: t()) :: t_map()

Converts an %ErrorMessage{} struct to a map and makes sure that the contents of the details map can be converted to json

## Example

  iex> ErrorMessage.to_jsonable_map(ErrorMessage.not_found("couldn't find user", %{user_id: "as21fasdfJ"}))
  %{code: :not_found, message: "couldn't find user", details: %{user_id: "as21fasdfJ"}}

  iex> error = ErrorMessage.im_a_teapot("teapot", %{
  ...>   user: %{health: {:alive, 500}},
  ...>   test: %TestStruct{a: [Date.new!(2020, 1, 10)]}
  ...> })
  iex> ErrorMessage.to_jsonable_map(error)
  %{
    code: :im_a_teapot,
    message: "teapot",
    details: %{
      user: %{health: [:alive, 500]},
      test: %{struct: "ErrorMessageTest.TestStruct", data: %{a: ["2020-01-10"]}}
    }
  }
Link to this function

to_string(error_message)

View Source

Specs

to_string(error_message :: t()) :: String.t()

Converts an %ErrorMessage{} struct to a string formatted error message

## Example

  iex> ErrorMessage.to_string(ErrorMessage.internal_server_error("Something bad happened", %{result: :unknown}))
  "internal_server_error - Something bad happened\nDetails: \n%{result: :unknown}"

Specs

too_early(message :: String.t()) :: t()

Create too_early error message for status code 425

Example

iex> ErrorMessage.too_early("error message")
%ErrorMessage{code: :too_early, message: "error message"}
Link to this function

too_early(message, details)

View Source

Specs

too_early(message :: String.t(), details :: any()) :: t()

Create too_early error message for status code 425 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.too_early("error message", %{item: 1234})
%ErrorMessage{code: :too_early, message: "error message", details: %{item: 1234}}
Link to this function

too_many_requests(message)

View Source

Specs

too_many_requests(message :: String.t()) :: t()

Create too_many_requests error message for status code 429

Example

iex> ErrorMessage.too_many_requests("error message")
%ErrorMessage{code: :too_many_requests, message: "error message"}
Link to this function

too_many_requests(message, details)

View Source

Specs

too_many_requests(message :: String.t(), details :: any()) :: t()

Create too_many_requests error message for status code 429 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.too_many_requests("error message", %{item: 1234})
%ErrorMessage{code: :too_many_requests, message: "error message", details: %{item: 1234}}

Specs

unauthorized(message :: String.t()) :: t()

Create unauthorized error message for status code 401

Example

iex> ErrorMessage.unauthorized("error message")
%ErrorMessage{code: :unauthorized, message: "error message"}
Link to this function

unauthorized(message, details)

View Source

Specs

unauthorized(message :: String.t(), details :: any()) :: t()

Create unauthorized error message for status code 401 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.unauthorized("error message", %{item: 1234})
%ErrorMessage{code: :unauthorized, message: "error message", details: %{item: 1234}}
Link to this function

unprocessable_entity(message)

View Source

Specs

unprocessable_entity(message :: String.t()) :: t()

Create unprocessable_entity error message for status code 422

Example

iex> ErrorMessage.unprocessable_entity("error message")
%ErrorMessage{code: :unprocessable_entity, message: "error message"}
Link to this function

unprocessable_entity(message, details)

View Source

Specs

unprocessable_entity(message :: String.t(), details :: any()) :: t()

Create unprocessable_entity error message for status code 422 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.unprocessable_entity("error message", %{item: 1234})
%ErrorMessage{code: :unprocessable_entity, message: "error message", details: %{item: 1234}}
Link to this function

unsupported_media_type(message)

View Source

Specs

unsupported_media_type(message :: String.t()) :: t()

Create unsupported_media_type error message for status code 415

Example

iex> ErrorMessage.unsupported_media_type("error message")
%ErrorMessage{code: :unsupported_media_type, message: "error message"}
Link to this function

unsupported_media_type(message, details)

View Source

Specs

unsupported_media_type(message :: String.t(), details :: any()) :: t()

Create unsupported_media_type error message for status code 415 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.unsupported_media_type("error message", %{item: 1234})
%ErrorMessage{code: :unsupported_media_type, message: "error message", details: %{item: 1234}}
Link to this function

upgrade_required(message)

View Source

Specs

upgrade_required(message :: String.t()) :: t()

Create upgrade_required error message for status code 426

Example

iex> ErrorMessage.upgrade_required("error message")
%ErrorMessage{code: :upgrade_required, message: "error message"}
Link to this function

upgrade_required(message, details)

View Source

Specs

upgrade_required(message :: String.t(), details :: any()) :: t()

Create upgrade_required error message for status code 426 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.upgrade_required("error message", %{item: 1234})
%ErrorMessage{code: :upgrade_required, message: "error message", details: %{item: 1234}}

Specs

use_proxy(message :: String.t()) :: t()

Create use_proxy error message for status code 305

Example

iex> ErrorMessage.use_proxy("error message")
%ErrorMessage{code: :use_proxy, message: "error message"}
Link to this function

use_proxy(message, details)

View Source

Specs

use_proxy(message :: String.t(), details :: any()) :: t()

Create use_proxy error message for status code 305 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.use_proxy("error message", %{item: 1234})
%ErrorMessage{code: :use_proxy, message: "error message", details: %{item: 1234}}
Link to this function

variant_also_negotiates(message)

View Source

Specs

variant_also_negotiates(message :: String.t()) :: t()

Create variant_also_negotiates error message for status code 506

Example

iex> ErrorMessage.variant_also_negotiates("error message")
%ErrorMessage{code: :variant_also_negotiates, message: "error message"}
Link to this function

variant_also_negotiates(message, details)

View Source

Specs

variant_also_negotiates(message :: String.t(), details :: any()) :: t()

Create variant_also_negotiates error message for status code 506 with a details item which is passed in as the details key under the ErrorMessage struct

Example

iex> ErrorMessage.variant_also_negotiates("error message", %{item: 1234})
%ErrorMessage{code: :variant_also_negotiates, message: "error message", details: %{item: 1234}}