feeder: log failing SQL statements (#45314)

This commit is contained in:
Benjamin Dauvergne 2021-05-28 08:14:39 +02:00
parent d44fafb844
commit ee4bcadcc0
2 changed files with 15 additions and 1 deletions

View File

@ -1,3 +1,5 @@
import logging
import pytest
from wcs_olap.feeder import WcsOlapFeeder
@ -11,3 +13,10 @@ def test_constructor():
with pytest.raises(ValueError):
feeder = WcsOlapFeeder(api=None, pg_dsn='', schema='x' * 64)
def test_sql_error_logging(caplog):
feeder = WcsOlapFeeder(api=None, pg_dsn='', schema='x' * 63, logger=logging.getLogger())
with pytest.raises(Exception):
feeder.ex('COIN')
assert 'Failed to execute' in caplog.text

View File

@ -10,6 +10,7 @@ import itertools
import os
import json
import hashlib
import reprlib
from .utils import Whatever
import psycopg2
import psycopg2.errorcodes
@ -333,7 +334,11 @@ class WcsOlapFeeder(object):
ctx.update(self.default_ctx)
sql = query.format(**(ctx or {}))
self.logger.debug('SQL: %s VARS: %s', sql, vars)
self.cur.execute(sql, vars=vars)
try:
self.cur.execute(sql, vars=vars)
except Exception as e:
self.logger.error('Failed to execute %r with vars %s, raised %s', sql, reprlib.repr(vars or []), e)
raise
def do_schema(self):
self.ex('SET search_path = public')