• 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_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, "The parameter is invalid."},
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 listener is not used together."},
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 not running."},
35     {JS_ERROR_OUT_OF_MEMORY, "Out of memory."},
36     {JS_ERROR_INNER, "Common inner error."},
37 };
38 
GetParamErrorMsg(const std::string & param,const std::string & type)39 std::string GetParamErrorMsg(const std::string& param, const std::string& type)
40 {
41     std::string msg = "Parameter Error. The type of \"" + param + "\" must be " + type + ".";
42     return msg;
43 }
44 
GetErrorMessage(uint32_t errCode)45 std::string GetErrorMessage(uint32_t errCode)
46 {
47     auto iter = g_errorStringMap.find(errCode);
48     if (iter != g_errorStringMap.end()) {
49         return iter->second;
50     }
51     std::string errMsg = "Unknown error, errCode + " + std::to_string(errCode) + ".";
52     return errMsg;
53 }
54 
GenerateBusinessError(napi_env env,int32_t errCode,const std::string & errMsg)55 napi_value GenerateBusinessError(napi_env env, int32_t errCode, const std::string& errMsg)
56 {
57     napi_value businessError = nullptr;
58 
59     napi_value code = nullptr;
60     NAPI_CALL(env, napi_create_uint32(env, errCode, &code));
61 
62     napi_value msg = nullptr;
63     NAPI_CALL(env, napi_create_string_utf8(env, errMsg.c_str(), NAPI_AUTO_LENGTH, &msg));
64 
65     NAPI_CALL(env, napi_create_error(env, nullptr, msg, &businessError));
66     NAPI_CALL(env, napi_set_named_property(env, businessError, "code", code));
67     NAPI_CALL(env, napi_set_named_property(env, businessError, "message", msg));
68 
69     return businessError;
70 }
71 
GetNapiNull(napi_env env)72 napi_value GetNapiNull(napi_env env)
73 {
74     napi_value result = nullptr;
75     NAPI_CALL(env, napi_get_null(env, &result));
76     return result;
77 }
78 }  // namespace AccessToken
79 }  // namespace Security
80 }  // namespace OHOS
81