# `MishkaGervaz.Errors`
[🔗](https://github.com/mishka-group/mishka_gervaz/blob/v0.0.1-alpha.3/lib/mishka_gervaz/errors.ex#L1)

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)

# `class`

```elixir
@type class() :: %{
  :__struct__ =&gt; class_module(),
  :__exception__ =&gt; true,
  :errors =&gt; [t()],
  :class =&gt; error_class(),
  :bread_crumbs =&gt; [String.t()],
  :vars =&gt; Keyword.t(),
  :stacktrace =&gt; Splode.Stacktrace.t() | nil,
  :context =&gt; map(),
  optional(atom()) =&gt; any()
}
```

# `class_module`

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

# `error_class`

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

# `t`

```elixir
@type t() :: %{
  :__struct__ =&gt; module(),
  :__exception__ =&gt; true,
  :class =&gt; error_class(),
  :bread_crumbs =&gt; [String.t()],
  :vars =&gt; Keyword.t(),
  :stacktrace =&gt; Splode.Stacktrace.t() | nil,
  :context =&gt; map(),
  optional(atom()) =&gt; any()
}
```

# `extract_error_message`

```elixir
@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`

```elixir
@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?`

# `unwrap!`

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

---

*Consult [api-reference.md](api-reference.md) for complete listing*
