Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
singlestore-labs
GitHub Repository: singlestore-labs/singlestoredb-python
Path: blob/main/singlestoredb/mysql/err.py
469 views
1
import struct
2
from typing import Any
3
4
from ..exceptions import DatabaseError # noqa: F401
5
from ..exceptions import DataError # noqa: F401
6
from ..exceptions import Error # noqa: F401
7
from ..exceptions import IntegrityError # noqa: F401
8
from ..exceptions import InterfaceError # noqa: F401
9
from ..exceptions import InternalError # noqa: F401
10
from ..exceptions import MySQLError # noqa: F401
11
from ..exceptions import NotSupportedError # noqa: F401
12
from ..exceptions import OperationalError # noqa: F401
13
from ..exceptions import ProgrammingError # noqa: F401
14
from ..exceptions import Warning # noqa: F401
15
from .constants import ER
16
17
18
error_map = {}
19
20
21
def _map_error(exc: Any, *errors: int) -> None:
22
for error in errors:
23
error_map[error] = exc
24
25
26
_map_error(
27
ProgrammingError,
28
ER.DB_CREATE_EXISTS,
29
ER.SYNTAX_ERROR,
30
ER.PARSE_ERROR,
31
ER.NO_SUCH_TABLE,
32
ER.WRONG_DB_NAME,
33
ER.WRONG_TABLE_NAME,
34
ER.FIELD_SPECIFIED_TWICE,
35
ER.INVALID_GROUP_FUNC_USE,
36
ER.UNSUPPORTED_EXTENSION,
37
ER.TABLE_MUST_HAVE_COLUMNS,
38
ER.CANT_DO_THIS_DURING_AN_TRANSACTION,
39
ER.WRONG_DB_NAME,
40
ER.WRONG_COLUMN_NAME,
41
)
42
_map_error(
43
DataError,
44
ER.WARN_DATA_TRUNCATED,
45
ER.WARN_NULL_TO_NOTNULL,
46
ER.WARN_DATA_OUT_OF_RANGE,
47
ER.NO_DEFAULT,
48
ER.PRIMARY_CANT_HAVE_NULL,
49
ER.DATA_TOO_LONG,
50
ER.DATETIME_FUNCTION_OVERFLOW,
51
ER.TRUNCATED_WRONG_VALUE_FOR_FIELD,
52
ER.ILLEGAL_VALUE_FOR_TYPE,
53
)
54
_map_error(
55
IntegrityError,
56
ER.DUP_ENTRY,
57
ER.NO_REFERENCED_ROW,
58
ER.NO_REFERENCED_ROW_2,
59
ER.ROW_IS_REFERENCED,
60
ER.ROW_IS_REFERENCED_2,
61
ER.CANNOT_ADD_FOREIGN,
62
ER.BAD_NULL_ERROR,
63
)
64
_map_error(
65
NotSupportedError,
66
ER.WARNING_NOT_COMPLETE_ROLLBACK,
67
ER.NOT_SUPPORTED_YET,
68
ER.FEATURE_DISABLED,
69
ER.UNKNOWN_STORAGE_ENGINE,
70
)
71
_map_error(
72
OperationalError,
73
ER.DBACCESS_DENIED_ERROR,
74
ER.ACCESS_DENIED_ERROR,
75
ER.CON_COUNT_ERROR,
76
ER.TABLEACCESS_DENIED_ERROR,
77
ER.COLUMNACCESS_DENIED_ERROR,
78
ER.CONSTRAINT_FAILED,
79
ER.LOCK_DEADLOCK,
80
)
81
82
83
del _map_error, ER
84
85
86
def raise_mysql_exception(data: bytes) -> Exception:
87
errno = struct.unpack('<h', data[1:3])[0]
88
errval = data[9:].decode('utf-8', 'replace')
89
errorclass = error_map.get(errno)
90
if errorclass is None:
91
errorclass = InternalError if errno < 1000 else OperationalError
92
raise errorclass(errno, errval)
93
94