Skip to content

Commit d099c61

Browse files
committed
Handle error parsing confirmation email address
Handle the ConfirmationEmailToAddressError raised by FormSubmissionService in CheckYourAnswersController#submit_answers by re-rendering the check your answers page with a validation error message stating that the email address is invalid.
1 parent 81a7073 commit d099c61

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

app/controllers/forms/check_your_answers_controller.rb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,19 @@ def submit_answers
3636
return render template: "errors/incomplete_submission", locals: { current_form:, current_context: }
3737
end
3838

39-
submission_reference = FormSubmissionService.call(current_context:,
40-
email_confirmation_input:,
41-
mode:).submit
39+
begin
40+
submission_reference = FormSubmissionService.call(current_context:,
41+
email_confirmation_input:,
42+
mode:).submit
4243

43-
current_context.save_submission_details(submission_reference, requested_email_confirmation)
44+
current_context.save_submission_details(submission_reference, requested_email_confirmation)
4445

45-
redirect_to :form_submitted
46+
redirect_to :form_submitted
47+
rescue FormSubmissionService::ConfirmationEmailToAddressError
48+
setup_check_your_answers
49+
email_confirmation_input.errors.add(:confirmation_email_address, :invalid_email)
50+
render template: "forms/check_your_answers/show", locals: { email_confirmation_input: }, status: :unprocessable_content
51+
end
4652
rescue StandardError => e
4753
log_rescued_exception(e)
4854

spec/requests/forms/check_your_answers_controller_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,28 @@
704704

705705
include_examples "for notification references"
706706
end
707+
708+
context "when there is an ActionMailer error with the confirmation email address" do
709+
before do
710+
mock_form_submission_service = instance_double(FormSubmissionService)
711+
allow(FormSubmissionService).to receive(:new).and_return(mock_form_submission_service)
712+
allow(mock_form_submission_service).to receive(:submit).and_raise(FormSubmissionService::ConfirmationEmailToAddressError)
713+
714+
post form_submit_answers_path(2, "form-name", 1, mode:), params: { email_confirmation_input: }
715+
end
716+
717+
it "return 422 error code" do
718+
expect(response).to have_http_status(:unprocessable_content)
719+
end
720+
721+
it "renders the check your answers page" do
722+
expect(response).to render_template("forms/check_your_answers/show")
723+
end
724+
725+
it "has a validation error for the confirmation email address" do
726+
expect(response.body).to include(I18n.t("activemodel.errors.models.email_confirmation_input.attributes.confirmation_email_address.invalid_email"))
727+
end
728+
end
707729
end
708730

709731
private

0 commit comments

Comments
 (0)