From 665ac231c4c9bc264bd4f1e40297b15c558dd87b Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Fri, 15 Jun 2018 08:33:01 +1000 Subject: [PATCH] feat: only show backtrace in error response for non production environments --- .../api/resources/error_handler.rb | 6 +++- lib/pact_broker/configuration.rb | 4 +++ .../api/resources/error_handler_spec.rb | 26 ++++++++++++++++ spec/lib/pact_broker/configuration_spec.rb | 30 +++++++++++++++++++ 4 files changed, 65 insertions(+), 1 deletion(-) diff --git a/lib/pact_broker/api/resources/error_handler.rb b/lib/pact_broker/api/resources/error_handler.rb index 82f58188d..3090fde0a 100644 --- a/lib/pact_broker/api/resources/error_handler.rb +++ b/lib/pact_broker/api/resources/error_handler.rb @@ -11,7 +11,11 @@ class ErrorHandler def self.call e, request, response logger.error e logger.error e.backtrace - response.body = {:message => e.message, :backtrace => e.backtrace }.to_json + response_body = { :message => e.message } + if PactBroker.configuration.show_backtrace_in_error_response? + response_body[:backtrace] = e.backtrace + end + response.body = response_body.to_json report(e, request) if reportable?(e) end diff --git a/lib/pact_broker/configuration.rb b/lib/pact_broker/configuration.rb index a90fd68ea..a40de3137 100644 --- a/lib/pact_broker/configuration.rb +++ b/lib/pact_broker/configuration.rb @@ -91,6 +91,10 @@ def self.default_html_pact_render } end + def show_backtrace_in_error_response? + !!(ENV['RACK_ENV'] && ENV['RACK_ENV'].downcase != 'production') + end + def authentication_configured? !!authenticate || !!authenticate_with_basic_auth end diff --git a/spec/lib/pact_broker/api/resources/error_handler_spec.rb b/spec/lib/pact_broker/api/resources/error_handler_spec.rb index 64656fa3f..08ad423c2 100644 --- a/spec/lib/pact_broker/api/resources/error_handler_spec.rb +++ b/spec/lib/pact_broker/api/resources/error_handler_spec.rb @@ -40,6 +40,32 @@ module Resources end end + context "when show_backtrace_in_error_response? is true" do + before do + allow(PactBroker.configuration).to receive(:show_backtrace_in_error_response?).and_return(true) + end + + it "includes the backtrace in the error response" do + expect(response).to receive(:body=) do | body | + expect(body).to include("backtrace") + end + subject + end + end + + context "when show_backtrace_in_error_response? is false" do + before do + allow(PactBroker.configuration).to receive(:show_backtrace_in_error_response?).and_return(false) + end + + it "does not include the backtrace in the error response" do + expect(response).to receive(:body=) do | body | + expect(body).to_not include("backtrace") + end + subject + end + end + context "when the error is a PactBroker::TestError" do let(:error) { PactBroker::TestError.new('test error') } diff --git a/spec/lib/pact_broker/configuration_spec.rb b/spec/lib/pact_broker/configuration_spec.rb index 4473a2842..4736573c3 100644 --- a/spec/lib/pact_broker/configuration_spec.rb +++ b/spec/lib/pact_broker/configuration_spec.rb @@ -6,6 +6,36 @@ module PactBroker describe Configuration do + describe "show_backtrace_in_error_response?" do + before do + allow(ENV).to receive(:[]).and_call_original + end + + context "when RACK_ENV is not set" do + before do + allow(ENV).to receive(:[]).with("RACK_ENV").and_return(nil) + end + + its(:show_backtrace_in_error_response?) { is_expected.to be false } + end + + context "when RACK_ENV is not production" do + before do + allow(ENV).to receive(:[]).with("RACK_ENV").and_return('development') + end + + its(:show_backtrace_in_error_response?) { is_expected.to be true } + end + + context "when RACK_ENV is production" do + before do + allow(ENV).to receive(:[]).with("RACK_ENV").and_return('production') + end + + its(:show_backtrace_in_error_response?) { is_expected.to be false } + end + end + context "default configuration" do describe ".html_pact_renderer" do