#ronho
1 messages · Page 1 of 1 (latest)
try:
customer = stripe.Customer.retrieve(user.customer_id,
expand=['invoice_settings.default_payment_method', 'tax']
)
except Exception as e:
logger.exception(f"Could not retrieve customer {user.id}", extra={"user": user})
# This should get caught by the caller
raise e
specifically it errors on the 'expand' line
i can post the trace if needed too
This is an error from the SDK locally then, not from the API, right?
What is the exception you get?
{"message": "Could not retrieve customer xxxxx", "exc_info": "Traceback (most recent call last):\n File \"../views.py\", line 176, in _get_or_create_stripe_customer\n customer = stripe.Customer.retrieve(user.customer_id,\n File \"venv/lib/python3.8/site-packages/stripe/api_resources/abstract/api_resource.py\", line 12, in retrieve\n instance.refresh()\n File \"venv/lib/python3.8/site-packages/stripe/api_resources/abstract/api_resource.py\", line 16, in refresh\n return self._request_and_refresh(\"get\", self.instance_url())\n File \"venv/lib/python3.8/site-packages/stripe/api_resources/abstract/api_resource.py\", line 90, in _request_and_refresh\n obj = StripeObject._request(\n File \"venv/lib/python3.8/site-packages/stripe/stripe_object.py\", line 282, in _request\n response, api_key = requestor.request(method_, url_, params, headers)\n File \"venv/lib/python3.8/site-packages/stripe/api_requestor.py\", line 122, in request\n resp = self.interpret_response(rbody, rcode, rheaders)\n File \"venv/lib/python3.8/site-packages/stripe/api_requestor.py\", line 399, in interpret_response\n self.handle_error_response(rbody, rcode, resp.data, rheaders)\n File \"venv/lib/python3.8/site-packages/stripe/api_requestor.py\", line 159, in handle_error_response\n raise err\nstripe.error.InvalidRequestError: Request req_CIqSFQQmzgMm6F: No such customer: '1234565'", "timestamp": "2023-11-29 20:26:58.846745", "level": "ERROR"}
Request req_CIqSFQQmzgMm6F: No such customer: '1234565'",
That's not a valid customer ID
yeah correct
thats an expected case as we have different vendors for the customers depending on where they signed up
Looks like you swapped out the value for a placeholder, but the actual ID in your request is also invalid
so im just trying to catch the error
yeah i did
ooh, gotcha
my caller to that function does do another try except
so it should be caught all the way up the stack
but the code seems to indicate it has some issue specifically on that expand line?
like normally i would expect the error to show the trace going up the stack to the finally area where it gets caught, but in this case, it just seems to die right at the retrieve
Can you try using except stripe.error.InvalidRequestError: as shown here:
https://stripe.com/docs/error-handling?lang=python#catch-exceptions
ok one second
oh ok
seems like i have to catch that specific error
sorry i spoke to soon, it still seems to throw..
Yea i think the general Exception would be for errors we don't have specific types for
So you should handle our types then fall back to exception
I don't really understand why that wouldnt catch as expected
ok sorry i think i figured it out
it was related to the logging package and how it handles when i mark it as an exception
thanks for your assistance, it wasn't a stripe thing after all 😅
oh one more question
as far as i know, stripe customers always begins with cus_ ?
and it shouldn't really change ?
i will detect if its a non stripe customer before attempting to retrieve
I don't expect us to change this, BUT
we do say you should not over-index on our ID structures, they are subject to change without considering that a breaking change
ok
so i could potentially catch the 'customer not found' error instead of checking for prefix
https://stripe.com/docs/upgrades#what-changes-does-stripe-consider-to-be-backwards-compatible
Backwards-compatible changes
Stripe considers the following changes to be backwards-compatible:[...]
Changing the length or format of opaque strings, such as object IDs, error messages, and other human-readable strings.
This includes adding or removing fixed prefixes (such as ch_ on charge IDs).
You can safely assume object IDs we generate will never exceed 255 characters, but you should be able to handle IDs of up to that length. If for example you’re using MySQL, you should store IDs in a VARCHAR(255) COLLATE utf8_bin column (the COLLATE configuration ensures case-sensitivity in lookups).
yes
thanks synthrider!
NP! happy to help 🙂