Splode-based error handling for MishkaGervaz.
Error Classes
:data- Data loading, query, and fetch errors:action- Action execution errors (destroy, update, etc.)
Usage
# Raise an error
raise MishkaGervaz.Errors.Data.LoadFailed, resource: MyResource, reason: :timeout
# Create error without raising
error = MishkaGervaz.Errors.Data.LoadFailed.exception(resource: MyResource, reason: :timeout)
# Convert any value into a Splode error (unrecognized values become `Errors.Unknown`)
MishkaGervaz.Errors.to_error(error)
# Format error for flash message
MishkaGervaz.Errors.format_flash_message(error)
Summary
Functions
Extracts a human-readable message from various error formats.
Formats an error into a human-readable flash message.
Raises an error if the result is an error, otherwise returns the result
Types
@type class() :: %{ :__struct__ => class_module(), :__exception__ => true, :errors => [t()], :class => error_class(), :bread_crumbs => [String.t()], :vars => Keyword.t(), :stacktrace => Splode.Stacktrace.t() | nil, :context => map(), optional(atom()) => any() }
@type class_module() ::
MishkaGervaz.Errors.Action | MishkaGervaz.Errors.Data | Splode.Error.Unknown
@type error_class() :: :action | :data | :unknown
@type t() :: %{ :__struct__ => module(), :__exception__ => true, :class => error_class(), :bread_crumbs => [String.t()], :vars => Keyword.t(), :stacktrace => Splode.Stacktrace.t() | nil, :context => map(), optional(atom()) => any() }
Functions
Extracts a human-readable message from various error formats.
Examples
iex> MishkaGervaz.Errors.extract_error_message(%{message: "Invalid email"})
"Invalid email"
iex> MishkaGervaz.Errors.extract_error_message(%{field: :email, message: "is invalid"})
"email: is invalid"
Formats an error into a human-readable flash message.
Handles MishkaGervaz errors, Ash errors, and generic errors.
Examples
iex> error = MishkaGervaz.Errors.Action.Failed.exception(action: :archive, reason: "forbidden")
iex> MishkaGervaz.Errors.format_flash_message(error)
"Archive failed: forbidden"
Raises an error if the result is an error, otherwise returns the result
Alternatively, you can use the defsplode macro, which does this automatically.
Options
:error_opts- Options to pass toto_error/2when converting the returned error:unknown_error_opts- Options to pass to the unknown error if the function returns only:error. not necessary if your function always returns{:error, error}.
Examples
def function(arg) do
case do_something(arg) do
:success -> :ok
{:success, result} -> {:ok, result}
{:error, error} -> {:error, error}
endend
def function!(arg) do
YourErrors.unwrap!(function(arg))end