[email] Better error handling
This commit is contained in:
parent
1ed95a7352
commit
52a7f04313
|
@ -7,6 +7,11 @@ from kibicara.platformapi import Censor, Spawner
|
||||||
from kibicara.email import send_email
|
from kibicara.email import send_email
|
||||||
from kibicara.config import config
|
from kibicara.config import config
|
||||||
from kibicara.webapi.admin import to_token
|
from kibicara.webapi.admin import to_token
|
||||||
|
from smtplib import SMTPException
|
||||||
|
from logging import getLogger
|
||||||
|
|
||||||
|
|
||||||
|
logger = getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class EmailBot(Censor):
|
class EmailBot(Censor):
|
||||||
|
@ -36,7 +41,16 @@ class EmailBot(Censor):
|
||||||
"\n\n--\nIf you want to stop receiving these mails, "
|
"\n\n--\nIf you want to stop receiving these mails, "
|
||||||
"follow this link: " + unsubscribe_link
|
"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)
|
spawner = Spawner(Email, EmailBot)
|
||||||
|
|
|
@ -14,6 +14,7 @@ from ormantic.exceptions import NoMatch
|
||||||
from sqlite3 import IntegrityError
|
from sqlite3 import IntegrityError
|
||||||
from kibicara.webapi.admin import from_token, to_token
|
from kibicara.webapi.admin import from_token, to_token
|
||||||
from os import urandom
|
from os import urandom
|
||||||
|
from smtplib import SMTPException
|
||||||
from logging import getLogger
|
from logging import getLogger
|
||||||
|
|
||||||
|
|
||||||
|
@ -38,7 +39,7 @@ async def get_email(hood=Depends(get_hood)):
|
||||||
try:
|
try:
|
||||||
return await Email.objects.get(hood=hood)
|
return await Email.objects.get(hood=hood)
|
||||||
except NoMatch:
|
except NoMatch:
|
||||||
return HTTPException(status.HTTP_404_NOT_FOUND)
|
return HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||||
|
|
||||||
|
|
||||||
router = APIRouter()
|
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
|
config['root_url'] + "api/" + str(hood.id) + "/email/subscribe/confirm/" + token
|
||||||
)
|
)
|
||||||
logger.debug("Subscription confirmation link: " + confirm_link)
|
logger.debug("Subscription confirmation link: " + confirm_link)
|
||||||
send_email(
|
try:
|
||||||
subscriber.email,
|
send_email(
|
||||||
"Subscribe to Kibicara " + hood.name,
|
subscriber.email,
|
||||||
sender=hood.name,
|
"Subscribe to Kibicara " + hood.name,
|
||||||
body="To confirm your subscription, follow this link: " + confirm_link,
|
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
|
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 token.hood and url.hood are different, raise an error:
|
||||||
if hood.id is not payload['hood']:
|
if hood.id is not payload['hood']:
|
||||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
|
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/')
|
@router.post('/messages/')
|
||||||
|
|
Loading…
Reference in a new issue