• 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 
16 #include "napi_account_error.h"
17 #include "account_error_no.h"
18 #include "account_log_wrapper.h"
19 #include <unordered_map>
20 
21 using namespace OHOS::AccountSA;
22 
23 namespace OHOS {
24 namespace AccountJsKit {
25 static const std::unordered_map<uint32_t, std::string> g_errorStringMap = {
26     {ERR_JS_ACCOUNT_NOT_FOUND, "Account not found, please check whether the account exists"},
27     {ERR_JS_ACCOUNT_ALREADY_EXIST, "Account already exists, please cancel or try other account"},
28     {ERR_JS_ACCOUNT_ALREADY_ACTIVATED, "Account already activated, do not activate duplicately"},
29     {ERR_JS_ACCOUNT_SERVICE_BUSY, "Account service is busy, please wait for a moment and try again"},
30     {ERR_JS_ACCOUNT_NUMBER_REACH_LIMIT, "Account number limit exceeded, please try again after delete other accounts"},
31     {ERR_JS_MULTI_USER_NOT_SUPPORT, "Multiple users not supported, please cancel the creation"},
32     {ERR_JS_ACCOUNT_TYPE_NOT_SUPPORT, "Account type not supported, please create a non administrator account"},
33     {ERR_JS_ACCOUNT_RESTRICTED, "Account is restricted, the operation account ID is the reserved for system"},
34     {ERR_JS_LISTENER_ALREADY_REGISTERED,
35      "Listener is already registered, please register new listener or delete old listener and try again"},
36     {ERR_JS_LISTENER_NOT_REGISTERED, "Listener is not registered, please use the registered listener"},
37     {ERR_JS_CREDENTIAL_INPUTER_ALREADY_EXIST, "PIN inputer is already registered, please do not repeat register"},
38     {ERR_JS_SYSTEM_SERVICE_EXCEPTION, "System service exception, please try again or reboot your device"},
39     {ERR_JS_INVALID_PARAMETER, "Parameter invalid, please input the correct parameter"},
40     {ERR_JS_TRUST_LEVEL_NOT_SUPPORTED, "Trust level not supported, please input the correct trust level"},
41     {ERR_JS_AUTH_TYPE_NOT_SUPPORTED, "Auth type not supported, please input the correct auth type"},
42     {ERR_JS_AUTH_TIMEOUT, "Auth timeout, please try again or check your internet connection"},
43     {ERR_JS_AUTH_SERVICE_BUSY, "Auth service is busy, please try again later"},
44     {ERR_JS_AUTH_SERVICE_LOCKED,
45      "Auth service is locked, auth fail too many times, please try again after freezingTime"},
46     {ERR_JS_CREDENTIAL_NOT_EXIST, "Credential not found, please check whether credential exists and try again"},
47     {ERR_JS_INVALID_CONTEXT_ID, "Context ID is invalid, please check the context ID and try again"},
48     {ERR_JS_AUTH_CREDENTIAL_WRONG_ERROR,
49      "Auth credential incorrect, please check the password or credential, and try again"},
50     {ERR_JS_APPLICATION_NOT_EXIST, "Application not found, please check the bundle name and try again"},
51     {ERR_JS_ACCOUNT_AUTHENTICATOR_NOT_EXIST, "Application account authenticator service is not available, please try "
52                                              "again with applications that support authenticator service"},
53     {ERR_JS_ACCOUNT_AUTHENTICATOR_SERVICE_EXCEPTION,
54      "Application account authenticator service exception, please try again or reboot your device"},
55     {ERR_JS_SESSION_NOT_EXIST,
56      "Application account session is not exists, please use sessionId that successfully opened"},
57     {ERR_JS_AUTHORIZATION_LIST_TOO_LARGE,
58      "Application account authorization list is too large, please cancel some existed authentications and try again"},
59     {ERR_JS_TOKEN_NUMBER_REACH_LIMIT,
60      "Application account token number reach limitations, please delete some existed token and try again"},
61     {ERR_JS_CUSTOM_DATA_NUMBER_REACH_LIMIT,
62      "Application account customized data number reach limit, please delete some existed custom data and try again"},
63     {ERR_JS_CUSTOM_DATA_NOT_FOUND,
64      "Application account customized data not found, please use existed customized data and try again"},
65     {ERR_JS_AUTH_TYPE_NOT_FOUND, "Application account auth type not found, please use existed auth type"},
66     {ERR_JS_PERMISSION_DENIED, "Permission denied"},
67     {ERR_JS_DOMAIN_PLUGIN_ALREADY_REGISTERED, "Domain plugin already registered"}
68 };
69 
GenerateBusinessError(napi_env env,int32_t jsErrCode,const std::string & jsErrMsg)70 napi_value GenerateBusinessError(napi_env env, int32_t jsErrCode, const std::string &jsErrMsg)
71 {
72     napi_value errCodeJs = nullptr;
73     NAPI_CALL(env, napi_create_uint32(env, jsErrCode, &errCodeJs));
74 
75     napi_value errMsgJs = nullptr;
76     NAPI_CALL(env, napi_create_string_utf8(env, jsErrMsg.c_str(), NAPI_AUTO_LENGTH, &errMsgJs));
77 
78     napi_value errJs = nullptr;
79     NAPI_CALL(env, napi_create_error(env, nullptr, errMsgJs, &errJs));
80     NAPI_CALL(env, napi_set_named_property(env, errJs, "code", errCodeJs));
81     NAPI_CALL(env, napi_set_named_property(env, errJs, "message", errMsgJs));
82     return errJs;
83 }
84 
ConvertToJsErrMsg(int32_t jsErrCode)85 std::string ConvertToJsErrMsg(int32_t jsErrCode)
86 {
87     auto iter = g_errorStringMap.find(jsErrCode);
88     if (iter != g_errorStringMap.end()) {
89         return iter->second;
90     } else {
91         return "Unknown error, please reboot your device and try again";
92     }
93 }
94 
GetErrorCodeValue(napi_env env,int errCode)95 static napi_value GetErrorCodeValue(napi_env env, int errCode)
96 {
97     napi_value jsObject = nullptr;
98     napi_value jsValue = nullptr;
99     NAPI_CALL(env, napi_create_int32(env, errCode, &jsValue));
100     NAPI_CALL(env, napi_create_object(env, &jsObject));
101     NAPI_CALL(env, napi_set_named_property(env, jsObject, "code", jsValue));
102     return jsObject;
103 }
104 
GenerateBusinessSuccess(napi_env env,bool throwErr)105 napi_value GenerateBusinessSuccess(napi_env env, bool throwErr)
106 {
107     if (throwErr) {
108         napi_value errJs = nullptr;
109         napi_get_null(env, &errJs);
110         return errJs;
111     }
112     return GetErrorCodeValue(env, 0);
113 }
114 
GenerateBusinessError(napi_env env,int32_t nativeErrCode,bool throwErr)115 napi_value GenerateBusinessError(napi_env env, int32_t nativeErrCode, bool throwErr)
116 {
117     if (throwErr) {
118         return GenerateBusinessError(env, nativeErrCode);
119     }
120     return GetErrorCodeValue(env, nativeErrCode);
121 }
122 
GenerateBusinessError(napi_env env,int32_t nativeErrCode)123 napi_value GenerateBusinessError(napi_env env, int32_t nativeErrCode)
124 {
125     int32_t jsErrCode = ConvertToJSErrCode(nativeErrCode);
126     std::string jsErrMsg = ConvertToJsErrMsg(jsErrCode);
127 
128     return GenerateBusinessError(env, jsErrCode, jsErrMsg);
129 }
130 
AccountNapiThrow(napi_env env,int32_t nativeErrCode,bool throwErr)131 void AccountNapiThrow(napi_env env, int32_t nativeErrCode, bool throwErr)
132 {
133     if (throwErr) {
134         napi_throw(env, GenerateBusinessError(env, nativeErrCode));
135     }
136 }
137 
AccountNapiThrow(napi_env env,int32_t jsErrCode,const std::string & jsErrMsg,bool throwErr)138 void AccountNapiThrow(napi_env env, int32_t jsErrCode, const std::string &jsErrMsg, bool throwErr)
139 {
140     if (throwErr) {
141         napi_throw(env, GenerateBusinessError(env, jsErrCode, jsErrMsg));
142     }
143 }
144 
AccountIAMNapiThrow(napi_env env,int32_t jsErrCode,bool throwErr)145 void AccountIAMNapiThrow(napi_env env, int32_t jsErrCode, bool throwErr)
146 {
147     if (throwErr) {
148         napi_throw(env, GenerateBusinessError(env, jsErrCode, ConvertToJsErrMsg(jsErrCode)));
149     }
150 }
151 } // namespace AccountJsKit
152 } // namespace OHOS