ErrorMessage (error_message v0.3.3)

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

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

Types

code()

@type 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

t()

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

t(details)

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

t_map()

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

t_map(details)

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

t_ok_res()

@type t_ok_res() :: :ok | {:error, t()}

t_ok_res(details_type)

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

t_res()

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

t_res(result_type)

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

t_res(result_type, details_type)

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

Functions

bad_gateway(message)

@spec 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"}

bad_gateway(message, details)

@spec 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}}

bad_request(message)

@spec 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"}

bad_request(message, details)

@spec 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}}

conflict(message)

@spec 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"}

conflict(message, details)

@spec 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}}

expectation_failed(message)

@spec 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"}

expectation_failed(message, details)

@spec 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}}

failed_dependency(message)

@spec 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"}

failed_dependency(message, details)

@spec 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}}

forbidden(message)

@spec 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"}

forbidden(message, details)

@spec 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}}

found(message)

@spec 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"}

found(message, details)

@spec 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}}

gateway_timeout(message)

@spec 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"}

gateway_timeout(message, details)

@spec 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}}

gone(message)

@spec 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"}

gone(message, details)

@spec 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}}

http_code(error_code)

@spec http_code(error_code :: code()) :: non_neg_integer()
@spec 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

http_code_reason_atom(error_code)

@spec 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

http_version_not_supported(message)

@spec 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"}

http_version_not_supported(message, details)

@spec 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}}

im_a_teapot(message)

@spec 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"}

im_a_teapot(message, details)

@spec 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}}

insufficient_storage(message)

@spec 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"}

insufficient_storage(message, details)

@spec 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}}

internal_server_error(message)

@spec 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"}

internal_server_error(message, details)

@spec 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}}

length_required(message)

@spec 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"}

length_required(message, details)

@spec 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}}

locked(message)

@spec 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"}

locked(message, details)

@spec 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}}

loop_detected(message)

@spec 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"}

loop_detected(message, details)

@spec 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}}

method_not_allowed(message)

@spec 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"}

method_not_allowed(message, details)

@spec 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}}

misdirected_request(message)

@spec 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"}

misdirected_request(message, details)

@spec 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}}

moved_permanently(message)

@spec 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"}

moved_permanently(message, details)

@spec 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}}

multiple_choices(message)

@spec 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"}

multiple_choices(message, details)

@spec 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}}

network_authentication_required(message)

@spec 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"}

network_authentication_required(message, details)

@spec 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}}

not_acceptable(message)

@spec 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"}

not_acceptable(message, details)

@spec 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}}

not_extended(message)

@spec 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"}

not_extended(message, details)

@spec 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}}

not_found(message)

@spec 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"}

not_found(message, details)

@spec 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}}

not_implemented(message)

@spec 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"}

not_implemented(message, details)

@spec 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}}

not_modified(message)

@spec 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"}

not_modified(message, details)

@spec 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}}

payment_required(message)

@spec 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"}

payment_required(message, details)

@spec 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}}

permanent_redirect(message)

@spec 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"}

permanent_redirect(message, details)

@spec 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}}

precondition_failed(message)

@spec 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"}

precondition_failed(message, details)

@spec 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}}

precondition_required(message)

@spec 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"}

precondition_required(message, details)

@spec 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}}

proxy_authentication_required(message)

@spec 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"}

proxy_authentication_required(message, details)

@spec 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}}

request_entity_too_large(message)

@spec 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"}

request_entity_too_large(message, details)

@spec 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}}

request_header_fields_too_large(message)

@spec 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"}

request_header_fields_too_large(message, details)

@spec 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}}

request_timeout(message)

@spec 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"}

request_timeout(message, details)

@spec 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}}

request_uri_too_long(message)

@spec 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"}

request_uri_too_long(message, details)

@spec 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}}

requested_range_not_satisfiable(message)

@spec 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"}

requested_range_not_satisfiable(message, details)

@spec 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}}

see_other(message)

@spec 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"}

see_other(message, details)

@spec 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}}

service_unavailable(message)

@spec 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"}

service_unavailable(message, details)

@spec 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}}

switch_proxy(message)

@spec 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"}

switch_proxy(message, details)

@spec 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}}

temporary_redirect(message)

@spec 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"}

temporary_redirect(message, details)

@spec 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}}

to_jsonable_map(error_message)

@spec 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"]}}
    }
  }

to_string(error_message)

@spec 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}"

too_early(message)

@spec 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"}

too_early(message, details)

@spec 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}}

too_many_requests(message)

@spec 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"}

too_many_requests(message, details)

@spec 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}}

unauthorized(message)

@spec 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"}

unauthorized(message, details)

@spec 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}}

unprocessable_entity(message)

@spec 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"}

unprocessable_entity(message, details)

@spec 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}}

unsupported_media_type(message)

@spec 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"}

unsupported_media_type(message, details)

@spec 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}}

upgrade_required(message)

@spec 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"}

upgrade_required(message, details)

@spec 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}}

use_proxy(message)

@spec 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"}

use_proxy(message, details)

@spec 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}}

variant_also_negotiates(message)

@spec 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"}

variant_also_negotiates(message, details)

@spec 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}}