Trogon.Error (Trogon.Error v0.1.0)

View Source

Universal 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

Functions

Defines an error module with the given options.

Converts an atom to an integer code.

Types

code()

@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

debug_info()

@type debug_info() :: %{
  stack_entries: [String.t()],
  metadata: %{required(String.t()) => String.t()}
}

error_opts()

@type error_opts() :: [
  code: code(),
  message: String.t(),
  metadata: map(),
  causes: [t(module())],
  subject: subject(),
  debug_info: debug_info(),
  localized_message: localized_message(),
  retry_info: retry_info(),
  id: id(),
  time: time(),
  help: help(),
  visibility: visibility(),
  source_id: source_id()
]

help()

@type help() :: %{links: [help_link()]}

help_link()

@type help_link() :: %{description: String.t(), url: String.t()}

id()

@type id() :: String.t()

localized_message()

@type localized_message() :: %{locale: String.t(), message: String.t()}

retry_info()

@type retry_info() :: retry_info_duration()

retry_info_duration()

@type retry_info_duration() :: %{retry_offset: Duration.t()}

source_id()

@type source_id() :: String.t()

subject()

@type subject() :: String.t()

t(struct)

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

time()

@type time() :: DateTime.t()

visibility()

@type visibility() :: :internal | :private | :public

Functions

__using__(opts)

(macro)

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

to_code_int(arg1)

@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

to_msg(msg)

@spec to_msg(atom() | String.t()) :: String.t()

validate_options!(opts)

@spec validate_options!(keyword()) :: keyword()