1 /*
2  * Copyright (c) 2022 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 "util_napi_error.h"
17 
18 #undef LOG_TAG
19 #define LOG_TAG "UtilNapiError"
20 
21 namespace OHOS {
22 namespace Msdp {
23 namespace DeviceStatus {
24 namespace UtilNapiError {
25 
GetErrorMsg(int32_t code,std::string & codeMsg)26 bool GetErrorMsg(int32_t code, std::string &codeMsg)
27 {
28     auto iter = NAPI_ERRORS.find(code);
29     if (iter == NAPI_ERRORS.end()) {
30         FI_HILOGE("Error code %{public}d not found", code);
31         return false;
32     }
33     codeMsg = iter->second;
34     return true;
35 }
36 
HandleExecuteResult(napi_env env,int32_t errCode,const std::string param1,const std::string param2)37 void HandleExecuteResult(napi_env env, int32_t errCode, const std::string param1, const std::string param2)
38 {
39     switch (errCode) {
40         case COMMON_PERMISSION_CHECK_ERROR: {
41             THROWERR(env, errCode, param1.c_str(), param2.c_str());
42             break;
43         }
44         case COMMON_PARAMETER_ERROR: {
45             THROWERR_CUSTOM(env, errCode, "param is invalid");
46             break;
47         }
48         case COMMON_NOT_SYSTEM_APP: {
49             std::string errMsg;
50             if (!UtilNapiError::GetErrorMsg(errCode, errMsg)) {
51                 FI_HILOGE("GetErrorMsg failed");
52                 return;
53             }
54             THROWERR_CUSTOM(env, errCode, errMsg.c_str());
55             break;
56         }
57         case COMMON_NOT_ALLOWED_DISTRIBUTED: {
58             std::string errMsg;
59             if (!UtilNapiError::GetErrorMsg(errCode, errMsg)) {
60                 FI_HILOGE("GetErrorMsg failed");
61                 return;
62             }
63             THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, errMsg.c_str());
64             break;
65         }
66         default: {
67             FI_HILOGW("This error code does not require a synchronous exception throw");
68             break;
69         }
70     }
71 }
72 } // namespace UtilNapiError
73 } // namespace DeviceStatus
74 } // namespace Msdp
75 } // namespace OHOS