1# Copyright 2019 Huawei Technologies Co., Ltd 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================== 15""" 16Define custom exception 17 18Error rule: 19 EXCEPTIONS key: The error code key should be as same as the realized class name. 20 exception No: common module error No is 1-99, 21 unknown error No is 0, 22 shard* api error No is 100-199 23 file* api error No is 200-299 24 error message: It is the base error message. 25 You can recover error message in realized class __init__ function. 26""" 27from .enums import LogRuntime, ErrorCodeType, ErrorLevel 28from .constant import SYS_ID 29EXCEPTIONS = dict( 30 # the format of list is [exception No, base error message] 31 UnknownError=[0, 'Unknown Error.'], 32 ParamTypeError=[1, 'Param type is error.'], 33 ParamValueError=[2, 'Param value is error.'], 34 ParamMissError=[3, 'Param missing.'], 35 PathNotExistsError=[4, 'Path does not exist.'], 36 DbConnectionError=[5, 'Db connection is error.'], 37 38 # MindRecord error 100-199 for shard* 39 MRMOpenError=[100, 'MindRecord could not open.'], 40 MRMOpenForAppendError=[101, 'MindRecord could not open for append.'], 41 MRMInvalidPageSizeError=[102, 'Failed to set page size.'], 42 MRMInvalidHeaderSizeError=[103, 'Failed to set header size.'], 43 MRMSetHeaderError=[104, 'Failed to set header.'], 44 MRMWriteDatasetError=[105, 'Failed to write dataset.'], 45 MRMCommitError=[107, 'Failed to commit.'], 46 47 MRMLaunchError=[108, 'Failed to launch.'], 48 MRMFinishError=[109, 'Failed to finish.'], 49 MRMCloseError=[110, 'Failed to close.'], 50 51 MRMAddSchemaError=[111, 'Failed to add schema.'], 52 MRMAddIndexError=[112, 'Failed to add index field.'], 53 MRMBuildSchemaError=[113, 'Failed to build schema.'], 54 MRMGetMetaError=[114, 'Failed to get meta info.'], 55 56 MRMIndexGeneratorError=[115, 'Failed to create index generator.'], 57 MRMGenerateIndexError=[116, 'Failed to generate index.'], 58 59 MRMInitSegmentError=[117, 'Failed to initialize segment.'], 60 MRMFetchCandidateFieldsError=[118, 'Failed to fetch candidate category fields.'], 61 MRMReadCategoryInfoError=[119, 'Failed to read category information.'], 62 MRMFetchDataError=[120, 'Failed to fetch data by category.'], 63 64 65 # MindRecord error 200-299 for File* and MindPage 66 MRMInvalidSchemaError=[200, 'Schema is error.'], 67 MRMValidateDataError=[201, 'Raw data is valid.'], 68 MRMDefineIndexError=[202, 'Index field is error.'], 69 MRMDefineBlobError=[203, 'Blob field is error.'], 70 MRMUnsupportedSchemaError=[204, 'Schema is not supported.'], 71 MRMDefineCategoryError=[205, 'Category field is error.'], 72 73) 74 75 76class MindRecordException(Exception): 77 """MindRecord base error class.""" 78 79 def __init__(self): 80 """Initialize an error which may occurs in mindrecord.""" 81 super(MindRecordException, self).__init__() 82 class_name = self.__class__.__name__ 83 error_item = EXCEPTIONS.get(class_name) if class_name in EXCEPTIONS else EXCEPTIONS.get('UnknownError') 84 self._error_msg = error_item[1] 85 self._error_code = MindRecordException.transform_error_code(error_item[0]) 86 87 def __str__(self): 88 return "[{}]: {}".format(self.__class__.__name__, self._error_msg) 89 90 @property 91 def error_msg(self): 92 """return the description of this error.""" 93 return self._error_msg 94 95 @error_msg.setter 96 def error_msg(self, msg): 97 self._error_msg = msg 98 99 @property 100 def error_code(self): 101 """return the unique error number of this error.""" 102 return self._error_code 103 104 @staticmethod 105 def transform_error_code(exception_no): 106 """ 107 Transform mindrecord exception no to GE error code. 108 109 error_code = ((0xFF & runtime) << 30) \ 110 | ((0xFF & error_code_type) << 28) \ 111 | ((0xFF & error_level) << 25) \ 112 | ((0xFF & sys_id) << 17) \ 113 | ((0xFF & mod_id) << 12) \ 114 | (0x0FFF & exception_no) 115 Args: 116 exception_no: Integer. Exception number. 117 118 Returns: 119 Integer, error code. 120 121 """ 122 runtime = LogRuntime.RT_HOST 123 error_code_type = ErrorCodeType.ERROR_CODE 124 error_level = ErrorLevel.COMMON_LEVEL 125 mod_id = int(exception_no / 100) + 1 126 error_code = (((0xFF & runtime) << 30) 127 | ((0xFF & error_code_type) << 28) 128 | ((0xFF & error_level) << 25) 129 | ((0xFF & SYS_ID) << 17) 130 | ((0xFF & mod_id) << 12) 131 | (0x0FFF & exception_no)) 132 return error_code 133 134 135class UnknownError(MindRecordException): 136 """Raise an unknown error when an unknown error occurs.""" 137 138 139class ParamValueError(MindRecordException): 140 """ 141 Request param value error. 142 """ 143 144 def __init__(self, error_detail): 145 super(ParamValueError, self).__init__() 146 self.error_msg = 'Invalid parameter value. {}'.format(error_detail) 147 148 149class ParamTypeError(MindRecordException): 150 """ 151 Request param type error. 152 """ 153 154 def __init__(self, param_name, expected_type): 155 super(ParamTypeError, self).__init__() 156 self.error_msg = "Invalid parameter type. '{}' expect {} type." \ 157 "".format(param_name, expected_type) 158 159 160class ParamMissError(MindRecordException): 161 """ 162 missing param error. 163 """ 164 165 def __init__(self, param_name): 166 super(ParamMissError, self).__init__() 167 self.error_msg = "Param missing. '{}' is required.".format(param_name) 168 169 170class PathNotExistsError(MindRecordException): 171 """ 172 invalid path. 173 """ 174 def __init__(self, error_path): 175 super(PathNotExistsError, self).__init__() 176 self.error_msg = 'Invalid path. {}'.format(error_path) 177 178 179class DbConnectionError(MindRecordException): 180 """ 181 Database connection error. 182 """ 183 def __init__(self, error_detail): 184 super(DbConnectionError, self).__init__() 185 self.error_msg = 'Db connection is error. Detail: {}'.format(error_detail) 186 187 188class MRMOpenError(MindRecordException): 189 """ 190 Raised when could not open mind record file successfully. 191 """ 192 def __init__(self): 193 super(MRMOpenError, self).__init__() 194 self.error_msg = 'MindRecord File could not open successfully.' 195 196 197class MRMOpenForAppendError(MindRecordException): 198 """ 199 Raised when could not open mind record file successfully for append. 200 """ 201 def __init__(self): 202 super(MRMOpenForAppendError, self).__init__() 203 self.error_msg = 'MindRecord File could not open successfully for append.' 204 205 206class MRMInvalidPageSizeError(MindRecordException): 207 pass 208 209 210class MRMInvalidHeaderSizeError(MindRecordException): 211 pass 212 213 214class MRMSetHeaderError(MindRecordException): 215 pass 216 217 218class MRMWriteDatasetError(MindRecordException): 219 pass 220 221 222class MRMCommitError(MindRecordException): 223 pass 224 225 226class MRMLaunchError(MindRecordException): 227 pass 228 229 230class MRMFinishError(MindRecordException): 231 pass 232 233 234class MRMCloseError(MindRecordException): 235 pass 236 237 238class MRMAddSchemaError(MindRecordException): 239 pass 240 241 242class MRMAddIndexError(MindRecordException): 243 pass 244 245 246class MRMBuildSchemaError(MindRecordException): 247 pass 248 249 250class MRMGetMetaError(MindRecordException): 251 pass 252 253 254class MRMIndexGeneratorError(MindRecordException): 255 pass 256 257 258class MRMGenerateIndexError(MindRecordException): 259 pass 260 261 262class MRMInitSegmentError(MindRecordException): 263 pass 264 265 266class MRMFetchCandidateFieldsError(MindRecordException): 267 pass 268 269 270class MRMReadCategoryInfoError(MindRecordException): 271 pass 272 273 274class MRMFetchDataError(MindRecordException): 275 pass 276 277 278class MRMInvalidSchemaError(MindRecordException): 279 def __init__(self, error_detail): 280 super(MRMInvalidSchemaError, self).__init__() 281 self.error_msg = 'Schema format is error. Detail: {}'.format(error_detail) 282 283 284class MRMValidateDataError(MindRecordException): 285 def __init__(self, error_detail): 286 super(MRMValidateDataError, self).__init__() 287 self.error_msg = 'Raw data do not match the schema. Detail: {}'.format(error_detail) 288 289 290class MRMDefineIndexError(MindRecordException): 291 def __init__(self, error_detail): 292 super(MRMDefineIndexError, self).__init__() 293 self.error_msg = 'Failed to define index field. Detail: {}'.format(error_detail) 294 295 296class MRMDefineBlobError(MindRecordException): 297 def __init__(self, error_detail): 298 super(MRMDefineBlobError, self).__init__() 299 self.error_msg = 'Failed to define blob field. Detail: {}'.format(error_detail) 300 301 302class MRMUnsupportedSchemaError(MindRecordException): 303 def __init__(self, error_detail): 304 super(MRMUnsupportedSchemaError, self).__init__() 305 self.error_msg = 'Schema is not supported. Detail: {}'.format(error_detail) 306 307 308class MRMDefineCategoryError(MindRecordException): 309 def __init__(self, error_detail): 310 super(MRMDefineCategoryError, self).__init__() 311 self.error_msg = 'Failed to define category field. Detail: {}'.format(error_detail) 312