-
-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create delegate for database logger to tone down the 'table doe…
…s not exist' errors that freak people out at startup
- Loading branch information
Showing
2 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |