demarches-normaliennes/app/mailers/concerns/mailer_monitoring_concern.rb

40 lines
1.1 KiB
Ruby
Raw Normal View History

module MailerMonitoringConcern
2023-01-10 00:40:16 +01:00
extend ActiveSupport::Concern
included do
before_action :add_dolist_header
2023-01-10 00:40:16 +01:00
# Dont retry to send a message if the server rejects the recipient address
rescue_from Net::SMTPSyntaxError do |_exception|
message.perform_deliveries = false
end
rescue_from Net::SMTPServerBusy do |exception|
if /unexpected recipients/.match?(exception.message)
message.perform_deliveries = false
else
log_delivery_error(exception)
end
end
rescue_from StandardError, with: :log_delivery_error
# mandatory for dolist
# used for tracking in Dolist UI
# the delivery_method is yet unknown (:balancer)
# so we add the dolist header for everyone
def add_dolist_header
headers['X-Dolist-Message-Name'] = action_name
end
2023-01-10 00:40:16 +01:00
protected
def log_delivery_error(exception)
EmailEvent.create_from_message!(message, status: "dispatch_error")
Sentry.capture_exception(exception, extra: { to: message.to, subject: message.subject })
2023-01-10 00:40:16 +01:00
# TODO find a way to re attempt the job
end
end
end