MishkaPay.Errors (Mishka Pay v0.0.1)

Copy Markdown View Source

Every error Mishka Pay returns, built with Splode, the error library Ash is built on.

ClassModuleWhen
:invalidMishkaPay.Errors.Invalidthe input is wrong: a guarded_struct validation error, for example
:providerMishkaPay.Errors.Providerthe provider refused or cannot do it: Declined, Unsupported
:unknownMishkaPay.Errors.Unknownanything 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

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

error_class()

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

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

from_builder(ok)

@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?(arg1, splode)

traverse_errors(error_or_errors, fun)

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!(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