Path: blob/main/singlestoredb/mysql/err.py
469 views
import struct1from typing import Any23from ..exceptions import DatabaseError # noqa: F4014from ..exceptions import DataError # noqa: F4015from ..exceptions import Error # noqa: F4016from ..exceptions import IntegrityError # noqa: F4017from ..exceptions import InterfaceError # noqa: F4018from ..exceptions import InternalError # noqa: F4019from ..exceptions import MySQLError # noqa: F40110from ..exceptions import NotSupportedError # noqa: F40111from ..exceptions import OperationalError # noqa: F40112from ..exceptions import ProgrammingError # noqa: F40113from ..exceptions import Warning # noqa: F40114from .constants import ER151617error_map = {}181920def _map_error(exc: Any, *errors: int) -> None:21for error in errors:22error_map[error] = exc232425_map_error(26ProgrammingError,27ER.DB_CREATE_EXISTS,28ER.SYNTAX_ERROR,29ER.PARSE_ERROR,30ER.NO_SUCH_TABLE,31ER.WRONG_DB_NAME,32ER.WRONG_TABLE_NAME,33ER.FIELD_SPECIFIED_TWICE,34ER.INVALID_GROUP_FUNC_USE,35ER.UNSUPPORTED_EXTENSION,36ER.TABLE_MUST_HAVE_COLUMNS,37ER.CANT_DO_THIS_DURING_AN_TRANSACTION,38ER.WRONG_DB_NAME,39ER.WRONG_COLUMN_NAME,40)41_map_error(42DataError,43ER.WARN_DATA_TRUNCATED,44ER.WARN_NULL_TO_NOTNULL,45ER.WARN_DATA_OUT_OF_RANGE,46ER.NO_DEFAULT,47ER.PRIMARY_CANT_HAVE_NULL,48ER.DATA_TOO_LONG,49ER.DATETIME_FUNCTION_OVERFLOW,50ER.TRUNCATED_WRONG_VALUE_FOR_FIELD,51ER.ILLEGAL_VALUE_FOR_TYPE,52)53_map_error(54IntegrityError,55ER.DUP_ENTRY,56ER.NO_REFERENCED_ROW,57ER.NO_REFERENCED_ROW_2,58ER.ROW_IS_REFERENCED,59ER.ROW_IS_REFERENCED_2,60ER.CANNOT_ADD_FOREIGN,61ER.BAD_NULL_ERROR,62)63_map_error(64NotSupportedError,65ER.WARNING_NOT_COMPLETE_ROLLBACK,66ER.NOT_SUPPORTED_YET,67ER.FEATURE_DISABLED,68ER.UNKNOWN_STORAGE_ENGINE,69)70_map_error(71OperationalError,72ER.DBACCESS_DENIED_ERROR,73ER.ACCESS_DENIED_ERROR,74ER.CON_COUNT_ERROR,75ER.TABLEACCESS_DENIED_ERROR,76ER.COLUMNACCESS_DENIED_ERROR,77ER.CONSTRAINT_FAILED,78ER.LOCK_DEADLOCK,79)808182del _map_error, ER838485def raise_mysql_exception(data: bytes) -> Exception:86errno = struct.unpack('<h', data[1:3])[0]87errval = data[9:].decode('utf-8', 'replace')88errorclass = error_map.get(errno)89if errorclass is None:90errorclass = InternalError if errno < 1000 else OperationalError91raise errorclass(errno, errval)929394