Handle error better

This commit is contained in:
PCoder 2019-04-14 11:17:48 +02:00
parent e7196af1f9
commit c081f9e73a

View file

@ -30,20 +30,29 @@ def handle_invoice_webhook(request):
payload, sig_header, settings.INVOICE_WEBHOOK_SECRET
)
except ValueError as e:
logger.error("Invalid payload details = " + str(e))
# Invalid payload
return HttpResponse(status=400)
err_msg = "FAILURE handle_invoice_webhook: Invalid payload details"
err_body = "Details %s" % str(e)
return handle_error(err_msg, err_body)
except stripe.error.SignatureVerificationError as e:
logger.error("SignatureVerificationError details = " + str(e))
# Invalid signature
return HttpResponse(status=400)
err_msg = "FAILURE handle_invoice_webhook: SignatureVerificationError"
err_body = "Details %s" % str(e)
return handle_error(err_msg, err_body)
# Do something with event
logger.debug("Passed invoice signature verification")
# Get the user from the invoice
invoice = event.data.object
stripe_customer = StripeCustomer.objects.get(stripe_id=invoice.customer)
logger.debug("Checking whether StripeCustomer %s exists" % invoice.customer)
try:
stripe_customer = StripeCustomer.objects.get(stripe_id=invoice.customer)
except StripeCustomer.DoesNotExist as dne:
# StripeCustomer does not exist
err_msg = "FAILURE handle_invoice_webhook: StripeCustomer %s doesn't exist" % invoice.customer
err_body = "Details %s" % str(dne)
return handle_error(err_msg, err_body)
if event.type == "invoice.payment_succeeded":
logger.debug("Invoice payment succeeded")
@ -111,4 +120,16 @@ def handle_invoice_webhook(request):
else:
logger.error("Unhandled event : " + event.type)
return HttpResponse(status=200)
return HttpResponse(status=200)
def handle_error(error_msg, error_body):
logger.error("%s -- %s" % (error_msg, error_body))
email_to_admin_data = {
'subject': error_msg,
'from_email': settings.DCL_SUPPORT_FROM_ADDRESS,
'to': settings.WEBHOOK_EMAIL_TO.split(","),
'body': error_body,
}
send_plain_email_task.delay(email_to_admin_data)
return HttpResponse(status=400)