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