Skip to content

Commit

Permalink
fix: fix bug in error handling for 'can-i-deploy branch to environmen…
Browse files Browse the repository at this point in the history
…t' badge

PACT-1145
  • Loading branch information
bethesque committed Jul 3, 2023
1 parent 2dead44 commit c23beb6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/pact_broker/api/resources/badge_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 8 additions & 3 deletions lib/pact_broker/api/resources/error_handling_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,21 @@ 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
application_context.error_logger.call(error, error_reference, request.env)
# 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c23beb6

Please sign in to comment.