From c23beb6b337a627d0d5d3cda0ab18f01b8b166c9 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Mon, 3 Jul 2023 10:24:38 +1000 Subject: [PATCH] fix: fix bug in error handling for 'can-i-deploy branch to environment' badge PACT-1145 --- lib/pact_broker/api/resources/badge_methods.rb | 3 ++- .../api/resources/error_handling_methods.rb | 11 ++++++++--- ...rsion_by_branch_to_environment_badge_spec.rb | 17 +++++++++++++++++ 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/lib/pact_broker/api/resources/badge_methods.rb b/lib/pact_broker/api/resources/badge_methods.rb index d57e89436..badd81654 100644 --- a/lib/pact_broker/api/resources/badge_methods.rb +++ b/lib/pact_broker/api/resources/badge_methods.rb @@ -37,7 +37,8 @@ def moved_temporarily? badge_url rescue StandardError => e # Want to render a badge, even if there's an error - badge_service.error_badge_url("error", ErrorResponseGenerator.display_message(e, "reference: #{PactBroker::Errors.generate_error_reference}")) + error_reference = log_and_report_error(e) + badge_service.error_badge_url("error", "reference: #{error_reference}") end end diff --git a/lib/pact_broker/api/resources/error_handling_methods.rb b/lib/pact_broker/api/resources/error_handling_methods.rb index 880993fdd..ea15b41d2 100644 --- a/lib/pact_broker/api/resources/error_handling_methods.rb +++ b/lib/pact_broker/api/resources/error_handling_methods.rb @@ -8,6 +8,13 @@ module ErrorHandlingMethods # @override def handle_exception(error) + error_reference = log_and_report_error(error) + headers, body = application_context.error_response_generator.call(error, error_reference, request.env) + headers.each { | key, value | response.headers[key] = value } + response.body = body + end + + def log_and_report_error(error) # generate reference error_reference = PactBroker::Errors.generate_error_reference # log error @@ -15,9 +22,7 @@ def handle_exception(error) # report error application_context.error_reporter.call(error, error_reference, request.env) # generate response - headers, body = application_context.error_response_generator.call(error, error_reference, request.env) - headers.each { | key, value | response.headers[key] = value } - response.body = body + error_reference end def set_json_error_message detail, title: "Server error", type: "server_error", status: 500 diff --git a/spec/lib/pact_broker/api/resources/can_i_deploy_pacticipant_version_by_branch_to_environment_badge_spec.rb b/spec/lib/pact_broker/api/resources/can_i_deploy_pacticipant_version_by_branch_to_environment_badge_spec.rb index c77c78b9b..43294ea32 100644 --- a/spec/lib/pact_broker/api/resources/can_i_deploy_pacticipant_version_by_branch_to_environment_badge_spec.rb +++ b/spec/lib/pact_broker/api/resources/can_i_deploy_pacticipant_version_by_branch_to_environment_badge_spec.rb @@ -74,6 +74,23 @@ module Resources expect(subject.headers["Location"]).to eq "http://error_badge_url" end end + + context "when there is an error creating the badge URL" do + before do + allow(badge_service). to receive(:can_i_deploy_badge_url).and_raise(StandardError.new("some error")) + allow_any_instance_of(CanIDeployPacticipantVersionByBranchToEnvironmentBadge).to receive(:log_and_report_error).and_return("error_ref") + end + + it "logs and reports the error" do + expect_any_instance_of(CanIDeployPacticipantVersionByBranchToEnvironmentBadge).to receive(:log_and_report_error) + subject + end + + it "returns an error badge URL" do + expect(badge_service).to receive(:error_badge_url).with("error", "reference: error_ref") + expect(subject.headers["Location"]).to eq "http://error_badge_url" + end + end end end end