From: Daniele Varrazzo Date: Sat, 14 May 2022 08:22:44 +0000 (+0200) Subject: fix: don't barf on errors with blank sqlstate X-Git-Tag: 3.1~97 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f12b207dcb11475f17a929770250ab448822e9e3;p=thirdparty%2Fpsycopg.git fix: don't barf on errors with blank sqlstate Such messages are not entirely valid (sqlstate is documented to be always present), however we receive them after a SHOW HELP in the PgBouncer admin database. The SHOW HELP actually does generate a sqlstate `00000` but the message is somehow parsed incorrectly by the libpq, which goes on to report an error: message contents do not agree with length in message type "N" See #303. PgBouncer issue reported upstream in https://github.com/pgbouncer/pgbouncer/issues/718 --- diff --git a/docs/news.rst b/docs/news.rst index d13cc694c..b78a42f18 100644 --- a/docs/news.rst +++ b/docs/news.rst @@ -34,6 +34,7 @@ Psycopg 3.0.14 (unreleased) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ - Raise `DataError` dumping arrays of mixed types (:ticket:`#301`). +- Fix handling of incorrect server results, with blank sqlstate (:ticket:`#303`). Current release diff --git a/psycopg/psycopg/errors.py b/psycopg/psycopg/errors.py index b9252c268..cfb27ee37 100644 --- a/psycopg/psycopg/errors.py +++ b/psycopg/psycopg/errors.py @@ -356,7 +356,7 @@ def _class_for_state(sqlstate: str) -> Type[Error]: def get_base_exception(sqlstate: str) -> Type[Error]: return ( _base_exc_map.get(sqlstate[:2]) - or _base_exc_map.get(sqlstate[0]) + or _base_exc_map.get(sqlstate[:1]) or DatabaseError ) diff --git a/tests/test_errors.py b/tests/test_errors.py index 553aac3b0..22e38688d 100644 --- a/tests/test_errors.py +++ b/tests/test_errors.py @@ -296,3 +296,7 @@ def test_pgresult_pickle(conn): exc = pickle.loads(pickle.dumps(excinfo.value)) assert exc.pgresult is None assert exc.diag.sqlstate == "42P01" + + +def test_blank_sqlstate(conn): + assert e.get_base_exception("") is e.DatabaseError