Trogon.Error (Trogon.Error v0.1.1)
View SourceUniversal Error Specification implementation for Elixir.
This module provides a use
macro to define structured exceptions
following the Universal Error Specification ADR.
Usage
defmodule MyApp.NotFoundError do
use Trogon.Error,
domain: "com.myapp.mydomain",
reason: "not_found",
message: "The {resource} was not found"
end
Creating Errors
MyApp.NotFoundError.new!(
metadata: %{resource: "user"}
)
Summary
Types
@type code() ::
:cancelled
| :unknown
| :invalid_argument
| :deadline_exceeded
| :not_found
| :already_exists
| :permission_denied
| :unauthenticated
| :resource_exhausted
| :failed_precondition
| :aborted
| :out_of_range
| :unimplemented
| :internal
| :unavailable
| :data_loss
@type error_opts() :: [ metadata: map() | nil, causes: [t(module())] | nil, subject: subject() | nil, debug_info: debug_info() | nil, localized_message: localized_message() | nil, retry_info: retry_info() | nil, id: id() | nil, time: time() | nil, source_id: source_id() | nil ]
@type help() :: %{links: [help_link()]}
@type id() :: String.t()
@type retry_info() :: retry_info_duration()
@type retry_info_duration() :: %{retry_offset: Duration.t()}
@type source_id() :: String.t()
@type subject() :: String.t()
@type t(struct) :: %{ __struct__: struct, __exception__: true, specversion: non_neg_integer(), code: code(), message: String.t(), domain: String.t(), reason: String.t(), metadata: %{required(String.t()) => String.t()}, causes: [t(module())], visibility: visibility(), subject: subject() | nil, id: id() | nil, time: time() | nil, help: help() | nil, debug_info: debug_info() | nil, localized_message: localized_message() | nil, retry_info: retry_info() | nil, source_id: source_id() | nil }
@type time() :: DateTime.t()
@type visibility() :: :internal | :private | :public
Functions
Defines an error module with the given options.
Options
:domain
(String.t/0
) - Required. The error domain identifying the service or component that generated the error:reason
(String.t/0
) - Required. A unique identifier for the specific error within the domain:message
- The error message, either an atom that maps to a standard message or a custom string:metadata
(map/0
) - Default metadata to be merged with runtime metadata. The default value is%{}
.:code
- The standard error code The default value is:unknown
.:visibility
- Whether the error should be visible to end users or kept internal The default value is:internal
.:help
(map/0
) - Help information with links to documentation:links
(list ofmap/0
)
@spec to_code_int(atom() | t(module())) :: non_neg_integer()
Converts an atom to an integer code.
Examples
iex> Trogon.Error.to_code_int(:cancelled)
1
iex> err = TestSupport.Errors.InvalidCurrencyError.new!()
...> Trogon.Error.to_code_int(err)
2