src\stacosys\service\mail.py broad-exception-caught

Catching Exception might hide unexpected exceptions, like those that might be raised due to future modification.
Therefore, it is recommended to narrow the exceptions.

The method send of the class Mailer catches Exception in line 57.
MIMEText does not raise exceptions (if not using attachments).
See
https://docs.python.org/3/library/email.mime.html

Most code is handled in an inner exception handling.
In order to catch exception from SMTP_SSL I used SMTPException
See
https://docs.python.org/3/library/smtplib.html
pull/22/head
evidencebp 1 week ago
parent dc776881e4
commit 25ed2f06e0

@ -3,7 +3,7 @@
import logging import logging
from email.mime.text import MIMEText from email.mime.text import MIMEText
from smtplib import SMTP_SSL, SMTPAuthenticationError from smtplib import SMTP_SSL, SMTPAuthenticationError, SMTPException
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -54,6 +54,6 @@ class Mailer:
server.send_message(msg) server.send_message(msg)
return True return True
except Exception: except SMTPException:
logger.error("Error sending email", exc_info=True) logger.error("Error sending email", exc_info=True)
return False return False

Loading…
Cancel
Save