• 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_PARAMETER_ERROR, "Parameter invalid, please input the correct parameter"},
27     {ERR_JS_ACCOUNT_NOT_FOUND, "Account not found, please check whether the account exists"},
28     {ERR_JS_IS_NOT_SYSTEM_APP, "This api is system api, Please use the system application to call this api"},
29     {ERR_JS_ACCOUNT_ALREADY_EXIST, "Account already exists, please cancel or try other account"},
30     {ERR_JS_ACCOUNT_ALREADY_ACTIVATED, "Account already activated, do not activate duplicately"},
31     {ERR_JS_ACCOUNT_SERVICE_BUSY, "Account service is busy, please wait for a moment and try again"},
32     {ERR_JS_ACCOUNT_NUMBER_REACH_LIMIT, "Account number limit exceeded, please try again after delete other accounts"},
33     {ERR_JS_MULTI_USER_NOT_SUPPORT, "Multiple users not supported, please cancel the creation"},
34     {ERR_JS_ACCOUNT_TYPE_NOT_SUPPORT, "Account type not supported, please create a non administrator account"},
35     {ERR_JS_ACCOUNT_RESTRICTED, "Account is restricted, the operation account ID is the reserved for system"},
36     {ERR_JS_LISTENER_ALREADY_REGISTERED,
37      "Listener is already registered, please register new listener or delete old listener and try again"},
38     {ERR_JS_LISTENER_NOT_REGISTERED, "Listener is not registered, please use the registered listener"},
39     {ERR_JS_CREDENTIAL_INPUTER_ALREADY_EXIST, "PIN inputer is already registered, please do not repeat register"},
40     {ERR_JS_SYSTEM_SERVICE_EXCEPTION, "System service exception, please try again or reboot your device"},
41     {ERR_JS_INVALID_PARAMETER, "Parameter invalid, please input the correct parameter"},
42     {ERR_JS_TRUST_LEVEL_NOT_SUPPORTED, "Trust level not supported, please input the correct trust level"},
43     {ERR_JS_AUTH_TYPE_NOT_SUPPORTED, "Auth type not supported, please input the correct auth type"},
44     {ERR_JS_AUTH_TIMEOUT, "Auth timeout, please try again or check your internet connection"},
45     {ERR_JS_AUTH_SERVICE_BUSY, "Auth service is busy, please try again later"},
46     {ERR_JS_AUTH_SERVICE_LOCKED,
47      "Auth service is locked, auth fail too many times, please try again after freezingTime"},
48     {ERR_JS_CREDENTIAL_NOT_EXIST, "Credential not found, please check whether credential exists and try again"},
49     {ERR_JS_INVALID_CONTEXT_ID, "Context ID is invalid, please check the context ID and try again"},
50     {ERR_JS_AUTH_CREDENTIAL_WRONG_ERROR,
51      "Auth credential incorrect, please check the password or credential, and try again"},
52     {ERR_JS_APPLICATION_NOT_EXIST, "Application not found, please check the bundle name and try again"},
53     {ERR_JS_ACCOUNT_AUTHENTICATOR_NOT_EXIST, "Application account authenticator service is not available, please try "
54                                              "again with applications that support authenticator service"},
55     {ERR_JS_ACCOUNT_AUTHENTICATOR_SERVICE_EXCEPTION,
56      "Application account authenticator service exception, please try again or reboot your device"},
57     {ERR_JS_SESSION_NOT_EXIST,
58      "Application account session is not exists, please use sessionId that successfully opened"},
59     {ERR_JS_AUTHORIZATION_LIST_TOO_LARGE,
60      "Application account authorization list is too large, please cancel some existed authentications and try again"},
61     {ERR_JS_TOKEN_NUMBER_REACH_LIMIT,
62      "Application account token number reach limitations, please delete some existed token and try again"},
63     {ERR_JS_CUSTOM_DATA_NUMBER_REACH_LIMIT,
64      "Application account customized data number reach limit, please delete some existed custom data and try again"},
65     {ERR_JS_CUSTOM_DATA_NOT_FOUND,
66      "Application account customized data not found, please use existed customized data and try again"},
67     {ERR_JS_AUTH_TYPE_NOT_FOUND, "Application account auth type not found, please use existed auth type"},
68     {ERR_JS_PERMISSION_DENIED, "Permission denied"},
69     {ERR_JS_PLUGIN_NETWORK_EXCEPTION, "Network exception"},
70     {ERR_JS_CAPABILITY_NOT_SUPPORTED, "capability not supported"},
71     {ERR_JS_ACCOUNT_LOGGED_IN_ACCOUNTS_OVERSIZE, "The number of the logged in OS accounts reaches upper limit"},
72     {ERR_JS_COMPLEXITY_CHECK_FAILED, "The complexity of credential check failed"},
73     {ERR_JS_PIN_IS_EXPIRED, "The PIN credential is expired"},
74     {ERR_JS_DOMAIN_PLUGIN_ALREADY_REGISTERED, "The domain plugin is already registered"},
75     {ERR_JS_SERVER_UNREACHABLE, "The server is unreachable"},
76     {ERR_JS_SERVER_CONFIG_NOT_FOUND, "The server config not found"},
77     {ERR_JS_SERVER_CONFIG_ALREADY_EXISTS, "Server config already exists"},
78     {ERR_JS_SERVER_CONFIG_ASSOCIATED_ACCOUNT, "Server config has been associated with an account"},
79     {ERR_JS_SERVER_CONFIG_UPPER_LIMIT, "The number of server config reaches the upper limit"},
80 };
81 
GenerateBusinessError(napi_env env,int32_t jsErrCode,const std::string & jsErrMsg)82 napi_value GenerateBusinessError(napi_env env, int32_t jsErrCode, const std::string &jsErrMsg)
83 {
84     napi_value errCodeJs = nullptr;
85     NAPI_CALL(env, napi_create_uint32(env, jsErrCode, &errCodeJs));
86 
87     napi_value errMsgJs = nullptr;
88     NAPI_CALL(env, napi_create_string_utf8(env, jsErrMsg.c_str(), NAPI_AUTO_LENGTH, &errMsgJs));
89 
90     napi_value errJs = nullptr;
91     NAPI_CALL(env, napi_create_error(env, nullptr, errMsgJs, &errJs));
92     NAPI_CALL(env, napi_set_named_property(env, errJs, "code", errCodeJs));
93     NAPI_CALL(env, napi_set_named_property(env, errJs, "message", errMsgJs));
94     return errJs;
95 }
96 
ConvertToJsErrMsg(int32_t jsErrCode)97 std::string ConvertToJsErrMsg(int32_t jsErrCode)
98 {
99     auto iter = g_errorStringMap.find(jsErrCode);
100     if (iter != g_errorStringMap.end()) {
101         return iter->second;
102     } else {
103         return "Unknown error, please reboot your device and try again";
104     }
105 }
106 
GetErrorCodeValue(napi_env env,int errCode)107 static napi_value GetErrorCodeValue(napi_env env, int errCode)
108 {
109     napi_value jsObject = nullptr;
110     napi_value jsValue = nullptr;
111     NAPI_CALL(env, napi_create_int32(env, errCode, &jsValue));
112     NAPI_CALL(env, napi_create_object(env, &jsObject));
113     NAPI_CALL(env, napi_set_named_property(env, jsObject, "code", jsValue));
114     return jsObject;
115 }
116 
GenerateBusinessSuccess(napi_env env,bool throwErr)117 napi_value GenerateBusinessSuccess(napi_env env, bool throwErr)
118 {
119     if (throwErr) {
120         napi_value errJs = nullptr;
121         napi_get_null(env, &errJs);
122         return errJs;
123     }
124     return GetErrorCodeValue(env, 0);
125 }
126 
GenerateBusinessError(napi_env env,int32_t nativeErrCode,bool throwErr)127 napi_value GenerateBusinessError(napi_env env, int32_t nativeErrCode, bool throwErr)
128 {
129     if (throwErr) {
130         return GenerateBusinessError(env, nativeErrCode);
131     }
132     return GetErrorCodeValue(env, nativeErrCode);
133 }
134 
GenerateBusinessError(napi_env env,int32_t nativeErrCode)135 napi_value GenerateBusinessError(napi_env env, int32_t nativeErrCode)
136 {
137     int32_t jsErrCode = nativeErrCode;
138     auto iter = g_errorStringMap.find(jsErrCode);
139     if (iter == g_errorStringMap.end()) {
140         jsErrCode = ConvertToJSErrCode(nativeErrCode);
141     }
142     std::string jsErrMsg = ConvertToJsErrMsg(jsErrCode);
143 
144     return GenerateBusinessError(env, jsErrCode, jsErrMsg);
145 }
146 
CheckJsErrorCode(int32_t errCode)147 bool CheckJsErrorCode(int32_t errCode)
148 {
149     auto iter = g_errorStringMap.find(errCode);
150     if (iter == g_errorStringMap.end()) {
151         return false;
152     }
153     return true;
154 }
155 
AccountNapiThrow(napi_env env,int32_t nativeErrCode,bool throwErr)156 void AccountNapiThrow(napi_env env, int32_t nativeErrCode, bool throwErr)
157 {
158     if (throwErr) {
159         napi_throw(env, GenerateBusinessError(env, nativeErrCode));
160     }
161 }
162 
AccountNapiThrow(napi_env env,int32_t jsErrCode,const std::string & jsErrMsg,bool throwErr)163 void AccountNapiThrow(napi_env env, int32_t jsErrCode, const std::string &jsErrMsg, bool throwErr)
164 {
165     if (throwErr) {
166         napi_throw(env, GenerateBusinessError(env, jsErrCode, jsErrMsg));
167     }
168 }
169 
AccountIAMNapiThrow(napi_env env,int32_t jsErrCode,bool throwErr)170 void AccountIAMNapiThrow(napi_env env, int32_t jsErrCode, bool throwErr)
171 {
172     if (throwErr) {
173         napi_throw(env, GenerateBusinessError(env, jsErrCode, ConvertToJsErrMsg(jsErrCode)));
174     }
175 }
176 } // namespace AccountJsKit
177 } // namespace OHOS