Skip to content

Commit

Permalink
Able to pull through email info through header
Browse files Browse the repository at this point in the history
  • Loading branch information
syed87 committed Aug 8, 2024
1 parent 9a127b8 commit 6a0ab77
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
19 changes: 19 additions & 0 deletions app/mailers/application_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,23 @@ class ApplicationMailer < Mail::Notify::Mailer
"GOVUK_NOTIFY_TEMPLATE_ID_APPLICATION",
"95adafaf-0920-4623-bddc-340853c047af",
)

rescue_from Notifications::Client::RequestError do
# WARNING: this needs to be a block, otherwise the exception will not be
# re-raised and we will not be notified via Sentry, and the job will not retry.
#
# @see https://github.com/rails/rails/issues/39018
if respond_to?(:headers)
binding.break
email = Email.find(headers['email-log-id'].to_s)
email.update!(delivery_status: 'notify_error')
end

raise
end

def notify_email(headers)
headers = headers.merge(rails_mailer: mailer_name, rails_mail_template: action_name)
view_mail(GOVUK_NOTIFY_TEMPLATE_ID, headers)
end
end
1 change: 1 addition & 0 deletions app/views/anonymous/test_notify_error.text.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is used by the ApplicationMailer spec.
30 changes: 30 additions & 0 deletions spec/mailers/application_mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'rails_helper'

RSpec.describe ApplicationMailer, :sidekiq do
describe '.rescue_from' do
fake_mailer = Class.new(ApplicationMailer) do
self.delivery_method = :notify
self.notify_settings = {
api_key: 'not-real-e1f4c969-b675-4a0d-a14d-623e7c2d3fd8-24fea27b-824e-4259-b5ce-1badafe98150',
}

def test_notify_error
notify_email(
to: '[email protected]',
subject: 'Some subject',
)
end
end

it 'marks errors as failed and re-raises the error' do
stub_request(:post, 'https://api.notifications.service.gov.uk/v2/notifications/email')
.to_return(status: 404)

expect {
fake_mailer.test_notify_error.deliver_now
}.to raise_error(Notifications::Client::NotFoundError)

expect(Email.last.delivery_status).to eql('notify_error')
end
end
end

0 comments on commit 6a0ab77

Please sign in to comment.