diff --git a/lib/pact_broker/db/logger.rb b/lib/pact_broker/db/logger.rb new file mode 100644 index 000000000..93979eccd --- /dev/null +++ b/lib/pact_broker/db/logger.rb @@ -0,0 +1,32 @@ +require 'delegate' + +module PactBroker + module DB + class Logger < SimpleDelegator + def info *args + __getobj__().debug(*args) + end + + def error *args + if error_is_about_table_not_existing?(args) + __getobj__().debug(*reassure_people_that_this_is_expected(args)) + else + __getobj__().error(*args) + end + end + + def error_is_about_table_not_existing?(args) + args.first.is_a?(String) && + ( args.first.include?("PG::UndefinedTable") || + args.first.include?("no such table") || + args.first.include?("no such view")) + end + + def reassure_people_that_this_is_expected(args) + message = args.shift + message = message + " Don't panic. This just happens when Sequel doesn't know if a table/view exists or not." + [message] + args + end + end + end +end diff --git a/spec/lib/pact_broker/db/logger_spec.rb b/spec/lib/pact_broker/db/logger_spec.rb new file mode 100644 index 000000000..350cdbe1e --- /dev/null +++ b/spec/lib/pact_broker/db/logger_spec.rb @@ -0,0 +1,42 @@ +require 'pact_broker/db/logger' + +module PactBroker + module DB + describe Logger do + let(:logs) { StringIO.new } + let(:wrapped_logger) { ::Logger.new(logs) } + + subject { Logger.new(wrapped_logger) } + + describe "error" do + context "when the error is for a table or view that does not exist" do + before do + subject.error("PG::UndefinedTable - some error") + end + + it "logs the message at debug level" do + expect(logs.string).to include "DEBUG -- :" + end + + it "appends a friendly message so people don't freak out" do + expect(logs.string).to include "PG::UndefinedTable - some error Don't panic." + end + end + + context "when the error is NOT for a table or view that does not exist" do + before do + subject.error("foo bar") + end + + it "logs the message at error level" do + expect(logs.string).to include "ERROR -- :" + end + + it "does not appends a friendly message so people will correctly panic" do + expect(logs.string).to_not include "Don't panic." + end + end + end + end + end +end