Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pymysql/err.py: 80%

30 statements  

« prev     ^ index     » next       coverage.py v7.3.1, created at 2023-09-25 06:28 +0000

1import struct 

2 

3from .constants import ER 

4 

5 

6class MySQLError(Exception): 

7 """Exception related to operation with MySQL.""" 

8 

9 

10class Warning(Warning, MySQLError): 

11 """Exception raised for important warnings like data truncations 

12 while inserting, etc.""" 

13 

14 

15class Error(MySQLError): 

16 """Exception that is the base class of all other error exceptions 

17 (not Warning).""" 

18 

19 

20class InterfaceError(Error): 

21 """Exception raised for errors that are related to the database 

22 interface rather than the database itself.""" 

23 

24 

25class DatabaseError(Error): 

26 """Exception raised for errors that are related to the 

27 database.""" 

28 

29 

30class DataError(DatabaseError): 

31 """Exception raised for errors that are due to problems with the 

32 processed data like division by zero, numeric value out of range, 

33 etc.""" 

34 

35 

36class OperationalError(DatabaseError): 

37 """Exception raised for errors that are related to the database's 

38 operation and not necessarily under the control of the programmer, 

39 e.g. an unexpected disconnect occurs, the data source name is not 

40 found, a transaction could not be processed, a memory allocation 

41 error occurred during processing, etc.""" 

42 

43 

44class IntegrityError(DatabaseError): 

45 """Exception raised when the relational integrity of the database 

46 is affected, e.g. a foreign key check fails, duplicate key, 

47 etc.""" 

48 

49 

50class InternalError(DatabaseError): 

51 """Exception raised when the database encounters an internal 

52 error, e.g. the cursor is not valid anymore, the transaction is 

53 out of sync, etc.""" 

54 

55 

56class ProgrammingError(DatabaseError): 

57 """Exception raised for programming errors, e.g. table not found 

58 or already exists, syntax error in the SQL statement, wrong number 

59 of parameters specified, etc.""" 

60 

61 

62class NotSupportedError(DatabaseError): 

63 """Exception raised in case a method or database API was used 

64 which is not supported by the database, e.g. requesting a 

65 .rollback() on a connection that does not support transaction or 

66 has transactions turned off.""" 

67 

68 

69error_map = {} 

70 

71 

72def _map_error(exc, *errors): 

73 for error in errors: 

74 error_map[error] = exc 

75 

76 

77_map_error( 

78 ProgrammingError, 

79 ER.DB_CREATE_EXISTS, 

80 ER.SYNTAX_ERROR, 

81 ER.PARSE_ERROR, 

82 ER.NO_SUCH_TABLE, 

83 ER.WRONG_DB_NAME, 

84 ER.WRONG_TABLE_NAME, 

85 ER.FIELD_SPECIFIED_TWICE, 

86 ER.INVALID_GROUP_FUNC_USE, 

87 ER.UNSUPPORTED_EXTENSION, 

88 ER.TABLE_MUST_HAVE_COLUMNS, 

89 ER.CANT_DO_THIS_DURING_AN_TRANSACTION, 

90 ER.WRONG_DB_NAME, 

91 ER.WRONG_COLUMN_NAME, 

92) 

93_map_error( 

94 DataError, 

95 ER.WARN_DATA_TRUNCATED, 

96 ER.WARN_NULL_TO_NOTNULL, 

97 ER.WARN_DATA_OUT_OF_RANGE, 

98 ER.NO_DEFAULT, 

99 ER.PRIMARY_CANT_HAVE_NULL, 

100 ER.DATA_TOO_LONG, 

101 ER.DATETIME_FUNCTION_OVERFLOW, 

102 ER.TRUNCATED_WRONG_VALUE_FOR_FIELD, 

103 ER.ILLEGAL_VALUE_FOR_TYPE, 

104) 

105_map_error( 

106 IntegrityError, 

107 ER.DUP_ENTRY, 

108 ER.NO_REFERENCED_ROW, 

109 ER.NO_REFERENCED_ROW_2, 

110 ER.ROW_IS_REFERENCED, 

111 ER.ROW_IS_REFERENCED_2, 

112 ER.CANNOT_ADD_FOREIGN, 

113 ER.BAD_NULL_ERROR, 

114) 

115_map_error( 

116 NotSupportedError, 

117 ER.WARNING_NOT_COMPLETE_ROLLBACK, 

118 ER.NOT_SUPPORTED_YET, 

119 ER.FEATURE_DISABLED, 

120 ER.UNKNOWN_STORAGE_ENGINE, 

121) 

122_map_error( 

123 OperationalError, 

124 ER.DBACCESS_DENIED_ERROR, 

125 ER.ACCESS_DENIED_ERROR, 

126 ER.CON_COUNT_ERROR, 

127 ER.TABLEACCESS_DENIED_ERROR, 

128 ER.COLUMNACCESS_DENIED_ERROR, 

129 ER.CONSTRAINT_FAILED, 

130 ER.LOCK_DEADLOCK, 

131) 

132 

133 

134del _map_error, ER 

135 

136 

137def raise_mysql_exception(data): 

138 errno = struct.unpack("<h", data[1:3])[0] 

139 errval = data[9:].decode("utf-8", "replace") 

140 errorclass = error_map.get(errno) 

141 if errorclass is None: 

142 errorclass = InternalError if errno < 1000 else OperationalError 

143 raise errorclass(errno, errval)