Skip to content

Commit 573d204

Browse files
committed
feat: create delegate for database logger to tone down the 'table does not exist' errors that freak people out at startup
1 parent 94f3a5f commit 573d204

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

lib/pact_broker/db/logger.rb

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
require 'delegate'
2+
3+
module PactBroker
4+
module DB
5+
class Logger < SimpleDelegator
6+
def info *args
7+
__getobj__().debug(*args)
8+
end
9+
10+
def error *args
11+
if error_is_about_table_not_existing?(args)
12+
__getobj__().debug(*reassure_people_that_this_is_expected(args))
13+
else
14+
__getobj__().error(*args)
15+
end
16+
end
17+
18+
def error_is_about_table_not_existing?(args)
19+
args.first.is_a?(String) &&
20+
( args.first.include?("PG::UndefinedTable") ||
21+
args.first.include?("no such table") ||
22+
args.first.include?("no such view"))
23+
end
24+
25+
def reassure_people_that_this_is_expected(args)
26+
message = args.shift
27+
message = message + " Don't panic. This just happens when Sequel doesn't know if a table/view exists or not."
28+
[message] + args
29+
end
30+
end
31+
end
32+
end
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
require 'pact_broker/db/logger'
2+
3+
module PactBroker
4+
module DB
5+
describe Logger do
6+
let(:logs) { StringIO.new }
7+
let(:wrapped_logger) { ::Logger.new(logs) }
8+
9+
subject { Logger.new(wrapped_logger) }
10+
11+
describe "error" do
12+
context "when the error is for a table or view that does not exist" do
13+
before do
14+
subject.error("PG::UndefinedTable - some error")
15+
end
16+
17+
it "logs the message at debug level" do
18+
expect(logs.string).to include "DEBUG -- :"
19+
end
20+
21+
it "appends a friendly message so people don't freak out" do
22+
expect(logs.string).to include "PG::UndefinedTable - some error Don't panic."
23+
end
24+
end
25+
26+
context "when the error is NOT for a table or view that does not exist" do
27+
before do
28+
subject.error("foo bar")
29+
end
30+
31+
it "logs the message at error level" do
32+
expect(logs.string).to include "ERROR -- :"
33+
end
34+
35+
it "does not appends a friendly message so people will correctly panic" do
36+
expect(logs.string).to_not include "Don't panic."
37+
end
38+
end
39+
end
40+
end
41+
end
42+
end

0 commit comments

Comments
 (0)