refactor(mailer): shared error concern
This commit is contained in:
parent
812376cfb2
commit
b9c1799e60
4 changed files with 38 additions and 39 deletions
|
@ -1,25 +1,12 @@
|
|||
class ApplicationMailer < ActionMailer::Base
|
||||
include MailerErrorConcern
|
||||
|
||||
helper :application # gives access to all helpers defined within `application_helper`.
|
||||
default from: "#{APPLICATION_NAME} <#{CONTACT_EMAIL}>"
|
||||
layout 'mailer'
|
||||
|
||||
before_action :add_dolist_header
|
||||
|
||||
# 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
|
||||
log_smtp_error(exception)
|
||||
end
|
||||
end
|
||||
|
||||
rescue_from Net::SMTPError, with: :log_smtp_error
|
||||
|
||||
# Attach the procedure logo to the email (if any).
|
||||
# Returns the attachment url.
|
||||
def attach_logo(procedure)
|
||||
|
@ -28,7 +15,6 @@ class ApplicationMailer < ActionMailer::Base
|
|||
attachments.inline[logo_filename] = procedure.logo.download
|
||||
attachments[logo_filename].url
|
||||
end
|
||||
|
||||
rescue StandardError => e
|
||||
# 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 })
|
||||
|
@ -42,14 +28,4 @@ class ApplicationMailer < ActionMailer::Base
|
|||
def add_dolist_header
|
||||
headers['X-Dolist-Message-Name'] = action_name
|
||||
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
|
||||
|
|
33
app/mailers/concerns/mailer_error_concern.rb
Normal file
33
app/mailers/concerns/mailer_error_concern.rb
Normal file
|
@ -0,0 +1,33 @@
|
|||
module MailerErrorConcern
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
# 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
|
||||
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
|
|
@ -3,19 +3,9 @@ class DeviseUserMailer < Devise::Mailer
|
|||
helper :application # gives access to all helpers defined within `application_helper`.
|
||||
helper MailerHelper
|
||||
include Devise::Controllers::UrlHelpers # Optional. eg. `confirmation_url`
|
||||
include MailerErrorConcern
|
||||
layout 'mailers/layout'
|
||||
|
||||
# Don’t 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
|
||||
['devise_mailer']
|
||||
end
|
||||
|
|
|
@ -59,8 +59,8 @@ RSpec.describe ApplicationMailer, type: :mailer do
|
|||
end
|
||||
end
|
||||
|
||||
context "smtp unknown error" do
|
||||
let(:smtp_error) { Net::SMTPUnknownError.new }
|
||||
context "generic unknown error" do
|
||||
let(:smtp_error) { Net::OpenTimeout.new }
|
||||
|
||||
it "creates an event" do
|
||||
expect { send_email }.to change { EmailEvent.count }.by(1)
|
||||
|
|
Loading…
Reference in a new issue