• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 #define LOG_TAG "NapiErrorCode"
16 #include "napi_error_utils.h"
17 
18 namespace OHOS {
19 namespace UDMF {
20 using NapiErrorCode = OHOS::UDMF::NapiErrorCode;
21 
22 static constexpr NapiErrorCode JS_ERROR_CODE_MSGS[] = {
23     { Status::E_NO_PERMISSION, 201, "Permission denied!" },
24     { Status::E_INVALID_PARAMETERS, 401, "Parameter error." },
25     { Status::E_SETTINGS_EXISTED, 20400001,
26       "Settings already exist. To reconfigure, remove the existing sharing options." },
27     { Status::E_NO_SYSTEM_PERMISSION, 202,
28       "Permission denied, application which is not a system application uses system API." },
29 };
30 
GetErrorCode(int32_t errorCode)31 const std::optional<NapiErrorCode> GetErrorCode(int32_t errorCode)
32 {
33     auto napiErrorCode = NapiErrorCode{ errorCode, -1, "" };
34     auto iter = std::lower_bound(JS_ERROR_CODE_MSGS,
35         JS_ERROR_CODE_MSGS + sizeof(JS_ERROR_CODE_MSGS) / sizeof(JS_ERROR_CODE_MSGS[0]), napiErrorCode,
36         [](const NapiErrorCode &napiErrorCode1, const NapiErrorCode &napiErrorCode2) {
37             return napiErrorCode1.status < napiErrorCode2.status;
38         });
39     if (iter < JS_ERROR_CODE_MSGS + sizeof(JS_ERROR_CODE_MSGS) / sizeof(JS_ERROR_CODE_MSGS[0]) &&
40         iter->status == errorCode) {
41         return *iter;
42     }
43     return std::nullopt;
44 }
45 
GenerateNapiError(Status error,int32_t & errCode,std::string & errMessage)46 Status GenerateNapiError(Status error, int32_t &errCode, std::string &errMessage)
47 {
48     auto errormsg = GetErrorCode(error);
49     if (errormsg.has_value()) {
50         auto napiError = errormsg.value();
51         errCode = napiError.jsCode;
52         errMessage = napiError.message;
53     } else {
54         // unmatched status return unified error code
55         errCode = -1;
56         errMessage = "";
57     }
58     LOG_DEBUG(UDMF_KITS_NAPI, "GenerateNapiError errCode is %{public}d", errCode);
59     if (errCode == 0) {
60         return Status::E_OK;
61     }
62     return error;
63 }
64 
ThrowNapiError(napi_env env,int32_t status,const std::string & errMessage,bool isParamsCheck)65 void ThrowNapiError(napi_env env, int32_t status, const std::string &errMessage, bool isParamsCheck)
66 {
67     if (status == Status::E_OK) {
68         return;
69     }
70     auto errorMsg = GetErrorCode(status);
71     NapiErrorCode napiError;
72     if (errorMsg.has_value()) {
73         napiError = errorMsg.value();
74     } else {
75         napiError.jsCode = -1;
76         napiError.message = "";
77     }
78 
79     std::string message(napiError.message);
80     if (isParamsCheck) {
81         auto paramsCheckError = 401;
82         napiError.jsCode = paramsCheckError;
83         message += errMessage;
84     }
85 
86     std::string jsCode;
87     if (napiError.jsCode == -1) {
88         jsCode = "";
89     } else {
90         jsCode = std::to_string(napiError.jsCode);
91     }
92     LOG_INFO(UDMF_KITS_NAPI, "ThrowNapiError, status:%{public}d, jsCode: %{public}s, message: %{public}s",
93              status, jsCode.c_str(), errMessage.c_str());
94     napi_throw_error(env, jsCode.c_str(), message.c_str());
95 }
96 } // namespace UDMF
97 } // namespace OHOS
98