Skip to content

Commit

Permalink
feat: create delegate for database logger to tone down the 'table doe…
Browse files Browse the repository at this point in the history
…s not exist' errors that freak people out at startup
  • Loading branch information
bethesque committed Oct 23, 2018
1 parent 94f3a5f commit 573d204
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
32 changes: 32 additions & 0 deletions lib/pact_broker/db/logger.rb
Original file line number Diff line number Diff line change
@@ -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
42 changes: 42 additions & 0 deletions spec/lib/pact_broker/db/logger_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 573d204

Please sign in to comment.