2023-01-10 15:36:58 +01:00
|
|
|
|
module MailerMonitoringConcern
|
2023-01-10 00:40:16 +01:00
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
|
|
included do
|
2023-01-16 11:26:05 +01:00
|
|
|
|
# Intercept & log any error, then re-raise so job will retry.
|
|
|
|
|
# NOTE: rescue_from order matters, later matchers are tried first.
|
|
|
|
|
rescue_from StandardError, with: :log_and_raise_delivery_error
|
|
|
|
|
|
2023-01-10 00:40:16 +01:00
|
|
|
|
# Don’t 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
|
2023-01-16 11:26:05 +01:00
|
|
|
|
log_and_raise_delivery_error(exception)
|
2023-01-10 00:40:16 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2023-01-16 11:26:05 +01:00
|
|
|
|
def log_and_raise_delivery_error(exception)
|
2023-01-10 13:58:30 +01:00
|
|
|
|
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
|
|
|
|
|
2023-01-16 11:26:05 +01:00
|
|
|
|
# re-raise another error so job will retry later
|
|
|
|
|
raise MailDeliveryError.new(exception)
|
2023-01-10 00:40:16 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|