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
16 #include "napi_error_utils.h"
17
18 #include <algorithm>
19
20 namespace OHOS {
21 namespace UDMF {
22 using NapiErrorCode = OHOS::UDMF::NapiErrorCode;
23
24 static const NapiErrorCode JS_ERROR_CODE_MSGS[] = {
25 { Status::E_NO_PERMISSION, 201, "Permission denied!" },
26 { Status::E_INVALID_PARAMETERS, 401, "Parameter error." },
27 };
28
GetErrorCode(int32_t errorCode)29 const std::optional<NapiErrorCode> GetErrorCode(int32_t errorCode)
30 {
31 auto napiErrorCode = NapiErrorCode{ errorCode, -1, "" };
32 auto iter = std::lower_bound(JS_ERROR_CODE_MSGS,
33 JS_ERROR_CODE_MSGS + sizeof(JS_ERROR_CODE_MSGS) / sizeof(JS_ERROR_CODE_MSGS[0]), napiErrorCode,
34 [](const NapiErrorCode &napiErrorCode1, const NapiErrorCode &napiErrorCode2) {
35 return napiErrorCode1.status < napiErrorCode2.status;
36 });
37 if (iter < JS_ERROR_CODE_MSGS + sizeof(JS_ERROR_CODE_MSGS) / sizeof(JS_ERROR_CODE_MSGS[0]) &&
38 iter->status == errorCode) {
39 return *iter;
40 }
41 return std::nullopt;
42 }
43
GenerateNapiError(Status error,int32_t & errCode,std::string & errMessage)44 Status GenerateNapiError(Status error, int32_t &errCode, std::string &errMessage)
45 {
46 auto errormsg = GetErrorCode(error);
47 if (errormsg.has_value()) {
48 auto napiError = errormsg.value();
49 errCode = napiError.jsCode;
50 errMessage = napiError.message;
51 } else {
52 // unmatched status return unified error code
53 errCode = -1;
54 errMessage = "";
55 }
56 LOG_DEBUG(UDMF_KITS_NAPI, "GenerateNapiError errCode is %{public}d", errCode);
57 if (errCode == 0) {
58 return Status::E_OK;
59 }
60 return error;
61 }
62
ThrowNapiError(napi_env env,int32_t status,const std::string & errMessage,bool isParamsCheck)63 void ThrowNapiError(napi_env env, int32_t status, const std::string &errMessage, bool isParamsCheck)
64 {
65 LOG_INFO(UDMF_KITS_NAPI, "ThrowNapiError message: %{public}s", errMessage.c_str());
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 napi_throw_error(env, jsCode.c_str(), message.c_str());
92 }
93 } // namespace UDMF
94 } // namespace OHOS
95