From 3a7bf5eaf61649d11305388fb132c18321893637 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Tue, 15 Dec 2020 10:59:26 +1100 Subject: [PATCH] feat: allow error causes to be configured to log at warning level --- lib/pact_broker/api/resources/error_handler.rb | 2 +- lib/pact_broker/configuration.rb | 11 ++++++++--- .../api/resources/error_handler_spec.rb | 16 ++++++++++++++++ 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/lib/pact_broker/api/resources/error_handler.rb b/lib/pact_broker/api/resources/error_handler.rb index 78417f35a..655f999df 100644 --- a/lib/pact_broker/api/resources/error_handler.rb +++ b/lib/pact_broker/api/resources/error_handler.rb @@ -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) diff --git a/lib/pact_broker/configuration.rb b/lib/pact_broker/configuration.rb index e0f33c161..8a883b12b 100644 --- a/lib/pact_broker/configuration.rb +++ b/lib/pact_broker/configuration.rb @@ -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 @@ -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 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 463e1dd1d..56e9071e4 100644 --- a/spec/lib/pact_broker/api/resources/error_handler_spec.rb +++ b/spec/lib/pact_broker/api/resources/error_handler_spec.rb @@ -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') }