Skip to content

Commit

Permalink
feat: change logging level from error to info for JSON parsing errors
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Jun 27, 2019
1 parent f56e038 commit cc14406
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
19 changes: 13 additions & 6 deletions lib/pact_broker/api/resources/error_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/logging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 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 @@ -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 }
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit cc14406

Please sign in to comment.