Account for new psycopg2 exception classes mapping
From psycopg2 >= 2.8, specific exceptions are raised corresponding to
postgresql errors. E.g. a CheckViolation exception is raised instead of
a generic IntegrityError previously when a constraint violation occurs.
The way we intercept database errors, especially for constraint
violation, is not compliant with that because we do not catch subclasses
of IntegrityError in native source's doexec() method.
We fix this by checking for the presence of IntegrityError error in
exception class's mro. This is still overcomplicated and clumsy, because
we still use string comparison, but this is the best we can do as far as
I know. (A better fix would be 'isinstance(ex, IntegrityError)' but we
have no engine-independent error classes, so this is not possible.
Something like sqlalchemy's DBAPI Errors [1] might help:
https://docs.sqlalchemy.org/en/latest/errors.html#dbapi-errors)
"""twisted server for CubicWeb web instances
:organization: Logilab
:copyright: 2001-2011 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
"""
class HTTPResponse(object):
"""An object representing an HTTP Response to be sent to the client.
"""
def __init__(self, twisted_request, code=None, headers=None, stream=None):
self._headers_out = headers
self._twreq = twisted_request
self._stream = stream
self._code = code
self._init_headers()
self._finalize()
def _init_headers(self):
if self._headers_out is None:
return
# initialize headers
for k, values in self._headers_out.getAllRawHeaders():
self._twreq.responseHeaders.setRawHeaders(k, values)
# add content-length if not present
if (self._headers_out.getHeader('content-length') is None
and self._stream is not None):
self._twreq.setHeader('content-length', len(self._stream))
def _finalize(self):
# cw_failed is set on errors such as "connection aborted by client". In
# such cases, req.finish() was already called and calling it a twice
# would crash
if getattr(self._twreq, 'cw_failed', False):
return
# we must set code before writing anything, else it's too late
if self._code is not None:
self._twreq.setResponseCode(self._code)
if self._stream is not None:
self._twreq.write(str(self._stream))
self._twreq.finish()
def __repr__(self):
return "<%s.%s code=%d>" % (self.__module__, self.__class__.__name__, self._code)