Skip to content

Commit 665ac23

Browse files
committed
feat: only show backtrace in error response for non production environments
1 parent d976da7 commit 665ac23

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

lib/pact_broker/api/resources/error_handler.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ class ErrorHandler
1111
def self.call e, request, response
1212
logger.error e
1313
logger.error e.backtrace
14-
response.body = {:message => e.message, :backtrace => e.backtrace }.to_json
14+
response_body = { :message => e.message }
15+
if PactBroker.configuration.show_backtrace_in_error_response?
16+
response_body[:backtrace] = e.backtrace
17+
end
18+
response.body = response_body.to_json
1519
report(e, request) if reportable?(e)
1620
end
1721

lib/pact_broker/configuration.rb

+4
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ def self.default_html_pact_render
9191
}
9292
end
9393

94+
def show_backtrace_in_error_response?
95+
!!(ENV['RACK_ENV'] && ENV['RACK_ENV'].downcase != 'production')
96+
end
97+
9498
def authentication_configured?
9599
!!authenticate || !!authenticate_with_basic_auth
96100
end

spec/lib/pact_broker/api/resources/error_handler_spec.rb

+26
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,32 @@ module Resources
4040
end
4141
end
4242

43+
context "when show_backtrace_in_error_response? is true" do
44+
before do
45+
allow(PactBroker.configuration).to receive(:show_backtrace_in_error_response?).and_return(true)
46+
end
47+
48+
it "includes the backtrace in the error response" do
49+
expect(response).to receive(:body=) do | body |
50+
expect(body).to include("backtrace")
51+
end
52+
subject
53+
end
54+
end
55+
56+
context "when show_backtrace_in_error_response? is false" do
57+
before do
58+
allow(PactBroker.configuration).to receive(:show_backtrace_in_error_response?).and_return(false)
59+
end
60+
61+
it "does not include the backtrace in the error response" do
62+
expect(response).to receive(:body=) do | body |
63+
expect(body).to_not include("backtrace")
64+
end
65+
subject
66+
end
67+
end
68+
4369
context "when the error is a PactBroker::TestError" do
4470
let(:error) { PactBroker::TestError.new('test error') }
4571

spec/lib/pact_broker/configuration_spec.rb

+30
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,36 @@
66
module PactBroker
77
describe Configuration do
88

9+
describe "show_backtrace_in_error_response?" do
10+
before do
11+
allow(ENV).to receive(:[]).and_call_original
12+
end
13+
14+
context "when RACK_ENV is not set" do
15+
before do
16+
allow(ENV).to receive(:[]).with("RACK_ENV").and_return(nil)
17+
end
18+
19+
its(:show_backtrace_in_error_response?) { is_expected.to be false }
20+
end
21+
22+
context "when RACK_ENV is not production" do
23+
before do
24+
allow(ENV).to receive(:[]).with("RACK_ENV").and_return('development')
25+
end
26+
27+
its(:show_backtrace_in_error_response?) { is_expected.to be true }
28+
end
29+
30+
context "when RACK_ENV is production" do
31+
before do
32+
allow(ENV).to receive(:[]).with("RACK_ENV").and_return('production')
33+
end
34+
35+
its(:show_backtrace_in_error_response?) { is_expected.to be false }
36+
end
37+
end
38+
939
context "default configuration" do
1040
describe ".html_pact_renderer" do
1141

0 commit comments

Comments
 (0)