Skip to content

Commit

Permalink
#37 log errors
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Sep 4, 2023
1 parent 9e6d361 commit 706b7b8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
19 changes: 12 additions & 7 deletions lib/pgtk/pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,19 @@ def initialize(conn, log)
def exec(query, args = [], result = 0)
start = Time.now
sql = query.is_a?(Array) ? query.join(' ') : query
out = @conn.exec_params(sql, args, result) do |res|
if block_given?
yield res
else
rows = []
res.each { |r| rows << r }
rows
begin
out = @conn.exec_params(sql, args, result) do |res|
if block_given?
yield res
else
rows = []
res.each { |r| rows << r }
rows
end
end
rescue StandardError => e
@log.error("#{sql}: #{e.message}")
raise e
end
lag = Time.now - start
if lag < 1
Expand Down
10 changes: 10 additions & 0 deletions test/test_pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ def test_logs_sql
end
end

def test_logs_errors
log = Loog::Buffer.new
bootstrap(log: log) do |pool|
assert_raises PG::UndefinedTable do
pool.exec('INSERT INTO tableDoesNotExist (a) VALUES (42)')
end
assert(log.to_s.include?('INSERT INTO tableDoesNotExist'))
end
end

def test_transaction
bootstrap do |pool|
id = pool.transaction do |t|
Expand Down

0 comments on commit 706b7b8

Please sign in to comment.