mirror of
https://github.com/psycopg/psycopg2.git
synced 2025-02-07 12:50:32 +03:00
Added errors.lookup() function
This commit is contained in:
parent
5da968d6f6
commit
e7227ce87b
|
@ -56,3 +56,5 @@ idiomatic error handler:
|
|||
For completeness, the module also exposes all the DB-API-defined classes and
|
||||
:ref:`a few psycopg-specific exceptions <extension-exceptions>` previously
|
||||
exposed by the `!extensions` module. One stop shop for all your mistakes...
|
||||
|
||||
.. autofunction:: lookup
|
||||
|
|
|
@ -10,6 +10,14 @@ from psycopg2._psycopg import (
|
|||
QueryCanceledError, TransactionRollbackError)
|
||||
|
||||
|
||||
def lookup(code):
|
||||
"""Lookup an error code and return its exception class.
|
||||
|
||||
Raise `!KeyError` if the code is not found.
|
||||
"""
|
||||
return _by_sqlstate[code]
|
||||
|
||||
|
||||
_by_sqlstate = {}
|
||||
|
||||
|
||||
|
|
|
@ -54,6 +54,14 @@ class ErrorsTests(ConnectingTestCase):
|
|||
|
||||
self.assertEqual(type(e), self.conn.ProgrammingError)
|
||||
|
||||
def test_lookup(self):
|
||||
from psycopg2 import errors
|
||||
|
||||
self.assertIs(errors.lookup('42P01'), errors.UndefinedTable)
|
||||
|
||||
with self.assertRaises(KeyError):
|
||||
errors.lookup('XXXXX')
|
||||
|
||||
|
||||
def test_suite():
|
||||
return unittest.TestLoader().loadTestsFromName(__name__)
|
||||
|
|
Loading…
Reference in New Issue
Block a user