Skip to content

Commit

Permalink
Replace grouped error logging with individual log statements
Browse files Browse the repository at this point in the history
  • Loading branch information
TheByronHimes committed Jan 17, 2024
1 parent 98a20c9 commit 9e29b28
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
16 changes: 13 additions & 3 deletions src/ns/adapters/outbound/smtp_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#

"""Contains the smtp client adapter"""
import logging
import smtplib
import ssl
from email.message import EmailMessage
Expand All @@ -24,6 +25,8 @@

from ns.ports.outbound.smtp_client import SmtpClientPort

log = logging.getLogger(__name__)


class SmtpClientConfig(BaseSettings):
"""Configuration details for the SmtpClient"""
Expand Down Expand Up @@ -62,11 +65,18 @@ def send_email_message(self, message: EmailMessage):
self._config.login_password.get_secret_value(),
)
except smtplib.SMTPAuthenticationError as err:
raise self.FailedLoginError() from err
login_error = self.FailedLoginError()
log.critical(login_error)
raise login_error from err

# check for a connection
if server.noop()[0] != 250:
raise self.ConnectionError()
connection_error = self.ConnectionError()
log.critical(connection_error)
raise connection_error

server.send_message(msg=message)
except smtplib.SMTPException as exc:
raise self.GeneralSmtpException(error_info=exc.args[0]) from exc
error = self.GeneralSmtpException(error_info=exc.args[0])
log.error(error, extra={"error_info": exc.args[0]})
raise error from exc
40 changes: 22 additions & 18 deletions src/ns/core/notifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,8 @@ def __init__(self, *, config: NotifierConfig, smtp_client: SmtpClientPort):
async def send_notification(self, *, notification: event_schemas.Notification):
"""Sends notifications based on the channel info provided (e.g. email addresses)"""
if len(notification.recipient_email) > 0:
try:
message = self._construct_email(notification=notification)
self._smtp_client.send_email_message(message)
except (
self.BadTemplateFormat,
self.VariableNotSuppliedError,
SmtpClientPort.GeneralSmtpException,
SmtpClientPort.FailedLoginError,
SmtpClientPort.ConnectionError,
) as err:
log.fatal(msg=str(err))
raise
message = self._construct_email(notification=notification)
self._smtp_client.send_email_message(message)

def _construct_email(
self, *, notification: event_schemas.Notification
Expand All @@ -82,11 +72,18 @@ def _construct_email(
try:
plaintext_email = plaintext_template.substitute(payload_as_dict)
except KeyError as err:
raise self.VariableNotSuppliedError(variable=err.args[0]) from err
template_var_error = self.VariableNotSuppliedError(variable=err.args[0])
log.critical(template_var_error, extra={"variable": err.args[0]})
raise template_var_error from err
except ValueError as err:
raise self.BadTemplateFormat(
template_format_error = self.BadTemplateFormat(
template_type="plaintext", problem=err.args[0]
) from err
)
log.critical(
template_format_error,
extra={"template_type": "plaintext", "problem": err.args[0]},
)
raise template_format_error from err

message.set_content(plaintext_email)

Expand All @@ -96,11 +93,18 @@ def _construct_email(
try:
html_email = html_template.substitute(payload_as_dict)
except KeyError as err:
raise self.VariableNotSuppliedError(variable=err.args[0]) from err
template_var_error = self.VariableNotSuppliedError(variable=err.args[0])
log.critical(template_var_error, extra={"variable": err.args[0]})
raise template_var_error from err
except ValueError as err:
raise self.BadTemplateFormat(
template_format_error = self.BadTemplateFormat(
template_type="html", problem=err.args[0]
) from err
)
log.critical(
template_format_error,
extra={"template_type": "html", "problem": err.args[0]},
)
raise template_format_error from err

# add the html version to the EmailMessage object
message.add_alternative(html_email, subtype="html")
Expand Down

0 comments on commit 9e29b28

Please sign in to comment.