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 14 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. """Define custom exception."""
  16. import abc
  17. import sys
  18. from enum import unique
  19. from importlib import import_module
  20. from lib2to3.pgen2 import parse
  21. from treelib.exceptions import DuplicatedNodeIdError, MultipleRootError, NodeIDAbsentError
  22. from mindinsight.mindconverter.common.log import logger as log, logger_console as log_console
  23. from mindinsight.utils.constant import ScriptConverterErrors
  24. from mindinsight.utils.exceptions import MindInsightException
  25. @unique
  26. class ConverterErrors(ScriptConverterErrors):
  27. """Converter error codes."""
  28. SCRIPT_NOT_SUPPORT = 1
  29. NODE_TYPE_NOT_SUPPORT = 2
  30. CODE_SYNTAX_ERROR = 3
  31. NODE_INPUT_TYPE_NOT_SUPPORT = 4
  32. NODE_INPUT_MISSING = 5
  33. TREE_NODE_INSERT_FAIL = 6
  34. UNKNOWN_MODEL = 7
  35. MODEL_NOT_SUPPORT = 8
  36. SCRIPT_GENERATE_FAIL = 9
  37. REPORT_GENERATE_FAIL = 10
  38. NODE_CONVERSION_ERROR = 11
  39. INPUT_SHAPE_ERROR = 12
  40. TF_RUNTIME_ERROR = 13
  41. BASE_CONVERTER_FAIL = 000
  42. GRAPH_INIT_FAIL = 100
  43. TREE_CREATE_FAIL = 200
  44. SOURCE_FILES_SAVE_FAIL = 300
  45. GENERATOR_FAIL = 400
  46. SUB_GRAPH_SEARCHING_FAIL = 500
  47. MODEL_LOADING_FAIL = 600
  48. class ScriptNotSupport(MindInsightException):
  49. """The script can not support to process."""
  50. def __init__(self, msg):
  51. super(ScriptNotSupport, self).__init__(ConverterErrors.SCRIPT_NOT_SUPPORT,
  52. msg,
  53. http_code=400)
  54. class NodeTypeNotSupport(MindInsightException):
  55. """The astNode can not support to process."""
  56. def __init__(self, msg):
  57. super(NodeTypeNotSupport, self).__init__(ConverterErrors.NODE_TYPE_NOT_SUPPORT,
  58. msg,
  59. http_code=400)
  60. class CodeSyntaxError(MindInsightException):
  61. """The CodeSyntaxError class definition."""
  62. def __init__(self, msg):
  63. super(CodeSyntaxError, self).__init__(ConverterErrors.CODE_SYNTAX_ERROR,
  64. msg,
  65. http_code=400)
  66. class MindConverterException(Exception):
  67. """MindConverter exception."""
  68. def __init__(self, **kwargs):
  69. """Initialization of MindInsightException."""
  70. error = kwargs.get('error', None)
  71. user_msg = kwargs.get('user_msg', '')
  72. debug_msg = kwargs.get('debug_msg', '')
  73. cls_code = kwargs.get('cls_code', 0)
  74. if isinstance(user_msg, str):
  75. user_msg = ' '.join(user_msg.split())
  76. super(MindConverterException, self).__init__()
  77. self.error = error
  78. self.user_msg = user_msg
  79. self.debug_msg = debug_msg
  80. self.cls_code = cls_code
  81. def __str__(self):
  82. return '[{}] code: {}, msg: {}'.format(self.__class__.__name__, self.error_code(), self.user_msg)
  83. def __repr__(self):
  84. return self.__str__()
  85. def error_code(self):
  86. """"
  87. Calculate error code.
  88. code compose(2bytes)
  89. error: 16bits.
  90. num = 0xFFFF & error
  91. error_cods
  92. Returns:
  93. str, Hex string representing the composed MindConverter error code.
  94. """
  95. num = 0xFFFF & self.error.value
  96. error_code = ''.join((f'{self.cls_code}'.zfill(3), hex(num)[2:].zfill(4).upper()))
  97. return error_code
  98. @classmethod
  99. @abc.abstractmethod
  100. def raise_from(cls):
  101. """Raise from below exceptions."""
  102. @classmethod
  103. def uniform_catcher(cls, msg):
  104. """Uniform exception catcher."""
  105. def decorator(func):
  106. def _f(*args, **kwargs):
  107. try:
  108. res = func(*args, **kwargs)
  109. except cls.raise_from() as e:
  110. error = cls(msg=msg)
  111. detail_info = f"Error detail: {str(e)}"
  112. log_console.error(str(error))
  113. log_console.error(detail_info)
  114. log.exception(e)
  115. sys.exit(0)
  116. except ModuleNotFoundError as e:
  117. detail_info = f"Error detail: Required package not found, please check the runtime environment."
  118. log_console.error(str(e))
  119. log_console.error(detail_info)
  120. log.exception(e)
  121. sys.exit(0)
  122. return res
  123. return _f
  124. return decorator
  125. @classmethod
  126. def check_except(cls, msg):
  127. """Check except."""
  128. def decorator(func):
  129. def _f(*args, **kwargs):
  130. try:
  131. output = func(*args, **kwargs)
  132. except cls.raise_from() as e:
  133. log.error(msg)
  134. log.exception(e)
  135. raise cls(msg=msg)
  136. except Exception as e:
  137. log.error(msg)
  138. log.exception(e)
  139. raise e
  140. return output
  141. return _f
  142. return decorator
  143. class BaseConverterFail(MindConverterException):
  144. """Base converter failed."""
  145. def __init__(self, msg):
  146. super(BaseConverterFail, self).__init__(error=ConverterErrors.BASE_CONVERTER_FAIL,
  147. user_msg=msg)
  148. @classmethod
  149. def raise_from(cls):
  150. """Raise from exceptions below."""
  151. except_source = Exception, cls
  152. return except_source
  153. class UnknownModel(MindConverterException):
  154. """The unknown model error."""
  155. def __init__(self, msg):
  156. super(UnknownModel, self).__init__(error=ConverterErrors.UNKNOWN_MODEL,
  157. user_msg=msg)
  158. @classmethod
  159. def raise_from(cls):
  160. return cls
  161. class GraphInitFail(MindConverterException):
  162. """The graph init fail error."""
  163. def __init__(self, **kwargs):
  164. super(GraphInitFail, self).__init__(error=ConverterErrors.GRAPH_INIT_FAIL,
  165. user_msg=kwargs.get('msg', ''))
  166. @classmethod
  167. def raise_from(cls):
  168. """Raise from exceptions below."""
  169. except_source = (FileNotFoundError,
  170. ModuleNotFoundError,
  171. ModelNotSupport,
  172. SubGraphSearchingFail,
  173. TypeError,
  174. ZeroDivisionError,
  175. RuntimeError,
  176. cls)
  177. return except_source
  178. class TreeCreateFail(MindConverterException):
  179. """The tree create fail."""
  180. def __init__(self, msg):
  181. super(TreeCreateFail, self).__init__(error=ConverterErrors.TREE_CREATE_FAIL,
  182. user_msg=msg)
  183. @classmethod
  184. def raise_from(cls):
  185. """Raise from exceptions below."""
  186. except_source = (NodeInputMissing,
  187. TreeNodeInsertFail, cls)
  188. return except_source
  189. class SourceFilesSaveFail(MindConverterException):
  190. """The source files save fail error."""
  191. def __init__(self, msg):
  192. super(SourceFilesSaveFail, self).__init__(error=ConverterErrors.SOURCE_FILES_SAVE_FAIL,
  193. user_msg=msg)
  194. @classmethod
  195. def raise_from(cls):
  196. """Raise from exceptions below."""
  197. except_source = (NodeInputTypeNotSupport,
  198. ScriptGenerateFail,
  199. ReportGenerateFail,
  200. IOError, cls)
  201. return except_source
  202. class ModelNotSupport(MindConverterException):
  203. """The model not support error."""
  204. def __init__(self, msg):
  205. super(ModelNotSupport, self).__init__(error=ConverterErrors.MODEL_NOT_SUPPORT,
  206. user_msg=msg,
  207. cls_code=ConverterErrors.GRAPH_INIT_FAIL.value)
  208. @classmethod
  209. def raise_from(cls):
  210. """Raise from exceptions below."""
  211. except_source = (RuntimeError,
  212. ModuleNotFoundError,
  213. ValueError,
  214. AssertionError,
  215. TypeError,
  216. OSError,
  217. ZeroDivisionError, cls)
  218. return except_source
  219. class TfRuntimeError(MindConverterException):
  220. """Catch tf runtime error."""
  221. def __init__(self, msg):
  222. super(TfRuntimeError, self).__init__(error=ConverterErrors.TF_RUNTIME_ERROR,
  223. user_msg=msg,
  224. cls_code=ConverterErrors.GRAPH_INIT_FAIL.value)
  225. @classmethod
  226. def raise_from(cls):
  227. tf_error_module = import_module('tensorflow.python.framework.errors_impl')
  228. tf_error = getattr(tf_error_module, 'OpError')
  229. return tf_error, ValueError, RuntimeError, cls
  230. class NodeInputMissing(MindConverterException):
  231. """The node input missing error."""
  232. def __init__(self, msg):
  233. super(NodeInputMissing, self).__init__(error=ConverterErrors.NODE_INPUT_MISSING,
  234. user_msg=msg,
  235. cls_code=ConverterErrors.TREE_CREATE_FAIL.value)
  236. @classmethod
  237. def raise_from(cls):
  238. return ValueError, IndexError, KeyError, AttributeError, cls
  239. class TreeNodeInsertFail(MindConverterException):
  240. """The tree node create fail error."""
  241. def __init__(self, msg):
  242. super(TreeNodeInsertFail, self).__init__(error=ConverterErrors.TREE_NODE_INSERT_FAIL,
  243. user_msg=msg,
  244. cls_code=ConverterErrors.TREE_CREATE_FAIL.value)
  245. @classmethod
  246. def raise_from(cls):
  247. """Raise from exceptions below."""
  248. except_source = (OSError,
  249. DuplicatedNodeIdError,
  250. MultipleRootError,
  251. NodeIDAbsentError, cls)
  252. return except_source
  253. class NodeInputTypeNotSupport(MindConverterException):
  254. """The node input type NOT support error."""
  255. def __init__(self, msg):
  256. super(NodeInputTypeNotSupport, self).__init__(error=ConverterErrors.NODE_INPUT_TYPE_NOT_SUPPORT,
  257. user_msg=msg,
  258. cls_code=ConverterErrors.SOURCE_FILES_SAVE_FAIL.value)
  259. @classmethod
  260. def raise_from(cls):
  261. return ValueError, TypeError, IndexError, cls
  262. class ScriptGenerateFail(MindConverterException):
  263. """The script generate fail error."""
  264. def __init__(self, msg):
  265. super(ScriptGenerateFail, self).__init__(error=ConverterErrors.SCRIPT_GENERATE_FAIL,
  266. user_msg=msg,
  267. cls_code=ConverterErrors.SOURCE_FILES_SAVE_FAIL.value)
  268. @classmethod
  269. def raise_from(cls):
  270. """Raise from exceptions below."""
  271. except_source = (RuntimeError,
  272. parse.ParseError,
  273. AttributeError, cls)
  274. return except_source
  275. class ReportGenerateFail(MindConverterException):
  276. """The report generate fail error."""
  277. def __init__(self, msg):
  278. super(ReportGenerateFail, self).__init__(error=ConverterErrors.REPORT_GENERATE_FAIL,
  279. user_msg=msg,
  280. cls_code=ConverterErrors.SOURCE_FILES_SAVE_FAIL.value)
  281. @classmethod
  282. def raise_from(cls):
  283. """Raise from exceptions below."""
  284. return ZeroDivisionError, cls
  285. class SubGraphSearchingFail(MindConverterException):
  286. """Sub-graph searching exception."""
  287. def __init__(self, msg):
  288. super(SubGraphSearchingFail, self).__init__(error=ConverterErrors.MODEL_NOT_SUPPORT,
  289. cls_code=ConverterErrors.SUB_GRAPH_SEARCHING_FAIL.value,
  290. user_msg=msg)
  291. @classmethod
  292. def raise_from(cls):
  293. """Define exception in sub-graph searching module."""
  294. return IndexError, KeyError, ValueError, AttributeError, ZeroDivisionError, cls
  295. class GeneratorFail(MindConverterException):
  296. """The Generator fail error."""
  297. def __init__(self, msg):
  298. super(GeneratorFail, self).__init__(error=ConverterErrors.NODE_CONVERSION_ERROR,
  299. user_msg=msg,
  300. cls_code=ConverterErrors.GENERATOR_FAIL.value)
  301. @classmethod
  302. def raise_from(cls):
  303. """Raise from exceptions below."""
  304. except_source = (ValueError, TypeError, cls)
  305. return except_source
  306. class ModelLoadingFail(MindConverterException):
  307. """Model loading fail."""
  308. def __init__(self, msg):
  309. super(ModelLoadingFail, self).__init__(error=ConverterErrors.INPUT_SHAPE_ERROR,
  310. cls_code=ConverterErrors.MODEL_LOADING_FAIL.value,
  311. user_msg=msg)
  312. @classmethod
  313. def raise_from(cls):
  314. """Define exception when model loading fail."""
  315. return ValueError, cls