[database/exception] include the query information in database error for better debuging

This will change an exception from something like :

psycopg2.IntegrityError: null value in column "asource" violates not-null constraint DETAIL: Failing row contains (341471, CWRType, null, null).

To:

psycopg2.IntegrityError: when doing the query 'INSERT INTO entities ( eid, type ) VALUES ( %(eid)s, %(type)s )' with the args '{'type': u'CWRType', 'eid': 341471}' got the error 'null value in column "asource" violates not-null constraint DETAIL: Failing row contains (341471, CWRType, null, null).'

Tested with sqlite3 and postgresql.

authorLaurent Peuch <cortex@worlddomination.be>
changeset8e59f9bef80c
branchdefault
phasedraft
hiddenyes
parent revision#a4d465a3e77d fix(ci): manually remove the .tox/doc directory
child revision<not specified>
files modified by this revision
cubicweb/server/sources/native.py
# HG changeset patch
# User Laurent Peuch <cortex@worlddomination.be>
# Date 1558428691 -7200
# Tue May 21 10:51:31 2019 +0200
# Node ID 8e59f9bef80cfa3270babb53f2ad9e2c95d5b71d
# Parent a4d465a3e77d07cf6a79c121c10b2d6484cd7468
[database/exception] include the query information in database error for better debuging

This will change an exception from something like :

psycopg2.IntegrityError: null value in column "asource" violates not-null constraint
DETAIL: Failing row contains (341471, CWRType, null, null).

To:

psycopg2.IntegrityError: when doing the query 'INSERT INTO entities ( eid, type ) VALUES ( %(eid)s, %(type)s )' with the args '{'type': u'CWRType', 'eid': 341471}' got the error 'null value in column "asource" violates not-null constraint
DETAIL: Failing row contains (341471, CWRType, null, null).'

Tested with sqlite3 and postgresql.

diff --git a/cubicweb/server/sources/native.py b/cubicweb/server/sources/native.py
@@ -749,10 +749,13 @@
1                      mo = re.match('^constraint (cstr.*) failed$', arg)
2                      if mo is not None:
3                          # sqlite3 (old)
4                          raise ViolatedConstraint(cnx, cstrname=mo.group(1),
5                                                   query=query)
6 +            ex.args = ("when doing the query '%s' with the args '%s' got the error '%s'" % (
7 +                query, args, str(ex).strip()
8 +            ),)
9              raise
10          finally:
11              query_debug_informations["time"] = (time.time() - start) * 1000
12              emit_to_debug_channel("sql", query_debug_informations)
13          return cursor