Skip to content

Commit

Permalink
Add debug log statements
Browse files Browse the repository at this point in the history
  • Loading branch information
TheByronHimes committed Sep 6, 2024
1 parent cad9795 commit 77fc371
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/ns/adapters/outbound/smtp_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,13 @@ def get_connection(self) -> Generator[SMTP, None, None]:
)

try:
log.debug("Attempting to establish SMTP connection (timeout=%s).", timeout)
with (
SMTP(self._config.smtp_host, self._config.smtp_port, timeout=timeout)
if timeout
else SMTP(self._config.smtp_host, self._config.smtp_port) as server
):
log.debug("STMP connection successfully established.")
yield server
except OSError as err:
# TimeoutError is a subclass of OSError, but a blocked port can result
Expand All @@ -105,19 +107,23 @@ def send_email_message(self, message: EmailMessage):
username = self._config.smtp_auth.username
password = self._config.smtp_auth.password.get_secret_value()
try:
log.debug("Authenticating against the SMTP server.")
server.login(username, password)
except SMTPAuthenticationError as err:
login_error = self.FailedLoginError()
log.critical(login_error)
raise login_error from err

# check for a connection
log.debug("Performing NOOP to verify SMTP connection.")
if server.noop()[0] != 250:
connection_error = self.ServerPingError()
log.critical(connection_error)
raise connection_error

log.debug("NOOP successful, sending email.")
server.send_message(msg=message)
log.debug("Email sent.")
except SMTPException as exc:
error = self.GeneralSmtpException(error_info=exc.args[0])
log.error(error, extra={"error_info": exc.args[0]})
Expand Down
1 change: 1 addition & 0 deletions src/ns/core/notifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ async def send_notification(self, *, notification: event_schemas.Notification):
await self._register_new_notification(notification_record=notification_record)

message = self._construct_email(notification=notification)
log.info("Sending notification")
self._smtp_client.send_email_message(message)

# update the notification record to show that the notification has been sent.
Expand Down

0 comments on commit 77fc371

Please sign in to comment.