Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/pymysql/err.py: 73%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import struct
3from .constants import ER
6class MySQLError(Exception):
7 """Exception related to operation with MySQL."""
10class Warning(Warning, MySQLError):
11 """Exception raised for important warnings like data truncations
12 while inserting, etc."""
15class Error(MySQLError):
16 """Exception that is the base class of all other error exceptions
17 (not Warning)."""
19 def __init__(self, *args, sqlstate=None):
20 super().__init__(*args)
21 self.sqlstate = sqlstate
24class InterfaceError(Error):
25 """Exception raised for errors that are related to the database
26 interface rather than the database itself."""
29class DatabaseError(Error):
30 """Exception raised for errors that are related to the
31 database."""
34class DataError(DatabaseError):
35 """Exception raised for errors that are due to problems with the
36 processed data like division by zero, numeric value out of range,
37 etc."""
40class OperationalError(DatabaseError):
41 """Exception raised for errors that are related to the database's
42 operation and not necessarily under the control of the programmer,
43 e.g. an unexpected disconnect occurs, the data source name is not
44 found, a transaction could not be processed, a memory allocation
45 error occurred during processing, etc."""
48class IntegrityError(DatabaseError):
49 """Exception raised when the relational integrity of the database
50 is affected, e.g. a foreign key check fails, duplicate key,
51 etc."""
54class InternalError(DatabaseError):
55 """Exception raised when the database encounters an internal
56 error, e.g. the cursor is not valid anymore, the transaction is
57 out of sync, etc."""
60class ProgrammingError(DatabaseError):
61 """Exception raised for programming errors, e.g. table not found
62 or already exists, syntax error in the SQL statement, wrong number
63 of parameters specified, etc."""
66class NotSupportedError(DatabaseError):
67 """Exception raised in case a method or database API was used
68 which is not supported by the database, e.g. requesting a
69 .rollback() on a connection that does not support transaction or
70 has transactions turned off."""
73error_map = {}
76def _map_error(exc, *errors):
77 for error in errors:
78 error_map[error] = exc
81_map_error(
82 ProgrammingError,
83 ER.DB_CREATE_EXISTS,
84 ER.SYNTAX_ERROR,
85 ER.PARSE_ERROR,
86 ER.NO_SUCH_TABLE,
87 ER.WRONG_DB_NAME,
88 ER.WRONG_TABLE_NAME,
89 ER.FIELD_SPECIFIED_TWICE,
90 ER.INVALID_GROUP_FUNC_USE,
91 ER.UNSUPPORTED_EXTENSION,
92 ER.TABLE_MUST_HAVE_COLUMNS,
93 ER.CANT_DO_THIS_DURING_AN_TRANSACTION,
94 ER.WRONG_DB_NAME,
95 ER.WRONG_COLUMN_NAME,
96)
97_map_error(
98 DataError,
99 ER.WARN_DATA_TRUNCATED,
100 ER.WARN_NULL_TO_NOTNULL,
101 ER.WARN_DATA_OUT_OF_RANGE,
102 ER.NO_DEFAULT,
103 ER.PRIMARY_CANT_HAVE_NULL,
104 ER.DATA_TOO_LONG,
105 ER.DATETIME_FUNCTION_OVERFLOW,
106 ER.TRUNCATED_WRONG_VALUE_FOR_FIELD,
107 ER.ILLEGAL_VALUE_FOR_TYPE,
108)
109_map_error(
110 IntegrityError,
111 ER.DUP_ENTRY,
112 ER.NO_REFERENCED_ROW,
113 ER.NO_REFERENCED_ROW_2,
114 ER.ROW_IS_REFERENCED,
115 ER.ROW_IS_REFERENCED_2,
116 ER.CANNOT_ADD_FOREIGN,
117 ER.BAD_NULL_ERROR,
118)
119_map_error(
120 NotSupportedError,
121 ER.WARNING_NOT_COMPLETE_ROLLBACK,
122 ER.NOT_SUPPORTED_YET,
123 ER.FEATURE_DISABLED,
124 ER.UNKNOWN_STORAGE_ENGINE,
125)
126_map_error(
127 OperationalError,
128 ER.DBACCESS_DENIED_ERROR,
129 ER.ACCESS_DENIED_ERROR,
130 ER.CON_COUNT_ERROR,
131 ER.TABLEACCESS_DENIED_ERROR,
132 ER.COLUMNACCESS_DENIED_ERROR,
133 ER.CONSTRAINT_FAILED,
134 ER.LOCK_DEADLOCK,
135)
138del _map_error, ER
141def raise_mysql_exception(data):
142 errno = struct.unpack("<h", data[1:3])[0]
143 # https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_basic_err_packet.html
144 # Error packet has optional sqlstate that is 5 bytes and starts with '#'.
145 sqlstate = None
146 if data[3] == 0x23: # '#'
147 sqlstate = data[4:9].decode()
148 errval = data[9:].decode("utf-8", "replace")
149 else:
150 errval = data[3:].decode("utf-8", "replace")
151 errorclass = error_map.get(errno)
152 if errorclass is None:
153 errorclass = InternalError if errno < 1000 else OperationalError
154 raise errorclass(errno, errval, sqlstate=sqlstate)