From 52a7f043138038b2ec48e6ef363856c2acd7cf29 Mon Sep 17 00:00:00 2001 From: maike Date: Tue, 7 Jul 2020 15:28:51 +0200 Subject: [PATCH] [email] Better error handling --- kibicara/platforms/email/bot.py | 16 +++++++++++++++- kibicara/platforms/email/webapi.py | 23 +++++++++++++++-------- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/kibicara/platforms/email/bot.py b/kibicara/platforms/email/bot.py index a24a9a4..43c3e12 100644 --- a/kibicara/platforms/email/bot.py +++ b/kibicara/platforms/email/bot.py @@ -7,6 +7,11 @@ from kibicara.platformapi import Censor, Spawner from kibicara.email import send_email from kibicara.config import config from kibicara.webapi.admin import to_token +from smtplib import SMTPException +from logging import getLogger + + +logger = getLogger(__name__) class EmailBot(Censor): @@ -36,7 +41,16 @@ class EmailBot(Censor): "\n\n--\nIf you want to stop receiving these mails, " "follow this link: " + unsubscribe_link ) - send_email(subscriber.email, "Kibicara " + self.hood.name, body=message.text) + try: + send_email( + subscriber.email, + "Kibicara " + self.hood.name, + body=message.text, + ) + except (ConnectionRefusedError, SMTPException): + logger.error( + "Sending subscription confirmation email failed.", exc_info=True + ) spawner = Spawner(Email, EmailBot) diff --git a/kibicara/platforms/email/webapi.py b/kibicara/platforms/email/webapi.py index 81fad27..35f4ce7 100644 --- a/kibicara/platforms/email/webapi.py +++ b/kibicara/platforms/email/webapi.py @@ -14,6 +14,7 @@ from ormantic.exceptions import NoMatch from sqlite3 import IntegrityError from kibicara.webapi.admin import from_token, to_token from os import urandom +from smtplib import SMTPException from logging import getLogger @@ -38,7 +39,7 @@ async def get_email(hood=Depends(get_hood)): try: return await Email.objects.get(hood=hood) except NoMatch: - return HTTPException(status.HTTP_404_NOT_FOUND) + return HTTPException(status_code=status.HTTP_404_NOT_FOUND) router = APIRouter() @@ -85,12 +86,16 @@ async def email_subscribe(subscriber: Subscriber, hood=Depends(get_hood)): config['root_url'] + "api/" + str(hood.id) + "/email/subscribe/confirm/" + token ) logger.debug("Subscription confirmation link: " + confirm_link) - send_email( - subscriber.email, - "Subscribe to Kibicara " + hood.name, - sender=hood.name, - body="To confirm your subscription, follow this link: " + confirm_link, - ) + try: + send_email( + subscriber.email, + "Subscribe to Kibicara " + hood.name, + sender=hood.name, + body="To confirm your subscription, follow this link: " + confirm_link, + ) + except (ConnectionRefusedError, SMTPException): + logger.error("Sending subscription confirmation email failed.", exc_info=True) + raise HTTPException(status_code=status.HTTP_502_BAD_GATEWAY) return status.HTTP_200_OK @@ -122,7 +127,9 @@ async def email_unsubscribe(token, hood=Depends(get_hood)): # If token.hood and url.hood are different, raise an error: if hood.id is not payload['hood']: raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST) - await EmailSubscribers.objects.delete_many(hood=payload['hood'], email=payload['email']) + await EmailSubscribers.objects.delete_many( + hood=payload['hood'], email=payload['email'] + ) @router.post('/messages/')