You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

exceptions.py 9.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. """
  16. Define custom exception
  17. Error rule:
  18. EXCEPTIONS key: The error code key should be as same as the realized class name.
  19. exception No: common module error No is 1-99,
  20. unknown error No is 0,
  21. shard* api error No is 100-199
  22. file* api error No is 200-299
  23. error message: It is the base error message.
  24. You can recover error message in realized class __init__ function.
  25. """
  26. from .enums import LogRuntime, ErrorCodeType, ErrorLevel
  27. from .constant import SYS_ID
  28. EXCEPTIONS = dict(
  29. # the format of list is [exception No, base error message]
  30. UnknownError=[0, 'Unknown Error.'],
  31. ParamTypeError=[1, 'Param type is error.'],
  32. ParamValueError=[2, 'Param value is error.'],
  33. ParamMissError=[3, 'Param missing.'],
  34. PathNotExistsError=[4, 'Path does not exist.'],
  35. DbConnectionError=[5, 'Db connection is error.'],
  36. # MindRecord error 100-199 for shard*
  37. MRMOpenError=[100, 'MindRecord could not open.'],
  38. MRMOpenForAppendError=[101, 'MindRecord could not open for append.'],
  39. MRMInvalidPageSizeError=[102, 'Failed to set page size.'],
  40. MRMInvalidHeaderSizeError=[103, 'Failed to set header size.'],
  41. MRMSetHeaderError=[104, 'Failed to set header.'],
  42. MRMWriteDatasetError=[105, 'Failed to write dataset.'],
  43. MRMCommitError=[107, 'Failed to commit.'],
  44. MRMLaunchError=[108, 'Failed to launch.'],
  45. MRMFinishError=[109, 'Failed to finish.'],
  46. MRMCloseError=[110, 'Failed to close.'],
  47. MRMAddSchemaError=[111, 'Failed to add schema.'],
  48. MRMAddIndexError=[112, 'Failed to add index field.'],
  49. MRMBuildSchemaError=[113, 'Failed to build schema.'],
  50. MRMGetMetaError=[114, 'Failed to get meta info.'],
  51. MRMIndexGeneratorError=[115, 'Failed to create index generator.'],
  52. MRMGenerateIndexError=[116, 'Failed to generate index.'],
  53. MRMInitSegmentError=[117, 'Failed to initialize segment.'],
  54. MRMFetchCandidateFieldsError=[118, 'Failed to fetch candidate category fields.'],
  55. MRMReadCategoryInfoError=[119, 'Failed to read category information.'],
  56. MRMFetchDataError=[120, 'Failed to fetch data by category.'],
  57. # MindRecord error 200-299 for File* and MindPage
  58. MRMInvalidSchemaError=[200, 'Schema is error.'],
  59. MRMValidateDataError=[201, 'Raw data is valid.'],
  60. MRMDefineIndexError=[202, 'Index field is error.'],
  61. MRMDefineBlobError=[203, 'Blob field is error.'],
  62. MRMUnsupportedSchemaError=[204, 'Schema is not supported.'],
  63. MRMDefineCategoryError=[205, 'Category field is error.'],
  64. )
  65. class MindRecordException(Exception):
  66. """MindRecord base error class."""
  67. def __init__(self):
  68. """Initialize an error which may occurs in mindrecord."""
  69. super(MindRecordException, self).__init__()
  70. class_name = self.__class__.__name__
  71. error_item = EXCEPTIONS[class_name] if class_name in EXCEPTIONS \
  72. else EXCEPTIONS['UnknownError']
  73. self._error_msg = error_item[1]
  74. self._error_code = MindRecordException.transform_error_code(error_item[0])
  75. @property
  76. def error_msg(self):
  77. """return the description of this error."""
  78. return self._error_msg
  79. @error_msg.setter
  80. def error_msg(self, msg):
  81. self._error_msg = msg
  82. @property
  83. def error_code(self):
  84. """return the unique error number of this error."""
  85. return self._error_code
  86. def __str__(self):
  87. return "[{}]: error_code: {}, error_msg: {}".format(
  88. self.__class__.__name__, self._error_code, self._error_msg)
  89. @staticmethod
  90. def transform_error_code(exception_no):
  91. """
  92. Transform mindrecord exception no to GE error code.
  93. error_code = ((0xFF & runtime) << 30) \
  94. | ((0xFF & error_code_type) << 28) \
  95. | ((0xFF & error_level) << 25) \
  96. | ((0xFF & sys_id) << 17) \
  97. | ((0xFF & mod_id) << 12) \
  98. | (0x0FFF & exception_no)
  99. Args:
  100. exception_no: Integer. Exception number.
  101. Returns:
  102. Integer, error code.
  103. """
  104. runtime = LogRuntime.RT_HOST
  105. error_code_type = ErrorCodeType.ERROR_CODE
  106. error_level = ErrorLevel.COMMON_LEVEL
  107. exception_no_range_per_module = 100
  108. mod_id = int(exception_no / exception_no_range_per_module) + 1
  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. return error_code
  116. class UnknownError(MindRecordException):
  117. """Raise an unknown error when an unknown error occurs."""
  118. class ParamValueError(MindRecordException):
  119. """
  120. Request param value error.
  121. """
  122. def __init__(self, error_detail):
  123. super(ParamValueError, self).__init__()
  124. self.error_msg = 'Invalid parameter value. {}'.format(error_detail)
  125. class ParamTypeError(MindRecordException):
  126. """
  127. Request param type error.
  128. """
  129. def __init__(self, param_name, expected_type):
  130. super(ParamTypeError, self).__init__()
  131. self.error_msg = "Invalid parameter type. '{}' expect {} type." \
  132. "".format(param_name, expected_type)
  133. class ParamMissError(MindRecordException):
  134. """
  135. missing param error.
  136. """
  137. def __init__(self, param_name):
  138. super(ParamMissError, self).__init__()
  139. self.error_msg = "Param missing. '{}' is required.".format(param_name)
  140. class PathNotExistsError(MindRecordException):
  141. """
  142. invalid path.
  143. """
  144. def __init__(self, error_path):
  145. super(PathNotExistsError, self).__init__()
  146. self.error_msg = 'Invalid path. {}'.format(error_path)
  147. class DbConnectionError(MindRecordException):
  148. """
  149. Database connection error.
  150. """
  151. def __init__(self, error_detail):
  152. super(DbConnectionError, self).__init__()
  153. self.error_msg = 'Db connection is error. Detail: {}'.format(error_detail)
  154. ##
  155. class MRMOpenError(MindRecordException):
  156. """
  157. Raised when could not open mind record file successfully.
  158. """
  159. def __init__(self):
  160. super(MRMOpenError, self).__init__()
  161. self.error_msg = 'MindRecord File could not open successfully.'
  162. class MRMOpenForAppendError(MindRecordException):
  163. """
  164. Raised when could not open mind record file successfully for append.
  165. """
  166. def __init__(self):
  167. super(MRMOpenForAppendError, self).__init__()
  168. self.error_msg = 'MindRecord File could not open successfully for append.'
  169. class MRMInvalidPageSizeError(MindRecordException):
  170. pass
  171. class MRMInvalidHeaderSizeError(MindRecordException):
  172. pass
  173. class MRMSetHeaderError(MindRecordException):
  174. pass
  175. class MRMWriteDatasetError(MindRecordException):
  176. pass
  177. class MRMCommitError(MindRecordException):
  178. pass
  179. class MRMLaunchError(MindRecordException):
  180. pass
  181. class MRMFinishError(MindRecordException):
  182. pass
  183. class MRMCloseError(MindRecordException):
  184. pass
  185. class MRMAddSchemaError(MindRecordException):
  186. pass
  187. class MRMAddIndexError(MindRecordException):
  188. pass
  189. class MRMBuildSchemaError(MindRecordException):
  190. pass
  191. class MRMGetMetaError(MindRecordException):
  192. pass
  193. class MRMIndexGeneratorError(MindRecordException):
  194. pass
  195. class MRMGenerateIndexError(MindRecordException):
  196. pass
  197. class MRMInitSegmentError(MindRecordException):
  198. pass
  199. class MRMFetchCandidateFieldsError(MindRecordException):
  200. pass
  201. class MRMReadCategoryInfoError(MindRecordException):
  202. pass
  203. class MRMFetchDataError(MindRecordException):
  204. pass
  205. class MRMInvalidSchemaError(MindRecordException):
  206. def __init__(self, error_detail):
  207. super(MRMInvalidSchemaError, self).__init__()
  208. self.error_msg = 'Schema format is error. Detail: {}'.format(error_detail)
  209. class MRMValidateDataError(MindRecordException):
  210. def __init__(self, error_detail):
  211. super(MRMValidateDataError, self).__init__()
  212. self.error_msg = 'Raw data do not match the schema. Detail: {}'.format(error_detail)
  213. class MRMDefineIndexError(MindRecordException):
  214. def __init__(self, error_detail):
  215. super(MRMDefineIndexError, self).__init__()
  216. self.error_msg = 'Failed to define index field. Detail: {}'.format(error_detail)
  217. class MRMDefineBlobError(MindRecordException):
  218. def __init__(self, error_detail):
  219. super(MRMDefineBlobError, self).__init__()
  220. self.error_msg = 'Failed to define blob field. Detail: {}'.format(error_detail)
  221. class MRMUnsupportedSchemaError(MindRecordException):
  222. def __init__(self, error_detail):
  223. super(MRMUnsupportedSchemaError, self).__init__()
  224. self.error_msg = 'Schema is not supported. Detail: {}'.format(error_detail)
  225. class MRMDefineCategoryError(MindRecordException):
  226. def __init__(self, error_detail):
  227. super(MRMDefineCategoryError, self).__init__()
  228. self.error_msg = 'Failed to define category field. Detail: {}'.format(error_detail)