# `GuardedStruct.Errors`
[🔗](https://github.com/mishka-group/guarded_struct/blob/v0.1.4/lib/guarded_struct/errors.ex#L1)

Splode error aggregator for GuardedStruct runtime errors.

`builder/1` returns errors as `{:error, [%{field, action, message, ...}]}`.
This module wraps that list into Splode exceptions, giving you
`traverse_errors/2`, `to_class/1`, `set_path/2`, and JSON serialization.

## Usage

    case MyStruct.builder(input) do
      {:ok, _} = ok ->
        ok

      {:error, errs} ->
        {:error, GuardedStruct.Errors.from_tuple(errs)}
    end

Or build a single error directly:

    GuardedStruct.Errors.Validation.exception(
      field: :email,
      action: :email_r,
      message: "Invalid email format"
    )

# `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() :: GuardedStruct.Errors.Invalid | Splode.Error.Unknown
```

# `error_class`

```elixir
@type error_class() :: :invalid | :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()
}
```

# `from_tuple`

```elixir
@spec from_tuple({:error, list()} | list()) :: Splode.Error.t()
```

Convert an error tuple list into a Splode error class. Accepts either the
inner list or the full `{:error, list}` tuple.

# `splode_error?`

# `traverse_errors`

Traverses errors, calling `fun` for each leaf error, and returns a nested
map of results grouped by each error's `path`.

See `Splode.traverse_errors/2` for full documentation.

## Example

    iex> Elixir.GuardedStruct.Errors.traverse_errors(error, fn error ->
    ...>   Exception.message(error)
    ...> end)
    %{name: ["name is required"]}

# `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*
