Mercadopago.Error exception (mercadopago_sdk_elixir v0.3.2)

Copy Markdown View Source

An API error response, produced by Mercadopago.HTTP.unwrap/1 and raised by Mercadopago.Pagination.stream/3.

The SDK's own resource functions never return this: they report every completed response as {:ok, %{status: _, response: _}}, errors included. Use it when you would rather branch on {:ok, _} / {:error, _} than on the status code.

Match on :kind rather than the raw status when you care about the class of failure:

case client |> Mercadopago.Payment.get(id) |> Mercadopago.HTTP.unwrap() do
  {:ok, payment} -> payment
  {:error, %Mercadopago.Error{kind: :not_found}} -> nil
  {:error, %Mercadopago.Error{kind: :rate_limit, retry_after: seconds}} -> back_off(seconds)
  {:error, %Mercadopago.Error{kind: :server, request_id: id}} -> escalate(id)
end

Summary

Types

The class of failure, derived from the HTTP status. :api covers any status the API uses that has no more specific meaning.

t()

Functions

The class of failure for an HTTP status. See kind/0.

Builds an error from a status and a decoded response body.

Types

kind()

@type kind() ::
  :bad_request
  | :authentication
  | :payment
  | :forbidden
  | :not_found
  | :idempotency
  | :validation
  | :resource_locked
  | :dependency
  | :rate_limit
  | :server
  | :api

The class of failure, derived from the HTTP status. :api covers any status the API uses that has no more specific meaning.

t()

@type t() :: %Mercadopago.Error{
  __exception__: term(),
  cause: term(),
  kind: kind(),
  message: String.t(),
  request_id: String.t() | nil,
  response: map() | list() | binary() | nil,
  retry_after: non_neg_integer() | nil,
  status: non_neg_integer()
}

Functions

kind_for(status)

@spec kind_for(non_neg_integer()) :: kind()

The class of failure for an HTTP status. See kind/0.

new(status, response, opts \\ [])

@spec new(non_neg_integer(), map() | list() | binary() | nil, keyword()) :: t()

Builds an error from a status and a decoded response body.

MercadoPago is inconsistent about which key carries the human-readable text, so message falls back through "message", "error" and finally a generic string naming the status. The untouched body is always kept in :response.

Options

  • :request_id - the x-request-id the API answered with, the identifier MercadoPago support asks for
  • :retry_after - seconds from the Retry-After header, when it sent one