[email] Better error handling

This commit is contained in:
maike 2020-07-07 15:28:51 +02:00 committed by dl6tom
parent 1ed95a7352
commit 52a7f04313
2 changed files with 30 additions and 9 deletions

View file

@ -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)

View file

@ -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)
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/')