errors.py 2.79 KB
Newer Older
Lukáš Lalinský's avatar
Lukáš Lalinský committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright 2015 Lukas Lalinsky
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

Lukáš Lalinský's avatar
Lukáš Lalinský committed
15 16 17 18 19 20
__all__ = [
    'Warning', 'Error', 'InterfaceError', 'DatabaseError', 'DataError',
    'OperationalError', 'IntegrityError', 'InternalError',
    'ProgrammingError', 'NotSupportedError',
]

Lukáš Lalinský's avatar
Lukáš Lalinský committed
21 22 23 24
try:
    _StandardError = StandardError
except NameError:
    _StandardError = Exception
Lukáš Lalinský's avatar
Lukáš Lalinský committed
25

Lukáš Lalinský's avatar
Lukáš Lalinský committed
26 27

class Warning(_StandardError):
28 29
    """Not used by this package, only defined for compatibility
    with DB API 2.0."""
Lukáš Lalinský's avatar
Lukáš Lalinský committed
30 31


Lukáš Lalinský's avatar
Lukáš Lalinský committed
32
class Error(_StandardError):
33 34 35 36
    """Exception that is the base class of all other error exceptions.
    You can use this to catch all errors with one single except statement."""

    def __init__(self, message, code=None, sqlstate=None, cause=None):
Lukáš Lalinský's avatar
Lukáš Lalinský committed
37
        super(_StandardError, self).__init__(message, code, sqlstate, cause)
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

    @property
    def message(self):
        return self.args[0]

    @property
    def code(self):
        return self.args[1]

    @property
    def sqlstate(self):
        return self.args[2]

    @property
    def cause(self):
        return self.args[3]
Lukáš Lalinský's avatar
Lukáš Lalinský committed
54 55 56


class InterfaceError(Error):
57 58
    """Exception raised for errors that are related to the database
    interface rather than the database itself."""
Lukáš Lalinský's avatar
Lukáš Lalinský committed
59 60 61


class DatabaseError(Error):
62
    """Exception raised for errors that are related to the database."""
Lukáš Lalinský's avatar
Lukáš Lalinský committed
63 64 65


class DataError(DatabaseError):
66 67 68
    """Exception raised for errors that are due to problems with the
    processed data like division by zero, numeric value out of range,
    etc."""
Lukáš Lalinský's avatar
Lukáš Lalinský committed
69 70 71


class OperationalError(DatabaseError):
72 73 74 75 76
    """Raised for errors that are related to the database's operation and 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."""
Lukáš Lalinský's avatar
Lukáš Lalinský committed
77 78 79


class IntegrityError(DatabaseError):
80
    """Raised when the relational integrity of the database is affected, e.g. a foreign key check fails."""
Lukáš Lalinský's avatar
Lukáš Lalinský committed
81 82 83


class InternalError(DatabaseError):
84
    """Raised when the database encounters an internal problem."""
Lukáš Lalinský's avatar
Lukáš Lalinský committed
85 86 87


class ProgrammingError(DatabaseError):
88
    """Raises for programming errors, e.g. table not found, syntax error, etc."""
Lukáš Lalinský's avatar
Lukáš Lalinský committed
89 90 91


class NotSupportedError(DatabaseError):
92
    """Raised when using an API that is not supported by the database."""