Added error codes to messages.

This commit is contained in:
Federico Di Gregorio 2005-05-09 09:07:07 +00:00
parent b1745ff139
commit 8c2ac0658c
2 changed files with 14 additions and 2 deletions

2
NEWS
View File

@ -11,6 +11,8 @@ What's new in psycopg 2.0 beta 1
* Complete support for BYTEA columns and buffer objects. * 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 * The AsIs adapter is now exported by default (also Decimal objects are
adapter using the AsIs adapter (when str() is called on them they adapter using the AsIs adapter (when str() is called on them they
already format themselves using the right precision and scale.) already format themselves using the right precision and scale.)

View File

@ -47,6 +47,7 @@ void
pq_raise(connectionObject *conn, cursorObject *curs, PyObject *exc, char *msg) pq_raise(connectionObject *conn, cursorObject *curs, PyObject *exc, char *msg)
{ {
char *err = NULL; char *err = NULL;
char *code = NULL;
if ((conn == NULL && curs == NULL) || (curs != NULL && conn == NULL)) { if ((conn == NULL && curs == NULL) || (curs != NULL && conn == NULL)) {
PyErr_SetString(Error, PyErr_SetString(Error,
@ -54,8 +55,14 @@ pq_raise(connectionObject *conn, cursorObject *curs, PyObject *exc, char *msg)
return; return;
} }
if (curs && curs->pgres) if (curs && curs->pgres) {
err = PQresultErrorMessage(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) if (err == NULL)
err = PQerrorMessage(conn->pgconn); 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 (err && strlen(err) > 8) err = &(err[8]);
/* if msg is not NULL, add it to the error message, after a '\n' */ /* 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); PyErr_Format(exc, "%s\n%s", err, msg);
} }
else { else {