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

Hierarchy

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.

Python
result = gateway.customer.create({
    "email": "invalid_email",
    "credit_card": {
        "number": "not_numeric",
        "billing_address": {
            "country_name": "not_a_valid_country"
        }
    }
})
This code snippet now uses gateway instance methods instead of class-level methods. Learn more.

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 levels

To get errors on all levels, you can iterate over result.errors.deep_errors

Python
for error in result.errors.deep_errors:
    print(error.code)
    print(error.message)

Errors at specific levels

To get errors at a specific level, use the for_object method. The attribute will indicate which attribute is invalid.

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.

Python
# number of errors on customer
result.errors.for_object("customer").size
len(result.errors.for_object("customer"))

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

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

Errors on specific attribute

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.

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 errors

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.

Python
result = gateway.subscription.update("the_subscription_id", {
    "price": "10.00"
})

for error in result.errors.for_object("subscription").on("base"):
    print(error.message)
This code snippet now uses gateway instance methods instead of class-level methods. Learn more.

Errors on add-ons/discounts

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).

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)
This code snippet now uses gateway instance methods instead of class-level methods. Learn more.

See also