From 9e29b2873886f492072f70e8c435dc79ae35b46f Mon Sep 17 00:00:00 2001 From: TheByronHimes Date: Wed, 17 Jan 2024 09:31:11 +0000 Subject: [PATCH] Replace grouped error logging with individual log statements --- src/ns/adapters/outbound/smtp_client.py | 16 ++++++++-- src/ns/core/notifier.py | 40 ++++++++++++++----------- 2 files changed, 35 insertions(+), 21 deletions(-) diff --git a/src/ns/adapters/outbound/smtp_client.py b/src/ns/adapters/outbound/smtp_client.py index 4b4f452..42b969e 100644 --- a/src/ns/adapters/outbound/smtp_client.py +++ b/src/ns/adapters/outbound/smtp_client.py @@ -15,6 +15,7 @@ # """Contains the smtp client adapter""" +import logging import smtplib import ssl from email.message import EmailMessage @@ -24,6 +25,8 @@ from ns.ports.outbound.smtp_client import SmtpClientPort +log = logging.getLogger(__name__) + class SmtpClientConfig(BaseSettings): """Configuration details for the SmtpClient""" @@ -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 diff --git a/src/ns/core/notifier.py b/src/ns/core/notifier.py index 372787b..ea9a7d3 100644 --- a/src/ns/core/notifier.py +++ b/src/ns/core/notifier.py @@ -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 @@ -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) @@ -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")