MishkaGervaz.Errors (MishkaGervaz v0.0.1-alpha.3)

Copy Markdown View Source

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

class()

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

class_module()

@type class_module() ::
  MishkaGervaz.Errors.Action | MishkaGervaz.Errors.Data | Splode.Error.Unknown

error_class()

@type error_class() :: :action | :data | :unknown

t()

@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

extract_error_message(error)

@spec extract_error_message(any()) :: String.t()

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"

format_flash_message(error)

@spec format_flash_message(any()) :: String.t()

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"

splode_error?(arg1, splode)

unwrap!(result, opts \\ nil)

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 to to_error/2 when 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}
end

end

def function!(arg) do

YourErrors.unwrap!(function(arg))

end