1 /*
2 * Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development 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 "nfc_napi_ctrl_utils.h"
17 #include <cstring>
18 #include "loghelper.h"
19 #include "nfc_sdk_common.h"
20 #include "securec.h"
21
22 namespace OHOS {
23 namespace NFC {
24 namespace KITS {
25
CreateUndefined(napi_env env)26 napi_value CreateUndefined(napi_env env)
27 {
28 napi_value result = nullptr;
29 NAPI_CALL(env, napi_get_undefined(env, &result));
30 return result;
31 }
32
BuildErrorMessage(int errCode,std::string funcName,std::string forbiddenPerm,std::string paramName,std::string expertedType)33 std::string BuildErrorMessage(int errCode, std::string funcName, std::string forbiddenPerm,
34 std::string paramName, std::string expertedType)
35 {
36 std::string errMsg;
37 if (errCode == BUSI_ERR_PERM) {
38 return errMsg.append("Permission denied. An attempt was made to ${")
39 .append(funcName)
40 .append("} forbidden by permission: ${")
41 .append(forbiddenPerm)
42 .append("}.");
43 } else if (errCode == BUSI_ERR_PARAM) {
44 if (paramName.length() > 0) {
45 return errMsg.append("Parameter error. The type of \"${")
46 .append(paramName)
47 .append("}\" must be ${")
48 .append(expertedType)
49 .append("}.");
50 } else {
51 return "Parameter error. The parameter number is invalid.";
52 }
53 } else if (errCode == BUSI_ERR_NFC_STATE_INVALID) {
54 return "NFC state is abnormal in service.";
55 }
56 return "Unknown error message";
57 }
58
GenerateBusinessError(const napi_env & env,int errCode,const std::string & errMessage)59 napi_value GenerateBusinessError(const napi_env &env, int errCode, const std::string &errMessage)
60 {
61 napi_value code = nullptr;
62 napi_create_uint32(env, errCode, &code);
63 napi_value message = nullptr;
64 napi_create_string_utf8(env, errMessage.c_str(), NAPI_AUTO_LENGTH, &message);
65 napi_value businessError = nullptr;
66 napi_create_error(env, nullptr, message, &businessError);
67 napi_set_named_property(env, businessError, KEY_CODE.c_str(), code);
68 return businessError;
69 }
70
CheckNfcStatusCodeAndThrow(const napi_env & env,int statusCode,const std::string funcName)71 bool CheckNfcStatusCodeAndThrow(const napi_env &env, int statusCode, const std::string funcName)
72 {
73 if (statusCode == BUSI_ERR_PERM) {
74 napi_throw(env, GenerateBusinessError(env, BUSI_ERR_PERM,
75 BuildErrorMessage(BUSI_ERR_PERM, funcName, NFC_PERM_DESC, "", "")));
76 return false;
77 } else if (statusCode >= ErrorCode::ERR_NFC_BASE && statusCode < ErrorCode::ERR_TAG_BASE) {
78 napi_throw(env, GenerateBusinessError(env, BUSI_ERR_NFC_STATE_INVALID,
79 BuildErrorMessage(BUSI_ERR_NFC_STATE_INVALID, "", "", "", "")));
80 return false;
81 }
82 return true;
83 }
84
85 } // namespace KITS
86 } // namespace NFC
87 } // namespace OHOS
88