• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_PARAM_INVALID, "The parameter is invalid."},
26     {JS_ERROR_TOKENID_NOT_EXIST, "The specified token id does not exist."},
27     {JS_ERROR_PERMISSION_NOT_EXIST, "The specified permission does not exist."},
28     {JS_ERROR_NOT_USE_TOGETHER, "The listener is not used together."},
29     {JS_ERROR_REGISTERS_EXCEED_LIMITATION, "The number of registered listeners exceeds limitation."},
30     {JS_ERROR_PERMISSION_OPERATION_NOT_ALLOWED, "The operation of specified permission is not allowed."},
31     {JS_ERROR_SERVICE_NOT_RUNNING, "The service not running."},
32     {JS_ERROR_OUT_OF_MEMORY, "Out of memory."},
33     {JS_ERROR_INNER, "Common inner error."},
34 };
35 
GetParamErrorMsg(const std::string & param,const std::string & type)36 std::string GetParamErrorMsg(const std::string& param, const std::string& type)
37 {
38     std::string msg = "Parameter Error. The type of \"" + param + "\" must be " + type + ".";
39     return msg;
40 }
41 
GetErrorMessage(uint32_t errCode)42 std::string GetErrorMessage(uint32_t errCode)
43 {
44     auto iter = g_errorStringMap.find(errCode);
45     if (iter != g_errorStringMap.end()) {
46         return iter->second;
47     }
48     std::string errMsg = "Unknown error, errCode + " + std::to_string(errCode) + ".";
49     return errMsg;
50 }
51 
GenerateBusinessError(napi_env env,int32_t errCode,const std::string & errMsg)52 napi_value GenerateBusinessError(napi_env env, int32_t errCode, const std::string& errMsg)
53 {
54     napi_value businessError = nullptr;
55 
56     napi_value code = nullptr;
57     NAPI_CALL(env, napi_create_uint32(env, errCode, &code));
58 
59     napi_value msg = nullptr;
60     NAPI_CALL(env, napi_create_string_utf8(env, errMsg.c_str(), NAPI_AUTO_LENGTH, &msg));
61 
62     NAPI_CALL(env, napi_create_error(env, nullptr, msg, &businessError));
63     NAPI_CALL(env, napi_set_named_property(env, businessError, "code", code));
64     NAPI_CALL(env, napi_set_named_property(env, businessError, "message", msg));
65 
66     return businessError;
67 }
68 
GetNapiNull(napi_env env)69 napi_value GetNapiNull(napi_env env)
70 {
71     napi_value result = nullptr;
72     NAPI_CALL(env, napi_get_null(env, &result));
73     return result;
74 }
75 }  // namespace AccessToken
76 }  // namespace Security
77 }  // namespace OHOS
78