]> 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:56:46 +0000 (16:56 +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 422811ad3e6330cf78fb1436a3de26c76ab1a832..33f20587f0eca8a6a7f01a16243470a3d95d4483 100644 (file)
@@ -13,7 +13,8 @@ Future releases
 Psycopg 3.0.14 (unreleased)
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
-- Raise DataError dumping arrays of mixed types (:ticket:`#301`).
+- Raise `DataError` dumping arrays of mixed types (:ticket:`#301`).
+- Fix handling of incorrect server results, with blank sqlstate (:ticket:`#303`).
 
 
 Current release
index d23ce7506422d0f68d9537ae23bb367bcc4a39d3..21fbd7e3da32b9fef7e898f37749b4fc82a619a9 100644 (file)
@@ -315,7 +315,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 5ae3b51b9d3f448052b59aee27ed5ffe6dec0c61..ba766ec24eaae53302ba749d45f14091c32340bb 100644 (file)
@@ -260,3 +260,7 @@ def test_unknown_sqlstate(conn):
     # Survives pickling too
     pexc = pickle.loads(pickle.dumps(exc))
     assert pexc.sqlstate == code
+
+
+def test_blank_sqlstate(conn):
+    assert e.get_base_exception("") is e.DatabaseError