Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #include <exception>
12 : #include "exception_handler.h"
13 : #include "network_api_exception.h"
14 :
15 : namespace hccl {
16 : using namespace std;
17 :
18 13 : HcclResult ExceptionHandler::HandleException(const char* functionName)
19 : {
20 : try {
21 : // 重新抛出当前捕获的异常
22 13 : throw;
23 13 : } catch (const HcclException& e) {
24 0 : HCCL_ERROR("%s: HcclException, what: %s, code: %d", functionName, e.what(), e.code());
25 0 : return e.code();
26 0 : } catch (const Hccl::NetworkApiException& e) {
27 0 : HCCL_ERROR("%s: NetworkApiException, what: %s", functionName, e.what());
28 0 : return HCCL_E_TCP_CONNECT;
29 0 : } catch (const out_of_range& e) {
30 0 : HCCL_ERROR("%s: Out of range error, what: %s", functionName, e.what());
31 0 : return HCCL_E_NOT_FOUND;
32 0 : } catch (const runtime_error& e) {
33 0 : HCCL_ERROR("%s: Runtime error, what: %s", functionName, e.what());
34 0 : return HCCL_E_RUNTIME;
35 0 : } catch (const logic_error& e) {
36 0 : HCCL_ERROR("%s: Logic error, what: %s", functionName, e.what());
37 0 : return HCCL_E_INTERNAL;
38 13 : } catch (const exception& e) {
39 13 : HCCL_ERROR("%s: Standard exception, what: %s", functionName, e.what());
40 13 : return HCCL_E_INTERNAL;
41 13 : } catch (...) {
42 0 : HCCL_ERROR("%s: Unknown exception", functionName);
43 0 : return HCCL_E_INTERNAL;
44 0 : }
45 : }
46 :
47 0 : void ExceptionHandler::ThrowIfErrorCode(
48 : HcclResult errorCode, const string& errString, const char* fileName, s32 lineNum, const char* functionName)
49 : {
50 0 : if (LIKELY(errorCode == HCCL_SUCCESS)) {
51 0 : return;
52 : }
53 :
54 0 : string prefix = string(fileName) + ":" + to_string(lineNum) + "," + string(functionName);
55 0 : string suffix = "ret=" + to_string(errorCode) + " " + errString;
56 :
57 0 : switch (errorCode) {
58 0 : case HCCL_E_NOT_FOUND:
59 0 : throw out_of_range(prefix + ", Error: Out of range, " + suffix);
60 : break;
61 0 : case HCCL_E_INTERNAL:
62 0 : throw logic_error(prefix + ", Error: Logic error, " + suffix);
63 : break;
64 0 : case HCCL_E_RUNTIME:
65 0 : throw runtime_error(prefix + ", Error: Runtime error, " + suffix);
66 : break;
67 0 : case HCCL_E_PARA:
68 0 : throw HcclException(errorCode, prefix + ", Error: hccl special error, " + suffix);
69 : break;
70 0 : default:
71 0 : throw runtime_error(prefix + ", Error: Default error code, " + suffix);
72 : break;
73 : }
74 0 : }
75 :
76 : } // namespace hccl
|