• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Huawei Device Co., Ltd.
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 
16 #include "aip_napi_error.h"
17 
18 #include <optional>
19 
20 #include "aip_log.h"
21 
22 #undef LOG_TAG
23 #define LOG_TAG "AipNapiError"
24 
25 namespace OHOS {
26 namespace DataIntelligence {
CreateIntelligenceError(const napi_env & env,int32_t errorCode,const std::string & errorMsg)27 napi_value CreateIntelligenceError(const napi_env &env, int32_t errorCode, const std::string &errorMsg)
28 {
29     napi_value businessError = nullptr;
30     napi_value code = nullptr;
31     napi_value msg = nullptr;
32     NAPI_CALL(env, napi_create_int32(env, errorCode, &code));
33     NAPI_CALL(env, napi_create_string_utf8(env, errorMsg.c_str(), NAPI_AUTO_LENGTH, &msg));
34     napi_create_error(env, nullptr, msg, &businessError);
35     napi_set_named_property(env, businessError, "code", code);
36     return businessError;
37 }
38 
GetIntelligenceErrMsg(int32_t errorCode)39 std::optional<std::string> GetIntelligenceErrMsg(int32_t errorCode)
40 {
41     auto iter = ERROR_MESSAGES.find(errorCode);
42     if (iter != ERROR_MESSAGES.end()) {
43         return iter->second;
44     }
45     AIP_HILOGE("Error, messages not found");
46     return std::nullopt;
47 }
48 
ThrowIntelligenceErr(const napi_env & env,int32_t errorCode,const std::string & printMsg)49 void ThrowIntelligenceErr(const napi_env &env, int32_t errorCode, const std::string &printMsg)
50 {
51     AIP_HILOGE("printMsg:%{public}s, errorCode:%{public}d", printMsg.c_str(), errorCode);
52     std::optional<std::string> msg = GetIntelligenceErrMsg(errorCode);
53     if (!msg) {
54         AIP_HILOGE("errorCode:%{public}d is invalid", errorCode);
55         return;
56     }
57     napi_value error = CreateIntelligenceError(env, errorCode, msg.value());
58     napi_throw(env, error);
59 }
60 
ThrowIntelligenceErrByPromise(const napi_env & env,int32_t errorCode,const std::string & printMsg,napi_value & value)61 void ThrowIntelligenceErrByPromise(const napi_env &env, int32_t errorCode, const std::string &printMsg,
62     napi_value &value)
63 {
64     AIP_HILOGE("printMsg:%{public}s, errorCode:%{public}d", printMsg.c_str(), errorCode);
65     std::optional<std::string> msg = GetIntelligenceErrMsg(errorCode);
66     if (!msg) {
67         AIP_HILOGE("errorCode:%{public}d is invalid", errorCode);
68         return;
69     }
70     value = CreateIntelligenceError(env, errorCode, msg.value());
71 }
72 } // namespace DataIntelligence
73 } // namespae OHOS