refactor(email): log event when error occurs before recipients was set

This commit is contained in:
Colin Darie 2023-01-10 13:58:30 +01:00
parent 1f51267b25
commit 5ae5de0d9c
3 changed files with 5 additions and 9 deletions

View file

@ -20,12 +20,8 @@ module MailerErrorConcern
protected protected
def log_delivery_error(exception) def log_delivery_error(exception)
if defined?(message) && message.to.present? EmailEvent.create_from_message!(message, status: "dispatch_error")
EmailEvent.create_from_message!(message, status: "dispatch_error") Sentry.capture_exception(exception, extra: { to: message.to, subject: message.subject })
Sentry.capture_exception(exception, extra: { to: message&.to, subject: message&.subject })
else
Sentry.capture_exception(exception)
end
# TODO find a way to re attempt the job # TODO find a way to re attempt the job
end end

View file

@ -19,7 +19,9 @@ class EmailEvent < ApplicationRecord
class << self class << self
def create_from_message!(message, status:) def create_from_message!(message, status:)
message.to.each do |recipient| to = message.to || ["unset"] # no recipients when error occurs *before* setting to: in the mailer
to.each do |recipient|
EmailEvent.create!( EmailEvent.create!(
to: pseudonymize_email(recipient), to: pseudonymize_email(recipient),
subject: message.subject, subject: message.subject,

View file

@ -1,7 +1,5 @@
class EmailDeliveryObserver class EmailDeliveryObserver
def self.delivered_email(message) def self.delivered_email(message)
return if message.to.nil?
EmailEvent.create_from_message!(message, status: "dispatched") EmailEvent.create_from_message!(message, status: "dispatched")
end end
end end