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