Skip to content

Commit

Permalink
feat: allow error causes to be configured to log at warning level
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Dec 14, 2020
1 parent 43091b5 commit 3a7bf5e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/pact_broker/api/resources/error_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def self.reportable?(e)
end

def self.log_as_warning?(e)
PactBroker.configuration.warning_error_classes.any? { |clazz| e.is_a?(clazz) }
PactBroker.configuration.warning_error_classes.any? { |clazz| e.is_a?(clazz) || e.cause&.is_a?(clazz) }
end

def self.display_message(e, obfuscated_message)
Expand Down
11 changes: 8 additions & 3 deletions lib/pact_broker/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def self.default_configuration
require 'pact_broker/api/resources/default_base_resource'
PactBroker::Api::Resources::DefaultBaseResource
}
config.warning_error_class_names = ['Sequel::ForeignKeyConstraintViolation']
config.warning_error_class_names = ['Sequel::ForeignKeyConstraintViolation', 'PG::QueryCanceled']
config.metrics_sql_statement_timeout = 30
config
end
Expand Down Expand Up @@ -250,8 +250,13 @@ def webhook_host_whitelist= webhook_host_whitelist

def warning_error_classes
warning_error_class_names.collect do | class_name |
Object.const_get(class_name)
end
begin
Object.const_get(class_name)
rescue NameError => e
logger.warn("Class #{class_name} couldn't be loaded as a warning error class (#{e.class} - #{e.message}). Ignoring.")
nil
end
end.compact
end

private
Expand Down
16 changes: 16 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 @@ -53,6 +53,22 @@ module Resources
end
end

context "when the error cause class is in the warning_error_classes list" do
class TestCauseError < StandardError; end

before do
allow(PactBroker.configuration).to receive(:warning_error_classes).and_return([TestCauseError])
allow(error).to receive(:cause).and_return(TestCauseError.new)
end

let(:error) { StandardError.new("message") }

it "logs at warn so as not to wake everyone up in the middle of the night" do
expect(logger).to receive(:warn).with(/bYWfnyWPlf/, error)
subject
end
end

context "when the error is not reportable and not a warning level" do
let(:error) { PactBroker::Error.new('foo') }

Expand Down

0 comments on commit 3a7bf5e

Please sign in to comment.