EasyRpc.Error exception (EasyRpc v0.9.1)

Copy Markdown View Source

Unified error handling for EasyRpc library.

Provides structured errors with typed categories, optional detail metadata, and helpers for creation, logging, formatting, and wrapping raw exceptions.

Error Types

  • :config_error - Configuration validation errors
  • :rpc_error - Remote procedure call errors
  • :node_error - Node selection or availability errors
  • :timeout_error - RPC timeout errors
  • :validation_error - Input validation errors

Examples

iex> EasyRpc.Error.config_error("Invalid timeout value")
%EasyRpc.Error{type: :config_error, message: "Invalid timeout value", details: nil}

iex> EasyRpc.Error.rpc_error("Connection refused", node: :node1@host)
%EasyRpc.Error{type: :rpc_error, message: "Connection refused", details: [node: :node1@host]}

Summary

Functions

Creates a configuration error.

Formats the error as a human-readable string.

Logs the error at the given level (default :error).

Creates a node error.

Raises an error by type + message, or directly from an existing %EasyRpc.Error{}.

Creates an RPC error.

Creates a timeout error.

Creates a validation error.

Wraps a rescued exception into an EasyRpc.Error, preserving the original struct name in :details.

Types

error_type()

@type error_type() ::
  :config_error | :rpc_error | :node_error | :timeout_error | :validation_error

t()

@type t() :: %EasyRpc.Error{
  __exception__: true,
  details: keyword() | map() | nil,
  message: String.t(),
  type: error_type()
}

Functions

config_error(message, details \\ [])

@spec config_error(
  String.t() | term(),
  keyword()
) :: t()

Creates a configuration error.

format(error)

@spec format(t()) :: String.t()

Formats the error as a human-readable string.

Examples

iex> EasyRpc.Error.format(EasyRpc.Error.config_error("Invalid timeout"))
"[config_error] Invalid timeout"

iex> EasyRpc.Error.format(EasyRpc.Error.rpc_error("Refused", node: :n1@h))
"[rpc_error] Refused | details: [node: :n1@h]"

log(error, level \\ :error)

@spec log(t(), :error | :warning | :info | :debug) :: :ok

Logs the error at the given level (default :error).

node_error(message, details \\ [])

@spec node_error(
  String.t() | term(),
  keyword()
) :: t()

Creates a node error.

raise!(error)

@spec raise!(t()) :: no_return()

raise!(type, message, details \\ [])

@spec raise!(error_type(), String.t(), keyword()) :: no_return()

Raises an error by type + message, or directly from an existing %EasyRpc.Error{}.

Examples

EasyRpc.Error.raise!(:config_error, "Invalid config")
EasyRpc.Error.raise!(error_struct)

rpc_error(message, details \\ [])

@spec rpc_error(
  String.t() | term(),
  keyword()
) :: t()

Creates an RPC error.

timeout_error(message, details \\ [])

@spec timeout_error(
  String.t() | term(),
  keyword()
) :: t()

Creates a timeout error.

validation_error(message, details \\ [])

@spec validation_error(
  String.t() | term(),
  keyword()
) :: t()

Creates a validation error.

wrap_exception(exception, details \\ [])

@spec wrap_exception(
  Exception.t(),
  keyword()
) :: t()

Wraps a rescued exception into an EasyRpc.Error, preserving the original struct name in :details.

Examples

try do
  :erpc.call(node, mod, fun, args)
rescue
  e -> EasyRpc.Error.wrap_exception(e, node: node)
end