• 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. Possible causes:"
41         "(1)IPC communication failure;"
42         "(2)Insufficient memory;"
43         "(3)Insufficient disk space;"
44         "(4)Inability to start necessary system services."
45         "Please try again or restart your device"},
46     {ERR_JS_INVALID_PARAMETER, "Parameter invalid, please input the correct parameter"},
47     {ERR_JS_TRUST_LEVEL_NOT_SUPPORTED, "Trust level not supported, please input the correct trust level"},
48     {ERR_JS_AUTH_TYPE_NOT_SUPPORTED, "Auth type not supported, please input the correct auth type"},
49     {ERR_JS_AUTH_TIMEOUT, "Auth timeout, please try again or check your internet connection"},
50     {ERR_JS_AUTH_SERVICE_BUSY, "Auth service is busy, please try again later"},
51     {ERR_JS_AUTH_SERVICE_LOCKED,
52      "Auth service is locked, auth fail too many times, please try again after freezingTime"},
53     {ERR_JS_CREDENTIAL_NOT_EXIST, "Credential not found, please check whether credential exists and try again"},
54     {ERR_JS_INVALID_CONTEXT_ID, "Context ID is invalid, please check the context ID and try again"},
55     {ERR_JS_AUTH_CREDENTIAL_WRONG_ERROR,
56      "Auth credential incorrect, please check the password or credential, and try again"},
57     {ERR_JS_APPLICATION_NOT_EXIST, "Application not found, please check the bundle name and try again"},
58     {ERR_JS_ACCOUNT_AUTHENTICATOR_NOT_EXIST, "Application account authenticator service is not available, please try "
59                                              "again with applications that support authenticator service"},
60     {ERR_JS_ACCOUNT_AUTHENTICATOR_SERVICE_EXCEPTION,
61      "Application account authenticator service exception, please try again or reboot your device"},
62     {ERR_JS_SESSION_NOT_EXIST,
63      "Application account session is not exists, please use sessionId that successfully opened"},
64     {ERR_JS_AUTHORIZATION_LIST_TOO_LARGE,
65      "Application account authorization list is too large, please cancel some existed authentications and try again"},
66     {ERR_JS_TOKEN_NUMBER_REACH_LIMIT,
67      "Application account token number reach limitations, please delete some existed token and try again"},
68     {ERR_JS_CUSTOM_DATA_NUMBER_REACH_LIMIT,
69      "Application account customized data number reach limit, please delete some existed custom data and try again"},
70     {ERR_JS_CUSTOM_DATA_NOT_FOUND,
71      "Application account customized data not found, please use existed customized data and try again"},
72     {ERR_JS_AUTH_TYPE_NOT_FOUND, "Application account auth type not found, please use existed auth type"},
73     {ERR_JS_PERMISSION_DENIED, "Permission denied"},
74     {ERR_JS_PLUGIN_NETWORK_EXCEPTION, "Network exception"},
75     {ERR_JS_CAPABILITY_NOT_SUPPORTED, "capability not supported"},
76     {ERR_JS_ACCOUNT_LOGGED_IN_ACCOUNTS_OVERSIZE, "The number of the logged in OS accounts reaches upper limit"},
77     {ERR_JS_COMPLEXITY_CHECK_FAILED, "The complexity of credential check failed"},
78     {ERR_JS_PIN_IS_EXPIRED, "The PIN credential is expired"},
79     {ERR_JS_DOMAIN_PLUGIN_ALREADY_REGISTERED, "The domain plugin is already registered"},
80     {ERR_JS_SERVER_UNREACHABLE, "The server is unreachable"},
81     {ERR_JS_SERVER_CONFIG_NOT_FOUND, "The server config not found"},
82     {ERR_JS_SERVER_CONFIG_ALREADY_EXISTS, "Server config already exists"},
83     {ERR_JS_SERVER_CONFIG_ASSOCIATED_ACCOUNT, "Server config has been associated with an account"},
84     {ERR_JS_SERVER_CONFIG_UPPER_LIMIT, "The number of server config reaches the upper limit"},
85     {ERR_JS_OS_ACCOUNT_ALREADY_BOUND, "The OS account is already bound."},
86     {ERR_JS_DOMAIN_ACCOUNT_ALREADY_BOUND, "The domain account is already bound."},
87     {ERR_JS_ACCOUNT_CROSS_DEVICE_CAPABILITY_NOT_SUPPORT, "Cross-device capability not supported"},
88     {ERR_JS_ACCOUNT_CROSS_DEVICE_COMMUNICATION_FAILED, "Cross-device communication failed"},
89 };
90 
GenerateBusinessError(napi_env env,int32_t jsErrCode,const std::string & jsErrMsg)91 napi_value GenerateBusinessError(napi_env env, int32_t jsErrCode, const std::string &jsErrMsg)
92 {
93     napi_value errCodeJs = nullptr;
94     NAPI_CALL(env, napi_create_uint32(env, jsErrCode, &errCodeJs));
95 
96     napi_value errMsgJs = nullptr;
97     NAPI_CALL(env, napi_create_string_utf8(env, jsErrMsg.c_str(), NAPI_AUTO_LENGTH, &errMsgJs));
98 
99     napi_value errJs = nullptr;
100     NAPI_CALL(env, napi_create_error(env, nullptr, errMsgJs, &errJs));
101     NAPI_CALL(env, napi_set_named_property(env, errJs, "code", errCodeJs));
102     NAPI_CALL(env, napi_set_named_property(env, errJs, "message", errMsgJs));
103     return errJs;
104 }
105 
ConvertToJsErrMsg(int32_t jsErrCode)106 std::string ConvertToJsErrMsg(int32_t jsErrCode)
107 {
108     auto iter = g_errorStringMap.find(jsErrCode);
109     if (iter != g_errorStringMap.end()) {
110         return iter->second;
111     } else {
112         return "Unknown error, please reboot your device and try again";
113     }
114 }
115 
GetErrorCodeValue(napi_env env,int errCode)116 static napi_value GetErrorCodeValue(napi_env env, int errCode)
117 {
118     napi_value jsObject = nullptr;
119     napi_value jsValue = nullptr;
120     NAPI_CALL(env, napi_create_int32(env, errCode, &jsValue));
121     NAPI_CALL(env, napi_create_object(env, &jsObject));
122     NAPI_CALL(env, napi_set_named_property(env, jsObject, "code", jsValue));
123     return jsObject;
124 }
125 
GenerateBusinessSuccess(napi_env env,bool throwErr)126 napi_value GenerateBusinessSuccess(napi_env env, bool throwErr)
127 {
128     if (throwErr) {
129         napi_value errJs = nullptr;
130         napi_get_null(env, &errJs);
131         return errJs;
132     }
133     return GetErrorCodeValue(env, 0);
134 }
135 
GenerateBusinessError(napi_env env,int32_t nativeErrCode,bool throwErr)136 napi_value GenerateBusinessError(napi_env env, int32_t nativeErrCode, bool throwErr)
137 {
138     if (throwErr) {
139         return GenerateBusinessError(env, nativeErrCode);
140     }
141     return GetErrorCodeValue(env, nativeErrCode);
142 }
143 
GenerateBusinessError(napi_env env,int32_t nativeErrCode)144 napi_value GenerateBusinessError(napi_env env, int32_t nativeErrCode)
145 {
146     int32_t jsErrCode = nativeErrCode;
147     auto iter = g_errorStringMap.find(jsErrCode);
148     if (iter == g_errorStringMap.end()) {
149         jsErrCode = ConvertToJSErrCode(nativeErrCode);
150     }
151     std::string jsErrMsg = ConvertToJsErrMsg(jsErrCode);
152 
153     return GenerateBusinessError(env, jsErrCode, jsErrMsg);
154 }
155 
CheckJsErrorCode(int32_t errCode)156 bool CheckJsErrorCode(int32_t errCode)
157 {
158     auto iter = g_errorStringMap.find(errCode);
159     if (iter == g_errorStringMap.end()) {
160         return false;
161     }
162     return true;
163 }
164 
AccountNapiThrow(napi_env env,int32_t nativeErrCode,bool throwErr)165 void AccountNapiThrow(napi_env env, int32_t nativeErrCode, bool throwErr)
166 {
167     if (throwErr) {
168         napi_throw(env, GenerateBusinessError(env, nativeErrCode));
169     }
170 }
171 
AccountNapiThrow(napi_env env,int32_t jsErrCode,const std::string & jsErrMsg,bool throwErr)172 void AccountNapiThrow(napi_env env, int32_t jsErrCode, const std::string &jsErrMsg, bool throwErr)
173 {
174     if (throwErr) {
175         napi_throw(env, GenerateBusinessError(env, jsErrCode, jsErrMsg));
176     }
177 }
178 
AccountIAMNapiThrow(napi_env env,int32_t jsErrCode,bool throwErr)179 void AccountIAMNapiThrow(napi_env env, int32_t jsErrCode, bool throwErr)
180 {
181     if (throwErr) {
182         napi_throw(env, GenerateBusinessError(env, jsErrCode, ConvertToJsErrMsg(jsErrCode)));
183     }
184 }
185 } // namespace AccountJsKit
186 } // namespace OHOS