Validation Errors

Overview

Validation errors will be returned on error result objects when we can't process the API call because parameters are invalid. Validation errors contain the following:
  • code - for programmatic consumption
  • message - for human consumption
  • attribute - the parameter that caused an error

HierarchyAnchorIcon

Errors are returned in a hierarchy that matches the parameter hierarchy. For example, when creating a customer with a credit card and billing address, the credit card is nested under customer, and the billing address under credit card.
  1. Python
result = gateway.customer.create({
  email: "invalid_email",
  credit_card: {
    number: "not_numeric",
    billing_address: {
      country_name: "not_a_valid_country"
    }
  }
})
You can get errors at all levels, errors at a specific level, or errors at a specific level on a specific attribute.

All errors on all levelsAnchorIcon

To get errors on _all levels_, you can iterate over `result.errors.deep_errors`
  1. Python
for error in result.errors.deep_errors:
  print(error.code)
  print(error.message)

Errors at specific levelsAnchorIcon

To get errors at a _specific level_, use the `for_object` method. The `attribute` will indicate which attribute is invalid.
  1. Python
for error in result.errors.for_object("customer"):
  print(error.attribute)
  print(error.code)
  print(error.message)

for error in result.errors.for_object("customer").for_object("credit_card"):
  print(error.attribute)
  print(error.code)
  print(error.message)

for error in result.errors.for_object("customer").for_object("credit_card").for_object("billing_address"):
  print(error.attribute)
  print(error.code)
  print(error.message)
From a _specific_ level, you can also get the number of errors at that level using `size`.
  1. Python
# number of errors on customer
len(result.errors.for_object("customer"))

# number of errors on credit card
len(result.errors.for_object("customer").for_object("credit_card"))

# number of errors on billing address
len(result.errors.for_object("customer").for_object("credit_card").for_object("billing_address"))

Errors on specific attributeAnchorIcon

You can also get errors at a specific level on a specific attribute. This is useful if you want to display error messages inline in your forms.
  1. Python
result.errors.for_object("customer").on("email")
result.errors.for_object("customer").for_object("credit_card").on("number")
result.errors.for_object("customer").for_object("credit_card").for_object("billing_address").on("country_name")

Base errorsAnchorIcon

Sometimes validation errors aren't caused by a specific input parameter. For example, canceled subscriptions can't be updated. For these validation errors, we use an attribute named "base" for the validation error.
  1. Python
result = gateway.subscription.update("the_subscription_id", {
  price: "10.00"
})

for error in result.errors.for_object("subscription").on("base"):
  print(error.message)

Errors on add-ons/discountsAnchorIcon

It is possible to add, update and remove many add-ons and discounts at once. If any of the add-ons or discounts contain errors, these errors will be indexed based on the order of the add-on or discount in the request (beginning at 0).
  1. Python
result = gateway.subscription.create({
  payment_method_token: "the_payment_method_token",
  plan_id: "the_plan_id",
  add_ons: {
    update: [
      {
        existing_id: "increase_10",
        amount: "invalid"
      },
      {
        existing_id: "increase_20",
        quantity: -2
      }
    ]
  }
})

for error in result.errors.for_object("subscription").for_object("add_ons").for_object("update").for_index(0).on("amount"):
  print(error.message)

for error in result.errors.for_object("subscription").for_object("add_ons").for_object("update").for_index(1).on("quantity"):
  print(error.message)

See AlsoAnchorIcon