]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Added exceptions module
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 18 Mar 2020 11:25:50 +0000 (00:25 +1300)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 18 Mar 2020 11:32:23 +0000 (00:32 +1300)
psycopg3/__init__.py
psycopg3/exceptions.py [new file with mode: 0644]
psycopg3/pq/pq_ctypes.py

index ce7a0b6a1b57cdf327a6f1ea18ca32be0dd55b22..847d6424be3ec243f6a8983a70ea9ebae8741c79 100644 (file)
@@ -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 (file)
index 0000000..ca2eb18
--- /dev/null
@@ -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,
+    """
index a916e55475c50482972528f080d8db929dc8c02f..9d56007c98f5ac92d5137333b8d149013ae81137 100644 (file)
@@ -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