Skip to content

Commit

Permalink
feat: only show backtrace in error response for non production enviro…
Browse files Browse the repository at this point in the history
…nments
  • Loading branch information
bethesque committed Jun 14, 2018
1 parent d976da7 commit 665ac23
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/pact_broker/api/resources/error_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions lib/pact_broker/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions spec/lib/pact_broker/api/resources/error_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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') }

Expand Down
30 changes: 30 additions & 0 deletions spec/lib/pact_broker/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 665ac23

Please sign in to comment.