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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. # Copyright 2020 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. """Definition of error code and relative messages in profiler module."""
  16. from mindspore.profiler.common.exceptions.error_code import ProfilerErrors, \
  17. ProfilerErrorMsg
  18. class ProfilerException(Exception):
  19. """
  20. Base class for Profilier exception.
  21. Examples:
  22. >>> raise ProfilerException(GeneralErrors.PATH_NOT_EXISTS_ERROR, 'path not exists')
  23. """
  24. RUNTIME = 1
  25. TYPE = 1
  26. LEVEL = 0
  27. SYSID = 42
  28. def __init__(self, error, message, http_code=500):
  29. """
  30. Initialization of ProfilerException.
  31. Args:
  32. error (Enum): Error value for specified case.
  33. message (str): Description for exception.
  34. http_code (int): Http code for exception. Default is 500.
  35. """
  36. if isinstance(message, str):
  37. message = ' '.join(message.split())
  38. super(ProfilerException, self).__init__(message)
  39. self.error = error
  40. self.message = message
  41. self.http_code = http_code
  42. @property
  43. def error_code(self):
  44. """
  45. Transform exception no to Profiler error code.
  46. code compose(4bytes):
  47. runtime 2bits, type 2bits, level 3bits, sysid 8bits, modid 5bits, value 12bits.
  48. num = ((0xFF & runtime) << 30) \
  49. | ((0xFF & type) << 28) \
  50. | ((0xFF & level) << 25) \
  51. | ((0xFF & sysid) << 17) \
  52. | ((0xFF & modid) << 12) \
  53. | (0x0FFF & value)
  54. Returns:
  55. str, Hex string representing the composed Profiler error code.
  56. """
  57. num = (((0xFF & self.RUNTIME) << 30)
  58. | ((0xFF & self.TYPE) << 28)
  59. | ((0xFF & self.LEVEL) << 25)
  60. | ((0xFF & self.SYSID) << 17)
  61. | ((0xFF & 6) << 12)
  62. | (0x0FFF & self.error.value))
  63. return hex(num)[2:].zfill(8).upper()
  64. def __str__(self):
  65. return '[{}] code: {}, msg: {}'.format(self.__class__.__name__, self.error_code, self.message)
  66. class ProfilerParamValueErrorException(ProfilerException):
  67. """The parameter value error in profiler module."""
  68. def __init__(self, msg):
  69. super(ProfilerParamValueErrorException, self).__init__(
  70. error=ProfilerErrors.PARAM_VALUE_ERROR,
  71. message=ProfilerErrorMsg.PARAM_VALUE_ERROR.value.format(msg),
  72. http_code=400
  73. )
  74. class ProfilerPathErrorException(ProfilerException):
  75. """The path error in profiler module."""
  76. def __init__(self, msg):
  77. super(ProfilerPathErrorException, self).__init__(
  78. error=ProfilerErrors.PATH_ERROR,
  79. message=ProfilerErrorMsg.PATH_ERROR.value.format(msg),
  80. http_code=400
  81. )
  82. class ProfilerParamTypeErrorException(ProfilerException):
  83. """The parameter type error in profiler module."""
  84. def __init__(self, msg):
  85. super(ProfilerParamTypeErrorException, self).__init__(
  86. error=ProfilerErrors.PARAM_TYPE_ERROR,
  87. message=ProfilerErrorMsg.PARAM_TYPE_ERROR.value.format(msg),
  88. http_code=400
  89. )
  90. class ProfilerDirNotFoundException(ProfilerException):
  91. """The dir not found exception in profiler module."""
  92. def __init__(self, msg):
  93. super(ProfilerDirNotFoundException, self).__init__(
  94. error=ProfilerErrors.DIR_NOT_FOUND_ERROR,
  95. message=ProfilerErrorMsg.DIR_NOT_FOUND_ERROR.value.format(msg),
  96. http_code=400
  97. )
  98. class ProfilerFileNotFoundException(ProfilerException):
  99. """The file not found exception in profiler module."""
  100. def __init__(self, msg):
  101. super(ProfilerFileNotFoundException, self).__init__(
  102. error=ProfilerErrors.FILE_NOT_FOUND_ERROR,
  103. message=ProfilerErrorMsg.FILE_NOT_FOUND_ERROR.value.format(msg),
  104. http_code=400
  105. )
  106. class ProfilerIOException(ProfilerException):
  107. """The IO exception in profiler module."""
  108. def __init__(self):
  109. super(ProfilerIOException, self).__init__(
  110. error=ProfilerErrors.IO_ERROR,
  111. message=ProfilerErrorMsg.IO_ERROR.value,
  112. http_code=400
  113. )
  114. class ProfilerDeviceIdMismatchException(ProfilerException):
  115. """The device id mismatch exception in profiler module."""
  116. def __init__(self):
  117. super(ProfilerDeviceIdMismatchException, self).__init__(
  118. error=ProfilerErrors.DEVICE_ID_MISMATCH_ERROR,
  119. message=ProfilerErrorMsg.DEVICE_ID_MISMATCH_ERROR.value,
  120. http_code=400
  121. )
  122. class ProfilerRawFileException(ProfilerException):
  123. """The raw file exception in profiler module."""
  124. def __init__(self, msg):
  125. super(ProfilerRawFileException, self).__init__(
  126. error=ProfilerErrors.RAW_FILE_ERROR,
  127. message=ProfilerErrorMsg.RAW_FILE_ERROR.value.format(msg),
  128. http_code=400
  129. )
  130. class ProfilerColumnNotExistException(ProfilerException):
  131. """The column does not exist exception in profiler module."""
  132. def __init__(self, msg):
  133. super(ProfilerColumnNotExistException, self).__init__(
  134. error=ProfilerErrors.COLUMN_NOT_EXIST_ERROR,
  135. message=ProfilerErrorMsg.COLUMN_NOT_EXIST_ERROR.value.format(msg),
  136. http_code=400
  137. )
  138. class ProfilerAnalyserNotExistException(ProfilerException):
  139. """The analyser in profiler module."""
  140. def __init__(self, msg):
  141. super(ProfilerAnalyserNotExistException, self).__init__(
  142. error=ProfilerErrors.ANALYSER_NOT_EXIST_ERROR,
  143. message=ProfilerErrorMsg.ANALYSER_NOT_EXIST_ERROR.value.format(msg),
  144. http_code=400
  145. )
  146. class ProfilerDeviceIdException(ProfilerException):
  147. """The parameter device_id error in profiler module."""
  148. def __init__(self, msg):
  149. super(ProfilerDeviceIdException, self).__init__(
  150. error=ProfilerErrors.DEVICE_ID_ERROR,
  151. message=ProfilerErrorMsg.DEIVICE_ID_ERROR.value.format(msg),
  152. http_code=400
  153. )
  154. class ProfilerOpTypeException(ProfilerException):
  155. """The parameter op_type error in profiler module."""
  156. def __init__(self, msg):
  157. super(ProfilerOpTypeException, self).__init__(
  158. error=ProfilerErrors.OP_TYPE_ERROR,
  159. message=ProfilerErrorMsg.OP_TYPE_ERROR.value.format(msg),
  160. http_code=400
  161. )
  162. class ProfilerSortConditionException(ProfilerException):
  163. """The parameter sort_condition error in profiler module."""
  164. def __init__(self, msg):
  165. super(ProfilerSortConditionException, self).__init__(
  166. error=ProfilerErrors.SORT_CONDITION_ERROR,
  167. message=ProfilerErrorMsg.SORT_CONDITION_ERROR.value.format(msg),
  168. http_code=400
  169. )
  170. class ProfilerFilterConditionException(ProfilerException):
  171. """The parameter filer_condition error in profiler module."""
  172. def __init__(self, msg):
  173. super(ProfilerFilterConditionException, self).__init__(
  174. error=ProfilerErrors.FILTER_CONDITION_ERROR,
  175. message=ProfilerErrorMsg.FILTER_CONDITION_ERROR.value.format(msg),
  176. http_code=400
  177. )
  178. class ProfilerGroupConditionException(ProfilerException):
  179. """The parameter group_condition error in profiler module."""
  180. def __init__(self, msg):
  181. super(ProfilerGroupConditionException, self).__init__(
  182. error=ProfilerErrors.GROUP_CONDITION_ERROR,
  183. message=ProfilerErrorMsg.GROUP_CONDITION_ERROR.value.format(msg),
  184. http_code=400
  185. )
  186. class ProfilerColumnNotSupportSortException(ProfilerException):
  187. """The column does not support to sort error in profiler module."""
  188. def __init__(self, msg):
  189. super(ProfilerColumnNotSupportSortException, self).__init__(
  190. error=ProfilerErrors.COLUMN_NOT_SUPPORT_SORT_ERROR,
  191. message=ProfilerErrorMsg.COLUMN_NOT_SUPPORT_SORT_ERROR.value.format(msg),
  192. http_code=400
  193. )
  194. class StepNumNotSupportedException(ProfilerException):
  195. """The step number error in profiler module."""
  196. def __init__(self, msg):
  197. super(StepNumNotSupportedException, self).__init__(
  198. error=ProfilerErrors.STEP_NUM_NOT_SUPPORTED_ERROR,
  199. message=ProfilerErrorMsg.STEP_NUM_NOT_SUPPORTED_ERROR.value.format(msg),
  200. http_code=400
  201. )
  202. class JobIdMismatchException(ProfilerException):
  203. """The Job ID mismatch error in profiler module."""
  204. def __init__(self):
  205. super(JobIdMismatchException, self).__init__(
  206. error=ProfilerErrors.JOB_ID_MISMATCH_ERROR,
  207. message=ProfilerErrorMsg.JOB_ID_MISMATCH_ERROR.value,
  208. http_code=400
  209. )
  210. class ProfilerPipelineOpNotExistException(ProfilerException):
  211. """The minddata pipeline operator does not exist error in profiler module."""
  212. def __init__(self, msg):
  213. super(ProfilerPipelineOpNotExistException, self).__init__(
  214. error=ProfilerErrors.PIPELINE_OP_NOT_EXIST_ERROR,
  215. message=ProfilerErrorMsg.PIPELINE_OP_NOT_EXIST_ERROR.value.format(msg),
  216. http_code=400
  217. )