ErrorMessage (error_message v0.3.2) View Source
ErrorMessage
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
Specs
Specs
Specs
Specs
t_ok_res() :: :ok | {:error, t()}
Specs
t_ok_res(details_type) :: :ok | {:error, t(details_type)}
Specs
Specs
t_res(result_type) :: {:ok, result_type} | {:error, t()}
Specs
t_res(result_type, details_type) :: {:ok, result_type} | {:error, t(details_type)}
Link to this section Functions
Specs
Create bad_gateway error message for status code 502
Example
iex> ErrorMessage.bad_gateway("error message")
%ErrorMessage{code: :bad_gateway, message: "error message"}
Specs
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
Create bad_request error message for status code 400
Example
iex> ErrorMessage.bad_request("error message")
%ErrorMessage{code: :bad_request, message: "error message"}
Specs
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
Create conflict error message for status code 409
Example
iex> ErrorMessage.conflict("error message")
%ErrorMessage{code: :conflict, message: "error message"}
Specs
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}}
Specs
Create expectation_failed error message for status code 417
Example
iex> ErrorMessage.expectation_failed("error message")
%ErrorMessage{code: :expectation_failed, message: "error message"}
Specs
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}}
Specs
Create failed_dependency error message for status code 424
Example
iex> ErrorMessage.failed_dependency("error message")
%ErrorMessage{code: :failed_dependency, message: "error message"}
Specs
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
Create forbidden error message for status code 403
Example
iex> ErrorMessage.forbidden("error message")
%ErrorMessage{code: :forbidden, message: "error message"}
Specs
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
Create found error message for status code 302
Example
iex> ErrorMessage.found("error message")
%ErrorMessage{code: :found, message: "error message"}
Specs
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}}
Specs
Create gateway_timeout error message for status code 504
Example
iex> ErrorMessage.gateway_timeout("error message")
%ErrorMessage{code: :gateway_timeout, message: "error message"}
Specs
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
Create gone error message for status code 410
Example
iex> ErrorMessage.gone("error message")
%ErrorMessage{code: :gone, message: "error message"}
Specs
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
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
Specs
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"}
Specs
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
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"}
Specs
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}}
Specs
Create insufficient_storage error message for status code 507
Example
iex> ErrorMessage.insufficient_storage("error message")
%ErrorMessage{code: :insufficient_storage, message: "error message"}
Specs
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}}
Specs
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"}
Specs
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}}
Specs
Create length_required error message for status code 411
Example
iex> ErrorMessage.length_required("error message")
%ErrorMessage{code: :length_required, message: "error message"}
Specs
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
Create locked error message for status code 423
Example
iex> ErrorMessage.locked("error message")
%ErrorMessage{code: :locked, message: "error message"}
Specs
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
Create loop_detected error message for status code 508
Example
iex> ErrorMessage.loop_detected("error message")
%ErrorMessage{code: :loop_detected, message: "error message"}
Specs
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}}
Specs
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"}
Specs
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}}
Specs
Create misdirected_request error message for status code 421
Example
iex> ErrorMessage.misdirected_request("error message")
%ErrorMessage{code: :misdirected_request, message: "error message"}
Specs
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}}
Specs
Create moved_permanently error message for status code 301
Example
iex> ErrorMessage.moved_permanently("error message")
%ErrorMessage{code: :moved_permanently, message: "error message"}
Specs
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}}
Specs
Create multiple_choices error message for status code 300
Example
iex> ErrorMessage.multiple_choices("error message")
%ErrorMessage{code: :multiple_choices, message: "error message"}
Specs
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}}
Specs
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"}
Specs
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
Create not_acceptable error message for status code 406
Example
iex> ErrorMessage.not_acceptable("error message")
%ErrorMessage{code: :not_acceptable, message: "error message"}
Specs
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
Create not_extended error message for status code 510
Example
iex> ErrorMessage.not_extended("error message")
%ErrorMessage{code: :not_extended, message: "error message"}
Specs
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
Create not_found error message for status code 404
Example
iex> ErrorMessage.not_found("error message")
%ErrorMessage{code: :not_found, message: "error message"}
Specs
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}}
Specs
Create not_implemented error message for status code 501
Example
iex> ErrorMessage.not_implemented("error message")
%ErrorMessage{code: :not_implemented, message: "error message"}
Specs
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
Create not_modified error message for status code 304
Example
iex> ErrorMessage.not_modified("error message")
%ErrorMessage{code: :not_modified, message: "error message"}
Specs
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}}
Specs
Create payment_required error message for status code 402
Example
iex> ErrorMessage.payment_required("error message")
%ErrorMessage{code: :payment_required, message: "error message"}
Specs
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}}
Specs
Create permanent_redirect error message for status code 308
Example
iex> ErrorMessage.permanent_redirect("error message")
%ErrorMessage{code: :permanent_redirect, message: "error message"}
Specs
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}}
Specs
Create precondition_failed error message for status code 412
Example
iex> ErrorMessage.precondition_failed("error message")
%ErrorMessage{code: :precondition_failed, message: "error message"}
Specs
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}}
Specs
Create precondition_required error message for status code 428
Example
iex> ErrorMessage.precondition_required("error message")
%ErrorMessage{code: :precondition_required, message: "error message"}
Specs
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}}
Specs
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"}
Specs
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}}
Specs
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"}
Specs
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}}
Specs
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"}
Specs
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}}
Specs
Create request_timeout error message for status code 408
Example
iex> ErrorMessage.request_timeout("error message")
%ErrorMessage{code: :request_timeout, message: "error message"}
Specs
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}}
Specs
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"}
Specs
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}}
Specs
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"}
Specs
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
Create see_other error message for status code 303
Example
iex> ErrorMessage.see_other("error message")
%ErrorMessage{code: :see_other, message: "error message"}
Specs
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}}
Specs
Create switch_proxy error message for status code 306
Example
iex> ErrorMessage.switch_proxy("error message")
%ErrorMessage{code: :switch_proxy, message: "error message"}
Specs
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}}
Specs
Create temporary_redirect error message for status code 307
Example
iex> ErrorMessage.temporary_redirect("error message")
%ErrorMessage{code: :temporary_redirect, message: "error message"}
Specs
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}}
Specs
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"]}}
}
}
Specs
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
Create too_early error message for status code 425
Example
iex> ErrorMessage.too_early("error message")
%ErrorMessage{code: :too_early, message: "error message"}
Specs
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}}
Specs
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"}
Specs
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
Create unauthorized error message for status code 401
Example
iex> ErrorMessage.unauthorized("error message")
%ErrorMessage{code: :unauthorized, message: "error message"}
Specs
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}}
Specs
Create unprocessable_entity error message for status code 422
Example
iex> ErrorMessage.unprocessable_entity("error message")
%ErrorMessage{code: :unprocessable_entity, message: "error message"}
Specs
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}}
Specs
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"}
Specs
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}}
Specs
Create upgrade_required error message for status code 426
Example
iex> ErrorMessage.upgrade_required("error message")
%ErrorMessage{code: :upgrade_required, message: "error message"}
Specs
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
Create use_proxy error message for status code 305
Example
iex> ErrorMessage.use_proxy("error message")
%ErrorMessage{code: :use_proxy, message: "error message"}
Specs
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}}
Specs
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"}
Specs
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}}