From cdca0a20e04853b3ea3e70524feef4c43992073e Mon Sep 17 00:00:00 2001 From: Justas Sadzevicius Date: Mon, 7 Sep 2020 09:44:44 +0300 Subject: [PATCH] Classify connection exceptions as operational errors to better conform with PEP 249 --- NEWS | 9 +++++++++ psycopg/error_type.c | 2 ++ tests/test_errors.py | 7 +++++++ 3 files changed, 18 insertions(+) diff --git a/NEWS b/NEWS index 3bd03f37..bd2c5a2b 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,15 @@ Current release --------------- +What's new in psycopg 2.9.0 +^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +- Reclassified SQLSTATE connection exceptions (08XXX) as + `~psycopg2.errors.OperationalError`, conforming better to + PEP 249. This error is a subclass of previously used + `~psycopg2.errors.DatabaseError`. + + What's new in psycopg 2.8.6 ^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/psycopg/error_type.c b/psycopg/error_type.c index 37b4d4ae..60b2c48e 100644 --- a/psycopg/error_type.c +++ b/psycopg/error_type.c @@ -65,6 +65,8 @@ base_exception_from_sqlstate(const char *sqlstate) switch (sqlstate[0]) { case '0': switch (sqlstate[1]) { + case '8': /* Class 08 - Connection Exception */ + return OperationalError; case 'A': /* Class 0A - Feature Not Supported */ return NotSupportedError; } diff --git a/tests/test_errors.py b/tests/test_errors.py index bd3e7fd6..ec2950c5 100755 --- a/tests/test_errors.py +++ b/tests/test_errors.py @@ -62,6 +62,13 @@ class ErrorsTests(ConnectingTestCase): with self.assertRaises(KeyError): errors.lookup('XXXXX') + def test_connection_exceptions_backwards_compatibility(self): + err = errors.lookup('08000') + # connection exceptions are classified as operational errors + self.assert_(issubclass(err, errors.OperationalError)) + # previously these errors were classified only as DatabaseError + self.assert_(issubclass(err, errors.DatabaseError)) + def test_has_base_exceptions(self): excs = [] for n in dir(psycopg2):