]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
fix: don't barf on errors with blank sqlstate
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sat, 14 May 2022 08:22:44 +0000 (10:22 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sun, 15 May 2022 14:58:50 +0000 (16:58 +0200)
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

docs/news.rst
psycopg/psycopg/errors.py
tests/test_errors.py

index d13cc694c6efddd39d3d1c2ef5da3a184c9d564e..b78a42f184b4f6427296b843ef3e0da2acec0031 100644 (file)
@@ -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
index b9252c26839166e8b574a7b1eb43994209de1ade..cfb27ee3755693e9b830a3aa00eddeea883b9367 100644 (file)
@@ -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
     )
 
index 553aac3b01c10614ee24228ad41804bfea0bd7f6..22e38688dddf015c8315bb4fda4e50f5113325fc 100644 (file)
@@ -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