diff --git a/lib/pact_broker/api/resources/error_handler.rb b/lib/pact_broker/api/resources/error_handler.rb index a61547af6..d745dd22a 100644 --- a/lib/pact_broker/api/resources/error_handler.rb +++ b/lib/pact_broker/api/resources/error_handler.rb @@ -9,15 +9,22 @@ class ErrorHandler include PactBroker::Logging def self.call e, request, response - error_reference = SecureRandom.urlsafe_base64.gsub(/[^a-z]/i, '')[0,10] - logger.error "#{e.message} - error reference #{error_reference}" - logger.error e.backtrace + error_reference = generate_error_reference + if reportable?(e) + log_error(e, "Error reference #{error_reference}") + report(e, error_reference, request) + else + logger.info "Error reference #{error_reference} - #{e.class} #{e.message}\n#{e.backtrace.join("\n")}" + end response.body = response_body_hash(e, error_reference).to_json - report(e, error_reference, request) if reportable?(e) end - def self.reportable? e - !e.is_a?(PactBroker::Error) + def self.generate_error_reference + SecureRandom.urlsafe_base64.gsub(/[^a-z]/i, '')[0,10] + end + + def self.reportable?(e) + !e.is_a?(PactBroker::Error) && !e.is_a?(JSON::GeneratorError) end def self.display_message(e, error_reference) diff --git a/lib/pact_broker/logging.rb b/lib/pact_broker/logging.rb index 2df4c7543..80f5e500c 100644 --- a/lib/pact_broker/logging.rb +++ b/lib/pact_broker/logging.rb @@ -32,7 +32,7 @@ def logger=(logger) end def log_error e, description = nil - message = "#{e.class} #{e.message} #{e.backtrace.join("\n")}" + message = "#{e.class} #{e.message}\n#{e.backtrace.join("\n")}" message = "#{description} - #{message}" if description logger.error message 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 07c676e6f..aa83f5d7f 100644 --- a/spec/lib/pact_broker/api/resources/error_handler_spec.rb +++ b/spec/lib/pact_broker/api/resources/error_handler_spec.rb @@ -9,6 +9,7 @@ module Resources before do allow(ErrorHandler).to receive(:logger).and_return(logger) allow(SecureRandom).to receive(:urlsafe_base64).and_return("bYWfn-+yWPlf") + allow(error).to receive(:backtrace).and_return(["backtrace"]) end let(:logger) { double('logger').as_null_object } @@ -71,6 +72,7 @@ module Resources subject end end + context "when the error is not a PactBroker::Error or subclass" do it "invokes the api error reporters" do expect(thing).to receive(:call).with(error, options) @@ -120,6 +122,7 @@ module Resources subject end end + context "when the error is not a PactBroker::Error or subclass" do it "invokes the api error reporters" do expect(thing).to receive(:call).with(error, options)