Skip to content

Commit

Permalink
Handle delivery error in CBV Flow Invitations Controller
Browse files Browse the repository at this point in the history
When there is a delivery error, let's catch it and give the user a
better error message.

In order to test this in development, you'll need to set
`config.action_mailer.raise_delivery_errors` to true. (It's true by
default in production.)

Finishes FFS-839.
  • Loading branch information
tdooner committed Jun 14, 2024
1 parent a53361a commit 98a3859
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
17 changes: 13 additions & 4 deletions app/app/controllers/cbv_flow_invitations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,19 @@ def new
end

def create
CbvInvitationService.new.invite(
cbv_flow_invitation_params[:email_address],
cbv_flow_invitation_params[:case_number]
)
begin
CbvInvitationService.new.invite(
cbv_flow_invitation_params[:email_address],
cbv_flow_invitation_params[:case_number]
)
rescue => ex
flash[:alert] = t(".invite_failed",
email_address: cbv_flow_invitation_params[:email_address],
error_message: ex.message
)
Rails.logger.error("Error sending CBV invitation: #{ex.class} - #{ex.message}")
return redirect_to new_cbv_flow_invitation_path(secret: params[:secret])
end

flash[:notice] = t(".invite_success", email_address: cbv_flow_invitation_params[:email_address])
redirect_to root_url
Expand Down
1 change: 1 addition & 0 deletions app/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ en:
thank_you: Thank you!
cbv_flow_invitations:
create:
invite_failed: 'Error sending invitation to %{email_address}: %{error_message}.'
invite_success: Successfully delivered invitation to %{email_address}.
incorrect_invite_secret: Unable to send invitation due to missing invitation secret parameter.
new:
Expand Down
26 changes: 26 additions & 0 deletions app/spec/controllers/cbv_flow_invitations_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,32 @@

expect(response).to redirect_to(root_url)
end

context "when the CbvInvitationService has an error" do
let(:broken_params) do
valid_params.tap do |params|
params[:cbv_flow_invitation][:email_address] = "bad-email@"
end
end

before do
allow_any_instance_of(CbvInvitationService)
.to receive(:invite)
.with("bad-email@", "ABC1234")
.and_raise(StandardError.new("Some random error, like a bad email address or something."))
end

it "redirects back to the invitation form with the error" do
expect_any_instance_of(CbvInvitationService)
.to receive(:invite)
.with("bad-email@", "ABC1234")

post :create, params: broken_params

expect(response).to redirect_to(new_cbv_flow_invitation_path(secret: broken_params[:secret]))
expect(controller.flash.alert).to include("Some random error")
end
end
end
end
end

0 comments on commit 98a3859

Please sign in to comment.