diff options
author | Federico Di Gregorio <fog@initd.org> | 2005-05-09 09:07:07 +0000 |
---|---|---|
committer | Federico Di Gregorio <fog@initd.org> | 2005-05-09 09:07:07 +0000 |
commit | 8c2ac0658cf9882c81e90de11c1010467b55ad5e (patch) | |
tree | 5b00983d87ae0b7275552331995b687ec0a3f58a | |
parent | b1745ff139bac8890bb73e09c3965f22304753ac (diff) | |
download | psycopg2-8c2ac0658cf9882c81e90de11c1010467b55ad5e.tar.gz |
Added error codes to messages.
-rw-r--r-- | NEWS | 2 | ||||
-rw-r--r-- | psycopg/pqpath.c | 14 |
2 files changed, 14 insertions, 2 deletions
@@ -11,6 +11,8 @@ What's new in psycopg 2.0 beta 1 * Complete support for BYTEA columns and buffer objects. +* Added error codes to error messages. + * The AsIs adapter is now exported by default (also Decimal objects are adapter using the AsIs adapter (when str() is called on them they already format themselves using the right precision and scale.) diff --git a/psycopg/pqpath.c b/psycopg/pqpath.c index 0292335..c927187 100644 --- a/psycopg/pqpath.c +++ b/psycopg/pqpath.c @@ -47,6 +47,7 @@ void pq_raise(connectionObject *conn, cursorObject *curs, PyObject *exc, char *msg) { char *err = NULL; + char *code = NULL; if ((conn == NULL && curs == NULL) || (curs != NULL && conn == NULL)) { PyErr_SetString(Error, @@ -54,8 +55,14 @@ pq_raise(connectionObject *conn, cursorObject *curs, PyObject *exc, char *msg) return; } - if (curs && curs->pgres) + if (curs && curs->pgres) { err = PQresultErrorMessage(curs->pgres); +#ifdef HAVE_PQPROTOCOL3 + if (err != NULL && conn->protocol == 3) { + code = PQresultErrorField(curs->pgres, PG_DIAG_SQLSTATE); + } +#endif + } if (err == NULL) err = PQerrorMessage(conn->pgconn); @@ -100,7 +107,10 @@ pq_raise(connectionObject *conn, cursorObject *curs, PyObject *exc, char *msg) if (err && strlen(err) > 8) err = &(err[8]); /* if msg is not NULL, add it to the error message, after a '\n' */ - if (msg) { + if (msg && code) { + PyErr_Format(exc, "[%s] %s\n%s", code, err, msg); + } + else if (msg) { PyErr_Format(exc, "%s\n%s", err, msg); } else { |