billing: Simplify logging of Stripe errors.

Several changes:
* De-duplicate code for different error types.
* No need to list lots of error subtypes where we aren't treating
  them differently; StripeError is the base class of them all.
* Unexpected, non-Stripe-related, exceptions we can handle in the normal
  way.  Just make them show up in the billing-specific log too.
* The Stripe client library already logs type, code, param, and message
  before raising an error, so we don't need to repeat those; just add the
  HTTP status code (because it's not there already and sure why not),
  and the Python exception type the client library chose to raise
  in case that makes things a bit easier to interpret.
This commit is contained in:
Greg Price 2018-01-17 18:05:27 -08:00
parent 858b9d7bc1
commit 1ef2d9d637
1 changed files with 9 additions and 15 deletions

View File

@ -177,20 +177,14 @@ def add_payment_method(request: HttpRequest) -> HttpResponse:
ctx["num_cards"] = 1
ctx["payment_method_added"] = True
return render(request, 'zilencer/payment.html', context=ctx)
except (CardError, RateLimitError, APIConnectionError) as e:
err = e.json_body.get('error', {})
billing_logger.error("Stripe error - Status: {}, Type: {}, Code: {}, Param: {}, Message: {}".format(
e.http_status, err.get('type'), err.get('code'), err.get('param'), err.get('message')
))
ctx["error_message"] = err.get('message')
except StripeError as e:
billing_logger.error("Stripe error: %d %s", e.http_status, e.__class__.__name__)
if isinstance(e, CardError):
ctx["error_message"] = e.json_body.get('error', {}).get('message')
else:
ctx["error_message"] = _("Something went wrong. Please try again or email us at %s."
% (settings.ZULIP_ADMINISTRATOR,))
return render(request, 'zilencer/payment.html', context=ctx)
except (InvalidRequestError, AuthenticationError, StripeError) as e:
err = e.json_body.get('error', {})
billing_logger.error("Stripe error - Status: {}, Type: {}, Code: {}, Param: {}, Message: {}".format(
e.http_status, err.get('type'), err.get('code'), err.get('param'), err.get('message')
))
except Exception as e:
billing_logger.error('Stripe error: %s' % (str(e),))
ctx["error_message"] = _("Something went wrong. Please try again or email us at %s."
% (settings.ZULIP_ADMINISTRATOR,))
return render(request, 'zilencer/payment.html', context=ctx)
billing_logger.exception("Uncaught error in billing")
raise