Closed rechner closed 1 year ago
@rechner,
I don't know anything about the test values. But, I came across this issue because I was trying to use the is_error()
pattern and getting an unhandled exception.
I can't speak for the Square devs, but after poking around the code I found that catching the APIException
in an except
block seems to be the new pattern for checking response status. The API errors you were expecting can be found in APIException.response.text
.
try:
result = client.locations.list_locations()
except APIException as e:
errors = json.loads(e.response.text)['errors']
I don't know if this helps you, but it would have helped me. Hopefully the devs can comment on whether or not this is a bug or a feature. If this is intentional, the SDK docs could really use some updating.
Hi @rechner and @icarlthiscode
@rechner thanks so much for the detailed issue description, it made getting up to speed on this a lot easier.
@icarlthiscode thanks for adding some additional thoughts and helping out!
@rechner I confirmed this issue exists in the sdk versions you mentioned about, and I have also confirmed that this has been fixed. Checking 25.2.0.20230315
and the issue has been resolved.
If you run into further issues with this, please consider posting on our Developer Forums - These forums are actively monitored by Square employees as well as has engagement from the community. You might also find your issue has been posted here before and answered!
Cheers!
Describe the bug Regression in latest SDK version with payments API raises an exception for test values which indicate a payment failure.
Expected behavior Following the create payment example from the documentation should not result in an exception when payment fails, instead following the
is_error()
pattern and only raising an exception when there is an error in making the request.To Reproduce Steps to reproduce the bug:
square.exceptions.api_exception.APIException: HTTP response not OK.
An example payload body and code which produces this error, but works on the previous SDK version:
Full traceback:
Screenshots
Example code
```python import os import uuid import pprint from square.client import Client client = Client( access_token=os.environ['SQUARE_ACCESS_TOKEN'], environment='sandbox' ) result = client.locations.list_locations() if result.is_success(): location_id = result.body['locations'][0]['id'] else: print(result.errors) exit() payments_api = client.payments body = {} body['source_id'] = 'cnon:card-nonce-rejected-cvv' body['idempotency_key'] = str(uuid.uuid4()) body['amount_money'] = {} body['amount_money']['amount'] = 1000 body['amount_money']['currency'] = 'USD' body['app_fee_money'] = {} body['app_fee_money']['amount'] = 10 body['app_fee_money']['currency'] = 'USD' body['autocomplete'] = True body['location_id'] = location_id body['reference_id'] = '123456' body['note'] = 'Brief description' print("Request body:") pprint.pprint(body) result = payments_api.create_payment(body) if result.is_success(): print(result.body) elif result.is_error(): print(result.errors) ```Square SDK version
23.0.0.20221019
, tested also with24.0.0.20221116
and found to have the same issue.Additional context I use these premade nonce values to test error conditions in my code. Tests pass on version 22 of the SDK.