refactor(mailer): shared error concern

This commit is contained in:
Colin Darie 2023-01-10 00:40:16 +01:00
parent 812376cfb2
commit b9c1799e60
4 changed files with 38 additions and 39 deletions

View file

@ -1,25 +1,12 @@
class ApplicationMailer < ActionMailer::Base class ApplicationMailer < ActionMailer::Base
include MailerErrorConcern
helper :application # gives access to all helpers defined within `application_helper`. helper :application # gives access to all helpers defined within `application_helper`.
default from: "#{APPLICATION_NAME} <#{CONTACT_EMAIL}>" default from: "#{APPLICATION_NAME} <#{CONTACT_EMAIL}>"
layout 'mailer' layout 'mailer'
before_action :add_dolist_header before_action :add_dolist_header
# 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_smtp_error(exception)
end
end
rescue_from Net::SMTPError, with: :log_smtp_error
# Attach the procedure logo to the email (if any). # Attach the procedure logo to the email (if any).
# Returns the attachment url. # Returns the attachment url.
def attach_logo(procedure) def attach_logo(procedure)
@ -28,7 +15,6 @@ class ApplicationMailer < ActionMailer::Base
attachments.inline[logo_filename] = procedure.logo.download attachments.inline[logo_filename] = procedure.logo.download
attachments[logo_filename].url attachments[logo_filename].url
end end
rescue StandardError => e rescue StandardError => e
# A problem occured when reading logo, maybe the logo is missing and we should clean the procedure to remove logo reference ? # A problem occured when reading logo, maybe the logo is missing and we should clean the procedure to remove logo reference ?
Sentry.capture_exception(e, extra: { procedure_id: procedure.id }) Sentry.capture_exception(e, extra: { procedure_id: procedure.id })
@ -42,14 +28,4 @@ class ApplicationMailer < ActionMailer::Base
def add_dolist_header def add_dolist_header
headers['X-Dolist-Message-Name'] = action_name headers['X-Dolist-Message-Name'] = action_name
end end
def log_smtp_error(exception)
if defined?(message) && message.to.present?
EmailEvent.create_from_message!(message, status: "dispatch_error")
end
Sentry.capture_exception(exception, extra: { to: message&.to, subject: message&.subject })
# TODO find a way to re attempt the job
end
end end

View file

@ -0,0 +1,33 @@
module MailerErrorConcern
extend ActiveSupport::Concern
included do
# 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
protected
def log_delivery_error(exception)
if defined?(message) && message.to.present?
EmailEvent.create_from_message!(message, status: "dispatch_error")
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
end
end
end

View file

@ -3,19 +3,9 @@ class DeviseUserMailer < Devise::Mailer
helper :application # gives access to all helpers defined within `application_helper`. helper :application # gives access to all helpers defined within `application_helper`.
helper MailerHelper helper MailerHelper
include Devise::Controllers::UrlHelpers # Optional. eg. `confirmation_url` include Devise::Controllers::UrlHelpers # Optional. eg. `confirmation_url`
include MailerErrorConcern
layout 'mailers/layout' layout 'mailers/layout'
# Dont retry to send a message if the server rejects the recipient address
rescue_from Net::SMTPSyntaxError do |_error|
message.perform_deliveries = false
end
rescue_from Net::SMTPServerBusy do |error|
if /unexpected recipients/.match?(error.message)
message.perform_deliveries = false
end
end
def template_paths def template_paths
['devise_mailer'] ['devise_mailer']
end end

View file

@ -59,8 +59,8 @@ RSpec.describe ApplicationMailer, type: :mailer do
end end
end end
context "smtp unknown error" do context "generic unknown error" do
let(:smtp_error) { Net::SMTPUnknownError.new } let(:smtp_error) { Net::OpenTimeout.new }
it "creates an event" do it "creates an event" do
expect { send_email }.to change { EmailEvent.count }.by(1) expect { send_email }.to change { EmailEvent.count }.by(1)