Added ConnectionInfo.error_message

This commit is contained in:
Daniele Varrazzo 2018-10-13 00:47:04 +01:00
parent 1ac6359fef
commit 439dff974d
3 changed files with 37 additions and 0 deletions

View File

@ -182,6 +182,7 @@ introspection etc.
of digits is always ``00``. For example, version 9.3.5 will be
returned as ``90305``, version 10.2 as ``100002``.
.. autoattribute:: error_message
.. class:: Column

View File

@ -238,6 +238,28 @@ server_version_get(connInfoObject *self)
}
static const char error_message_doc[] =
"The error message most recently generated by an operation on the connection.\n"
"\n"
"`!None` if there is no current message.\n"
"\n"
".. seealso:: libpq docs for `PQerrorMessage()`__ for details.\n"
".. __: https://www.postgresql.org/docs/current/static/libpq-status.html"
"#LIBPQ-PQERRORMESSAGE";
static PyObject *
error_message_get(connInfoObject *self)
{
const char *val;
val = PQerrorMessage(self->conn->pgconn);
if (!val || !val[0]) {
Py_RETURN_NONE;
}
return conn_text_from_chars(self->conn, val);
}
static struct PyGetSetDef connInfoObject_getsets[] = {
{ "dbname", (getter)dbname_get, NULL, (char *)dbname_doc },
{ "user", (getter)user_get, NULL, (char *)user_doc },
@ -252,6 +274,8 @@ static struct PyGetSetDef connInfoObject_getsets[] = {
(char *)protocol_version_doc },
{ "server_version", (getter)server_version_get, NULL,
(char *)server_version_doc },
{ "error_message", (getter)error_message_get, NULL,
(char *)error_message_doc },
{NULL}
};

View File

@ -1752,6 +1752,18 @@ class TestConnectionInfo(ConnectingTestCase):
self.assertEqual(self.bconn.info.server_version, 0)
def test_error_message(self):
self.assertIsNone(self.conn.info.error_message)
self.assertIsNotNone(self.bconn.info.error_message)
cur = self.conn.cursor()
try:
cur.execute("select 1 from nosuchtable")
except psycopg2.DatabaseError:
pass
self.assert_('nosuchtable' in self.conn.info.error_message)
def test_suite():
return unittest.TestLoader().loadTestsFromName(__name__)