# `MishkaPay.Errors`
[🔗](https://github.com/mishka-group/mishka_pay/blob/v0.0.1/lib/mishka_pay/errors.ex#L5)

Every error Mishka Pay returns, built with [Splode](https://hex.pm/packages/splode), the error
library Ash is built on.

| Class | Module | When |
|---|---|---|
| `:invalid` | `MishkaPay.Errors.Invalid` | the input is wrong: a guarded_struct validation error, for example |
| `:provider` | `MishkaPay.Errors.Provider` | the provider refused or cannot do it: `Declined`, `Unsupported` |
| `:unknown` | `MishkaPay.Errors.Unknown` | anything else, as an `UnknownError` |

guarded_struct's errors (`GuardedStruct.Errors`) are merged in, so a failed `builder/1` joins
Mishka Pay's own errors with no conversion:

    MishkaPay.Errors.from_builder(MishkaPay.Money.builder(%{amount: -1, currency: "usd"}))
    #=> {:error, %MishkaPay.Errors.Invalid{errors: [%GuardedStruct.Errors.Validation{field: :amount}]}}

When errors of several classes are combined, the class listed first in `error_classes` wins.

# `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() ::
  MishkaPay.Errors.Unknown
  | MishkaPay.Errors.Provider
  | MishkaPay.Errors.Invalid
```

# `error_class`

```elixir
@type error_class() :: :unknown | :provider | :invalid
```

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

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

A guarded_struct `builder/1` answer with its errors as Mishka Pay's: `{:ok, struct}` stays, and
`{:error, list}` becomes `{:error, %MishkaPay.Errors.Invalid{}}` holding guarded_struct's
validation errors.

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