summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--NEWS2
-rw-r--r--psycopg/pqpath.c14
2 files changed, 14 insertions, 2 deletions
diff --git a/NEWS b/NEWS
index d9201a1..6908624 100644
--- a/NEWS
+++ b/NEWS
@@ -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 {