• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2023 Huawei Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef MINDSPORE_CCSRC_INCLUDE_COMMON_UTILS_PYTHON_UTILS_H_
18 #define MINDSPORE_CCSRC_INCLUDE_COMMON_UTILS_PYTHON_UTILS_H_
19 
20 #include <functional>
21 #include <string>
22 #include "utils/info.h"
23 
24 namespace mindspore {
25 inline void HandleExceptionRethrow(const std::function<void(void)> &main_func,
26                                    const std::function<void(void)> &already_set_error_handler,
27                                    const std::function<void(void)> &other_error_handler,
28                                    const std::function<void(void)> &default_error_handler,
29                                    const DebugInfoPtr &debug_info = nullptr, bool force_rethrow = false) {
30   try {
31     if (!main_func) {
32       MS_LOG(ERROR) << "The 'main_func' should not be empty.";
33       return;
34     }
35     main_func();
catch(const py::error_already_set & ex)36   } catch (const py::error_already_set &ex) {
37     MS_LOG(INFO) << "Caught exception: " << ex.what();
38     if (already_set_error_handler) {
39       already_set_error_handler();
40     }
41     if (force_rethrow) {
42       std::rethrow_exception(std::current_exception());
43     }
44 
45     // Re-throw this exception to Python interpreter to handle it
46     throw(py::error_already_set(ex));
47   } catch (const py::type_error &ex) {
48     MS_LOG(INFO) << "Caught exception: " << ex.what();
49     if (other_error_handler) {
50       other_error_handler();
51     }
52     if (force_rethrow) {
53       std::rethrow_exception(std::current_exception());
54     }
55 
56     if (debug_info == nullptr) {
57       throw py::type_error(ex);
58     } else {
59       std::stringstream ss;
60       ss << ex.what() << ".\n\n" << trace::GetDebugInfoStr(debug_info);
61       throw py::type_error(ss.str());
62     }
catch(const py::value_error & ex)63   } catch (const py::value_error &ex) {
64     MS_LOG(INFO) << "Caught exception: " << ex.what();
65     if (other_error_handler) {
66       other_error_handler();
67     }
68     if (force_rethrow) {
69       std::rethrow_exception(std::current_exception());
70     }
71 
72     if (debug_info == nullptr) {
73       throw py::value_error(ex);
74     } else {
75       std::stringstream ss;
76       ss << ex.what() << ".\n\n" << trace::GetDebugInfoStr(debug_info);
77       throw py::value_error(ss.str());
78     }
79   } catch (const py::index_error &ex) {
80     MS_LOG(INFO) << "Caught exception: " << ex.what();
81     if (other_error_handler) {
82       other_error_handler();
83     }
84     if (force_rethrow) {
85       std::rethrow_exception(std::current_exception());
86     }
87 
88     if (debug_info == nullptr) {
89       throw py::index_error(ex);
90     } else {
91       std::stringstream ss;
92       ss << ex.what() << ".\n\n" << trace::GetDebugInfoStr(debug_info);
93       throw py::index_error(ss.str());
94     }
catch(const py::key_error & ex)95   } catch (const py::key_error &ex) {
96     MS_LOG(INFO) << "Caught exception: " << ex.what();
97     if (other_error_handler) {
98       other_error_handler();
99     }
100     if (force_rethrow) {
101       std::rethrow_exception(std::current_exception());
102     }
103 
104     if (debug_info == nullptr) {
105       throw py::key_error(ex);
106     } else {
107       std::stringstream ss;
108       ss << ex.what() << ".\n\n" << trace::GetDebugInfoStr(debug_info);
109       throw py::key_error(ss.str());
110     }
111   } catch (const py::attribute_error &ex) {
112     MS_LOG(INFO) << "Caught exception: " << ex.what();
113     if (other_error_handler) {
114       other_error_handler();
115     }
116     if (force_rethrow) {
117       std::rethrow_exception(std::current_exception());
118     }
119 
120     if (debug_info == nullptr) {
121       throw py::attribute_error(ex);
122     } else {
123       std::stringstream ss;
124       ss << ex.what() << ".\n\n" << trace::GetDebugInfoStr(debug_info);
125       throw py::attribute_error(ss.str());
126     }
catch(const py::name_error & ex)127   } catch (const py::name_error &ex) {
128     MS_LOG(INFO) << "Caught exception: " << ex.what();
129     if (other_error_handler) {
130       other_error_handler();
131     }
132     if (force_rethrow) {
133       std::rethrow_exception(std::current_exception());
134     }
135 
136     if (debug_info == nullptr) {
137       throw py::name_error(ex);
138     } else {
139       std::stringstream ss;
140       ss << ex.what() << ".\n\n" << trace::GetDebugInfoStr(debug_info);
141       throw py::name_error(ss.str());
142     }
143   } catch (const py::assertion_error &ex) {
144     MS_LOG(INFO) << "Caught exception: " << ex.what();
145     if (other_error_handler) {
146       other_error_handler();
147     }
148     if (force_rethrow) {
149       std::rethrow_exception(std::current_exception());
150     }
151 
152     if (debug_info == nullptr) {
153       throw py::assertion_error(ex);
154     } else {
155       std::stringstream ss;
156       ss << ex.what() << ".\n\n" << trace::GetDebugInfoStr(debug_info);
157       throw py::assertion_error(ss.str());
158     }
catch(const py::base_exception & ex)159   } catch (const py::base_exception &ex) {
160     MS_LOG(INFO) << "Caught exception: " << ex.what();
161     if (other_error_handler) {
162       other_error_handler();
163     }
164     if (force_rethrow) {
165       std::rethrow_exception(std::current_exception());
166     }
167 
168     if (debug_info == nullptr) {
169       throw py::base_exception(ex);
170     } else {
171       std::stringstream ss;
172       ss << ex.what() << ".\n\n" << trace::GetDebugInfoStr(debug_info);
173       throw py::base_exception(ss.str());
174     }
175   } catch (const py::keyboard_interrupt &ex) {
176     MS_LOG(INFO) << "Caught exception: " << ex.what();
177     if (other_error_handler) {
178       other_error_handler();
179     }
180     if (force_rethrow) {
181       std::rethrow_exception(std::current_exception());
182     }
183 
184     if (debug_info == nullptr) {
185       throw py::keyboard_interrupt(ex);
186     } else {
187       std::stringstream ss;
188       ss << ex.what() << ".\n\n" << trace::GetDebugInfoStr(debug_info);
189       throw py::keyboard_interrupt(ss.str());
190     }
catch(const py::stop_iteration & ex)191   } catch (const py::stop_iteration &ex) {
192     MS_LOG(INFO) << "Caught exception: " << ex.what();
193     if (other_error_handler) {
194       other_error_handler();
195     }
196     if (force_rethrow) {
197       std::rethrow_exception(std::current_exception());
198     }
199 
200     if (debug_info == nullptr) {
201       throw py::stop_iteration(ex);
202     } else {
203       std::stringstream ss;
204       ss << ex.what() << ".\n\n" << trace::GetDebugInfoStr(debug_info);
205       throw py::stop_iteration(ss.str());
206     }
207   } catch (const py::overflow_error &ex) {
208     MS_LOG(INFO) << "Caught exception: " << ex.what();
209     if (other_error_handler) {
210       other_error_handler();
211     }
212     if (force_rethrow) {
213       std::rethrow_exception(std::current_exception());
214     }
215 
216     if (debug_info == nullptr) {
217       throw py::overflow_error(ex);
218     } else {
219       std::stringstream ss;
220       ss << ex.what() << ".\n\n" << trace::GetDebugInfoStr(debug_info);
221       throw py::overflow_error(ss.str());
222     }
catch(const py::zero_division_error & ex)223   } catch (const py::zero_division_error &ex) {
224     MS_LOG(INFO) << "Caught exception: " << ex.what();
225     if (other_error_handler) {
226       other_error_handler();
227     }
228     if (force_rethrow) {
229       std::rethrow_exception(std::current_exception());
230     }
231 
232     if (debug_info == nullptr) {
233       throw py::zero_division_error(ex);
234     } else {
235       std::stringstream ss;
236       ss << ex.what() << ".\n\n" << trace::GetDebugInfoStr(debug_info);
237       throw py::zero_division_error(ss.str());
238     }
239   } catch (const py::environment_error &ex) {
240     MS_LOG(INFO) << "Caught exception: " << ex.what();
241     if (other_error_handler) {
242       other_error_handler();
243     }
244     if (force_rethrow) {
245       std::rethrow_exception(std::current_exception());
246     }
247 
248     if (debug_info == nullptr) {
249       throw py::environment_error(ex);
250     } else {
251       std::stringstream ss;
252       ss << ex.what() << ".\n\n" << trace::GetDebugInfoStr(debug_info);
253       throw py::environment_error(ss.str());
254     }
catch(const py::io_error & ex)255   } catch (const py::io_error &ex) {
256     MS_LOG(INFO) << "Caught exception: " << ex.what();
257     if (other_error_handler) {
258       other_error_handler();
259     }
260     if (force_rethrow) {
261       std::rethrow_exception(std::current_exception());
262     }
263 
264     if (debug_info == nullptr) {
265       throw py::io_error(ex);
266     } else {
267       std::stringstream ss;
268       ss << ex.what() << ".\n\n" << trace::GetDebugInfoStr(debug_info);
269       throw py::io_error(ss.str());
270     }
271   } catch (const py::os_error &ex) {
272     MS_LOG(INFO) << "Caught exception: " << ex.what();
273     if (other_error_handler) {
274       other_error_handler();
275     }
276     if (force_rethrow) {
277       std::rethrow_exception(std::current_exception());
278     }
279 
280     if (debug_info == nullptr) {
281       throw py::os_error(ex);
282     } else {
283       std::stringstream ss;
284       ss << ex.what() << ".\n\n" << trace::GetDebugInfoStr(debug_info);
285       throw py::os_error(ss.str());
286     }
catch(const py::memory_error & ex)287   } catch (const py::memory_error &ex) {
288     MS_LOG(INFO) << "Caught exception: " << ex.what();
289     if (other_error_handler) {
290       other_error_handler();
291     }
292     if (force_rethrow) {
293       std::rethrow_exception(std::current_exception());
294     }
295 
296     if (debug_info == nullptr) {
297       throw py::memory_error(ex);
298     } else {
299       std::stringstream ss;
300       ss << ex.what() << ".\n\n" << trace::GetDebugInfoStr(debug_info);
301       throw py::memory_error(ss.str());
302     }
303   } catch (const py::unbound_local_error &ex) {
304     MS_LOG(INFO) << "Caught exception: " << ex.what();
305     if (other_error_handler) {
306       other_error_handler();
307     }
308     if (force_rethrow) {
309       std::rethrow_exception(std::current_exception());
310     }
311 
312     if (debug_info == nullptr) {
313       throw py::unbound_local_error(ex);
314     } else {
315       std::stringstream ss;
316       ss << ex.what() << ".\n\n" << trace::GetDebugInfoStr(debug_info);
317       throw py::unbound_local_error(ss.str());
318     }
catch(const py::not_implemented_error & ex)319   } catch (const py::not_implemented_error &ex) {
320     MS_LOG(INFO) << "Caught exception: " << ex.what();
321     if (other_error_handler) {
322       other_error_handler();
323     }
324     if (force_rethrow) {
325       std::rethrow_exception(std::current_exception());
326     }
327 
328     if (debug_info == nullptr) {
329       throw py::not_implemented_error(ex);
330     } else {
331       std::stringstream ss;
332       ss << ex.what() << ".\n\n" << trace::GetDebugInfoStr(debug_info);
333       throw py::not_implemented_error(ss.str());
334     }
335   } catch (const py::indentation_error &ex) {
336     MS_LOG(INFO) << "Caught exception: " << ex.what();
337     if (other_error_handler) {
338       other_error_handler();
339     }
340     if (force_rethrow) {
341       std::rethrow_exception(std::current_exception());
342     }
343 
344     if (debug_info == nullptr) {
345       throw py::indentation_error(ex);
346     } else {
347       std::stringstream ss;
348       ss << ex.what() << ".\n\n" << trace::GetDebugInfoStr(debug_info);
349       throw py::indentation_error(ss.str());
350     }
catch(const py::runtime_warning & ex)351   } catch (const py::runtime_warning &ex) {
352     MS_LOG(INFO) << "Caught exception: " << ex.what();
353     if (other_error_handler) {
354       other_error_handler();
355     }
356     if (force_rethrow) {
357       std::rethrow_exception(std::current_exception());
358     }
359 
360     if (debug_info == nullptr) {
361       throw py::runtime_warning(ex);
362     } else {
363       std::stringstream ss;
364       ss << ex.what() << ".\n\n" << trace::GetDebugInfoStr(debug_info);
365       throw py::runtime_warning(ss.str());
366     }
367   } catch (const std::exception &ex) {
368     MS_LOG(INFO) << "Caught exception: " << ex.what();
369     if (other_error_handler) {
370       other_error_handler();
371     }
372     if (force_rethrow) {
373       std::rethrow_exception(std::current_exception());
374     }
375 
376     // Re-throw this exception to Python interpreter to handle it.
377     if (debug_info == nullptr) {
378       throw std::runtime_error(ex.what());
379     } else {
380       std::stringstream ss;
381       ss << ex.what() << ".\n\n" << trace::GetDebugInfoStr(debug_info);
382       throw std::runtime_error(ss.str());
383     }
catch(...)384   } catch (...) {
385     if (default_error_handler) {
386       default_error_handler();
387     }
388     if (force_rethrow) {
389       std::rethrow_exception(std::current_exception());
390     }
391 
392 #ifndef _MSC_VER
393     auto exception_type = abi::__cxa_current_exception_type();
394     MS_EXCEPTION_IF_NULL(exception_type);
395     std::string ex_name(exception_type->name());
396     MS_LOG(EXCEPTION) << "Error occurred. Exception name: " << ex_name;
397 #else
398     MS_LOG(EXCEPTION) << "Error occurred.";
399 #endif
400   }
401 }
402 }  // namespace mindspore
403 #endif  // MINDSPORE_CCSRC_INCLUDE_COMMON_UTILS_PYTHON_UTILS_H_
404