From 981e82465223c37bdb65eb0fb83180e44d36c315 Mon Sep 17 00:00:00 2001 From: Jack <113976590+bibblobcode@users.noreply.github.com> Date: Fri, 11 Aug 2023 09:16:19 +0100 Subject: [PATCH] Rescue Notify badrequest errors (#1828) --- app/mailers/devise_notify_mailer.rb | 10 +++++++--- spec/mailers/devise_notify_mailer_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/app/mailers/devise_notify_mailer.rb b/app/mailers/devise_notify_mailer.rb index 533bd76b6..f4bde0fd9 100644 --- a/app/mailers/devise_notify_mailer.rb +++ b/app/mailers/devise_notify_mailer.rb @@ -5,14 +5,18 @@ class DeviseNotifyMailer < Devise::Mailer @notify_client ||= ::Notifications::Client.new(ENV["GOVUK_NOTIFY_API_KEY"]) end - def send_email(email, template_id, personalisation) - return true if intercept_send?(email) + def send_email(email_address, template_id, personalisation) + return true if intercept_send?(email_address) notify_client.send_email( - email_address: email, + email_address:, template_id:, personalisation:, ) + rescue Notifications::Client::BadRequestError => e + Sentry.capture_exception(e) + + true end def personalisation(record, token, url, username: false) diff --git a/spec/mailers/devise_notify_mailer_spec.rb b/spec/mailers/devise_notify_mailer_spec.rb index a0fe9171f..4ed209b24 100644 --- a/spec/mailers/devise_notify_mailer_spec.rb +++ b/spec/mailers/devise_notify_mailer_spec.rb @@ -8,6 +8,11 @@ RSpec.describe DeviseNotifyMailer do let(:name) { "test" } let(:password) { "password" } let(:role) { "data_coordinator" } + let(:bad_request_error) do + # rubocop:disable RSpec/VerifiedDoubles + Notifications::Client::BadRequestError.new(double(code: 200, body: "")) + # rubocop:enable RSpec/VerifiedDoubles + end before do allow(described_class).to receive(:new).and_return(devise_notify_mailer) @@ -39,6 +44,21 @@ RSpec.describe DeviseNotifyMailer do User.create!(name:, organisation:, email:, password:, role:) end end + + context "when notify mailer raises BadRequestError" do + before do + allow(notify_client).to receive(:send_email).and_raise(bad_request_error) + end + + let(:domain) { Rails.application.credentials[:email_allowlist].first } + let(:email) { "test@#{domain}" } + + it "does not raise an error" do + expect { + User.create!(name:, organisation:, email:, password:, role:) + }.not_to raise_error + end + end end context "when the rails environment is not staging" do