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