Every error Mishka Pay returns, built with 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.
Summary
Functions
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.
Traverses errors, calling fun for each leaf error, and returns a nested
map of results grouped by each error's path.
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() ::
MishkaPay.Errors.Unknown
| MishkaPay.Errors.Provider
| MishkaPay.Errors.Invalid
@type error_class() :: :unknown | :provider | :invalid
@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
@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.
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"]}
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