From: Daniele Varrazzo Date: Wed, 18 Mar 2020 11:25:50 +0000 (+1300) Subject: Added exceptions module X-Git-Tag: 3.0.dev0~701 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=0244fa05a0ac94c63d410d04af0e70e9d9383f9a;p=thirdparty%2Fpsycopg.git Added exceptions module --- diff --git a/psycopg3/__init__.py b/psycopg3/__init__.py index ce7a0b6a1..847d6424b 100644 --- a/psycopg3/__init__.py +++ b/psycopg3/__init__.py @@ -4,6 +4,30 @@ psycopg3 -- PostgreSQL database adaapter for Python # Copyright (C) 2020 The Psycopg Team -from .consts import VERSION as __version__ +from .consts import VERSION as __version__ # noqa -__all__ = ["__version__"] +from .exceptions import ( + Warning, + Error, + InterfaceError, + DatabaseError, + DataError, + OperationalError, + IntegrityError, + InternalError, + ProgrammingError, + NotSupportedError, +) + +__all__ = [ + "Warning", + "Error", + "InterfaceError", + "DatabaseError", + "DataError", + "OperationalError", + "IntegrityError", + "InternalError", + "ProgrammingError", + "NotSupportedError", +] diff --git a/psycopg3/exceptions.py b/psycopg3/exceptions.py new file mode 100644 index 000000000..ca2eb18e3 --- /dev/null +++ b/psycopg3/exceptions.py @@ -0,0 +1,100 @@ +""" +psycopg3 exceptions + +DBAPI-defined Exceptions are defined in the following hierarchy:: + + Exceptions + |__Warning + |__Error + |__InterfaceError + |__DatabaseError + |__DataError + |__OperationalError + |__IntegrityError + |__InternalError + |__ProgrammingError + |__NotSupportedError +""" + +# Copyright (C) 2020 The Psycopg Team + + +class Warning(Exception): + """ + Exception raised for important warnings. + + For example data truncations while inserting, etc. + """ + + +class Error(Exception): + """ + Base exception for all the errors psycopg3 will raise. + """ + + def __init__(self, *args, pgresult=None): + super().__init__(*args) + self.pgresult = pgresult + + +class InterfaceError(Error): + """ + An error related to the database interface rather than the database itself. + """ + + +class DatabaseError(Error): + """ + An error related to the database. + """ + + +class DataError(DatabaseError): + """ + An error caused by problems with the processed data. + + Examples may be division by zero, numeric value out of range, etc. + """ + + +class OperationalError(DatabaseError): + """ + An error related to the database's operation. + + These errors are not necessarily under the control of the programmer, e.g. + an unexpected disconnect occurs, the data source name is not found, a + transaction could not be processed, a memory allocation error occurred + during processing, etc. + """ + + +class IntegrityError(DatabaseError): + """ + An error caused when the relational integrity of the database is affected. + + An example may be a foreign key check failed. + """ + + +class InternalError(DatabaseError): + """ + An error generated when the database encounters an internal error, + + Examples could be the cursor is not valid anymore, the transaction is out + of sync, etc. + """ + + +class ProgrammingError(DatabaseError): + """ + Exception raised for programming errors + + Examples may be table not found or already exists, syntax error in the SQL + statement, wrong number of parameters specified, etc. + """ + + +class NotSupportedError(DatabaseError): + """ + A method or database API was used which is not supported by the database, + """ diff --git a/psycopg3/pq/pq_ctypes.py b/psycopg3/pq/pq_ctypes.py index a916e5547..9d56007c9 100644 --- a/psycopg3/pq/pq_ctypes.py +++ b/psycopg3/pq/pq_ctypes.py @@ -20,9 +20,10 @@ from .enums import ( Ping, ) from . import _pq_ctypes as impl +from ..exceptions import OperationalError -class PQerror(Exception): +class PQerror(OperationalError): pass