From c081f9e73a8ede6655e8dc0b4a8ea47e069658ae Mon Sep 17 00:00:00 2001 From: PCoder Date: Sun, 14 Apr 2019 11:17:48 +0200 Subject: [PATCH] Handle error better --- webhook/views.py | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/webhook/views.py b/webhook/views.py index 84e53724..8b8a65b0 100644 --- a/webhook/views.py +++ b/webhook/views.py @@ -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) \ No newline at end of file + 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) \ No newline at end of file