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, "Settings already exist." },
26 { Status::E_NO_SYSTEM_PERMISSION, 202,
27 "Permission denied, application which is not a system application uses system API." },
28 };
29
GetErrorCode(int32_t errorCode)30 const std::optional<NapiErrorCode> GetErrorCode(int32_t errorCode)
31 {
32 auto napiErrorCode = NapiErrorCode{ errorCode, -1, "" };
33 auto iter = std::lower_bound(JS_ERROR_CODE_MSGS,
34 JS_ERROR_CODE_MSGS + sizeof(JS_ERROR_CODE_MSGS) / sizeof(JS_ERROR_CODE_MSGS[0]), napiErrorCode,
35 [](const NapiErrorCode &napiErrorCode1, const NapiErrorCode &napiErrorCode2) {
36 return napiErrorCode1.status < napiErrorCode2.status;
37 });
38 if (iter < JS_ERROR_CODE_MSGS + sizeof(JS_ERROR_CODE_MSGS) / sizeof(JS_ERROR_CODE_MSGS[0]) &&
39 iter->status == errorCode) {
40 return *iter;
41 }
42 return std::nullopt;
43 }
44
GenerateNapiError(Status error,int32_t & errCode,std::string & errMessage)45 Status GenerateNapiError(Status error, int32_t &errCode, std::string &errMessage)
46 {
47 auto errormsg = GetErrorCode(error);
48 if (errormsg.has_value()) {
49 auto napiError = errormsg.value();
50 errCode = napiError.jsCode;
51 errMessage = napiError.message;
52 } else {
53 // unmatched status return unified error code
54 errCode = -1;
55 errMessage = "";
56 }
57 LOG_DEBUG(UDMF_KITS_NAPI, "GenerateNapiError errCode is %{public}d", errCode);
58 if (errCode == 0) {
59 return Status::E_OK;
60 }
61 return error;
62 }
63
ThrowNapiError(napi_env env,int32_t status,const std::string & errMessage,bool isParamsCheck)64 void ThrowNapiError(napi_env env, int32_t status, const std::string &errMessage, bool isParamsCheck)
65 {
66 if (status == Status::E_OK) {
67 return;
68 }
69 auto errorMsg = GetErrorCode(status);
70 NapiErrorCode napiError;
71 if (errorMsg.has_value()) {
72 napiError = errorMsg.value();
73 } else {
74 napiError.jsCode = -1;
75 napiError.message = "";
76 }
77
78 std::string message(napiError.message);
79 if (isParamsCheck) {
80 auto paramsCheckError = 401;
81 napiError.jsCode = paramsCheckError;
82 message += errMessage;
83 }
84
85 std::string jsCode;
86 if (napiError.jsCode == -1) {
87 jsCode = "";
88 } else {
89 jsCode = std::to_string(napiError.jsCode);
90 }
91 LOG_INFO(UDMF_KITS_NAPI, "ThrowNapiError, status:%{public}d, jsCode: %{public}s, message: %{public}s",
92 status, jsCode.c_str(), errMessage.c_str());
93 napi_throw_error(env, jsCode.c_str(), message.c_str());
94 }
95 } // namespace UDMF
96 } // namespace OHOS
97