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 #include "napi_error.h"
16 #include <map>
17
18 namespace OHOS {
19 namespace Security {
20 namespace AccessToken {
21 static const std::map<uint32_t, std::string> g_errorStringMap = {
22 {JS_ERROR_PERMISSION_DENIED, "Permission denied."},
23 {JS_ERROR_NOT_SYSTEM_APP, "Not system app."},
24 {JS_ERROR_SYSTEM_CAPABILITY_NOT_SUPPORT, "Not support system capability."},
25 {JS_ERROR_START_ABILITY_FAIL, "Start grant ability failed."},
26 {JS_ERROR_BACKGROUND_FAIL, "Ui extension turn background failed."},
27 {JS_ERROR_TERMINATE_FAIL, "Ui extension terminate failed."},
28 {JS_ERROR_PARAM_INVALID, "Invalid parameter."},
29 {JS_ERROR_TOKENID_NOT_EXIST, "The specified token id does not exist."},
30 {JS_ERROR_PERMISSION_NOT_EXIST, "The specified permission does not exist."},
31 {JS_ERROR_NOT_USE_TOGETHER, "The API is not used in pair with others."},
32 {JS_ERROR_REGISTERS_EXCEED_LIMITATION, "The number of registered listeners exceeds limitation."},
33 {JS_ERROR_PERMISSION_OPERATION_NOT_ALLOWED, "The operation of specified permission is not allowed."},
34 {JS_ERROR_SERVICE_NOT_RUNNING, "The service is abnormal."},
35 {JS_ERROR_OUT_OF_MEMORY, "Out of memory."},
36 {JS_ERROR_INNER, "Common inner error."},
37 {JS_ERROR_REQUEST_IS_ALREADY_EXIST, "The request already exists."},
38 {JS_ERROR_ALL_PERM_GRANTED, "All permissions in the permission list have been granted."},
39 {JS_ERROR_PERM_REVOKE_BY_USER,
40 "The permission list contains the permission that has not been revoked by the user."},
41 {JS_ERROR_GLOBAL_SWITCH_IS_ALREADY_OPEN, "The specific global switch is already open."},
42 };
43
GetParamErrorMsg(const std::string & param,const std::string & type)44 std::string GetParamErrorMsg(const std::string& param, const std::string& type)
45 {
46 std::string msg = "Parameter Error. The type of \"" + param + "\" must be " + type + ".";
47 return msg;
48 }
49
GetErrorMessage(uint32_t errCode)50 std::string GetErrorMessage(uint32_t errCode)
51 {
52 auto iter = g_errorStringMap.find(errCode);
53 if (iter != g_errorStringMap.end()) {
54 return iter->second;
55 }
56 std::string errMsg = "Unknown error, errCode + " + std::to_string(errCode) + ".";
57 return errMsg;
58 }
59
GenerateBusinessError(napi_env env,int32_t errCode,const std::string & errMsg)60 napi_value GenerateBusinessError(napi_env env, int32_t errCode, const std::string& errMsg)
61 {
62 napi_value businessError = nullptr;
63
64 napi_value code = nullptr;
65 NAPI_CALL(env, napi_create_uint32(env, errCode, &code));
66
67 napi_value msg = nullptr;
68 NAPI_CALL(env, napi_create_string_utf8(env, errMsg.c_str(), NAPI_AUTO_LENGTH, &msg));
69
70 NAPI_CALL(env, napi_create_error(env, nullptr, msg, &businessError));
71 NAPI_CALL(env, napi_set_named_property(env, businessError, "code", code));
72 NAPI_CALL(env, napi_set_named_property(env, businessError, "message", msg));
73
74 return businessError;
75 }
76
GetNapiNull(napi_env env)77 napi_value GetNapiNull(napi_env env)
78 {
79 napi_value result = nullptr;
80 NAPI_CALL(env, napi_get_null(env, &result));
81 return result;
82 }
83 } // namespace AccessToken
84 } // namespace Security
85 } // namespace OHOS
86