Skip to content

Commit

Permalink
fix: do not invoke error reporters for validation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Apr 5, 2018
1 parent 66ad33a commit 57eed65
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 5 deletions.
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 @@ -12,7 +12,11 @@ def self.call e, request, response
logger.error e
logger.error e.backtrace
response.body = {:message => e.message, :backtrace => e.backtrace }.to_json
report e, request
report(e, request) if reportable?(e)
end

def self.reportable? e
!e.is_a?(PactBroker::Error)
end

def self.report e, request
Expand Down
4 changes: 2 additions & 2 deletions lib/pact_broker/api/resources/error_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ def allowed_methods
end

def to_json
raise PactBroker::Error.new("Don't panic. This is a test API error.")
raise PactBroker::TestError.new("Don't panic. This is a test API error.")
end

def from_json
raise PactBroker::Error.new("Don't panic. This is a test API error.")
raise PactBroker::TestError.new("Don't panic. This is a test API error.")
end
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/pact_broker/error.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module PactBroker

class Error < StandardError; end
class TestError < StandardError; end

end
2 changes: 1 addition & 1 deletion lib/pact_broker/ui/controllers/error_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ErrorTest < Base
include PactBroker::Services

get "/" do
raise PactBroker::Error.new("Don't panic. This is a test UI error.")
raise PactBroker::TestError.new("Don't panic. This is a test UI error.")
end
end
end
Expand Down
20 changes: 19 additions & 1 deletion spec/lib/pact_broker/api/resources/error_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Api
module Resources
describe ErrorHandler do
describe "call" do
let(:error) { PactBroker::Error.new('test error') }
let(:error) { StandardError.new('test error') }
let(:thing) { double('thing', call: nil, another_call: nil) }
let(:options) { { env: env } }
let(:request) { double('request' ) }
Expand All @@ -31,6 +31,24 @@ module Resources
subject
end

context "when the error is a PactBroker::Error or subclass" do
let(:error) { Class.new(PactBroker::Error).new('test error') }

it "does not invoke the api error reporters" do
expect(thing).to_not receive(:call).with(error, options)
subject
end
end

context "when the error is a PactBroker::TestError" do
let(:error) { PactBroker::TestError.new('test error') }

it "invokes the api error reporters" do
expect(thing).to receive(:call).with(error, options)
subject
end
end

context "when the error reporter raises an error itself" do
class TestError < StandardError; end

Expand Down

0 comments on commit 57eed65

Please sign in to comment.