err.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import struct
  2. from .constants import ER
  3. class MySQLError(Exception):
  4. """Exception related to operation with MySQL."""
  5. class Warning(Warning, MySQLError):
  6. """Exception raised for important warnings like data truncations
  7. while inserting, etc."""
  8. class Error(MySQLError):
  9. """Exception that is the base class of all other error exceptions
  10. (not Warning)."""
  11. class InterfaceError(Error):
  12. """Exception raised for errors that are related to the database
  13. interface rather than the database itself."""
  14. class DatabaseError(Error):
  15. """Exception raised for errors that are related to the
  16. database."""
  17. class DataError(DatabaseError):
  18. """Exception raised for errors that are due to problems with the
  19. processed data like division by zero, numeric value out of range,
  20. etc."""
  21. class OperationalError(DatabaseError):
  22. """Exception raised for errors that are related to the database's
  23. operation and not necessarily under the control of the programmer,
  24. e.g. an unexpected disconnect occurs, the data source name is not
  25. found, a transaction could not be processed, a memory allocation
  26. error occurred during processing, etc."""
  27. class IntegrityError(DatabaseError):
  28. """Exception raised when the relational integrity of the database
  29. is affected, e.g. a foreign key check fails, duplicate key,
  30. etc."""
  31. class InternalError(DatabaseError):
  32. """Exception raised when the database encounters an internal
  33. error, e.g. the cursor is not valid anymore, the transaction is
  34. out of sync, etc."""
  35. class ProgrammingError(DatabaseError):
  36. """Exception raised for programming errors, e.g. table not found
  37. or already exists, syntax error in the SQL statement, wrong number
  38. of parameters specified, etc."""
  39. class NotSupportedError(DatabaseError):
  40. """Exception raised in case a method or database API was used
  41. which is not supported by the database, e.g. requesting a
  42. .rollback() on a connection that does not support transaction or
  43. has transactions turned off."""
  44. error_map = {}
  45. def _map_error(exc, *errors):
  46. for error in errors:
  47. error_map[error] = exc
  48. _map_error(
  49. ProgrammingError,
  50. ER.DB_CREATE_EXISTS,
  51. ER.SYNTAX_ERROR,
  52. ER.PARSE_ERROR,
  53. ER.NO_SUCH_TABLE,
  54. ER.WRONG_DB_NAME,
  55. ER.WRONG_TABLE_NAME,
  56. ER.FIELD_SPECIFIED_TWICE,
  57. ER.INVALID_GROUP_FUNC_USE,
  58. ER.UNSUPPORTED_EXTENSION,
  59. ER.TABLE_MUST_HAVE_COLUMNS,
  60. ER.CANT_DO_THIS_DURING_AN_TRANSACTION,
  61. ER.WRONG_DB_NAME,
  62. ER.WRONG_COLUMN_NAME,
  63. )
  64. _map_error(
  65. DataError,
  66. ER.WARN_DATA_TRUNCATED,
  67. ER.WARN_NULL_TO_NOTNULL,
  68. ER.WARN_DATA_OUT_OF_RANGE,
  69. ER.NO_DEFAULT,
  70. ER.PRIMARY_CANT_HAVE_NULL,
  71. ER.DATA_TOO_LONG,
  72. ER.DATETIME_FUNCTION_OVERFLOW,
  73. ER.TRUNCATED_WRONG_VALUE_FOR_FIELD,
  74. ER.ILLEGAL_VALUE_FOR_TYPE,
  75. )
  76. _map_error(
  77. IntegrityError,
  78. ER.DUP_ENTRY,
  79. ER.NO_REFERENCED_ROW,
  80. ER.NO_REFERENCED_ROW_2,
  81. ER.ROW_IS_REFERENCED,
  82. ER.ROW_IS_REFERENCED_2,
  83. ER.CANNOT_ADD_FOREIGN,
  84. ER.BAD_NULL_ERROR,
  85. )
  86. _map_error(
  87. NotSupportedError,
  88. ER.WARNING_NOT_COMPLETE_ROLLBACK,
  89. ER.NOT_SUPPORTED_YET,
  90. ER.FEATURE_DISABLED,
  91. ER.UNKNOWN_STORAGE_ENGINE,
  92. )
  93. _map_error(
  94. OperationalError,
  95. ER.DBACCESS_DENIED_ERROR,
  96. ER.ACCESS_DENIED_ERROR,
  97. ER.CON_COUNT_ERROR,
  98. ER.TABLEACCESS_DENIED_ERROR,
  99. ER.COLUMNACCESS_DENIED_ERROR,
  100. ER.CONSTRAINT_FAILED,
  101. ER.LOCK_DEADLOCK,
  102. )
  103. del _map_error, ER
  104. def raise_mysql_exception(data):
  105. errno = struct.unpack("<h", data[1:3])[0]
  106. errval = data[9:].decode("utf-8", "replace")
  107. errorclass = error_map.get(errno)
  108. if errorclass is None:
  109. errorclass = InternalError if errno < 1000 else OperationalError
  110. raise errorclass(errno, errval)