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 };
78 
GenerateBusinessError(napi_env env,int32_t jsErrCode,const std::string & jsErrMsg)79 napi_value GenerateBusinessError(napi_env env, int32_t jsErrCode, const std::string &jsErrMsg)
80 {
81     napi_value errCodeJs = nullptr;
82     NAPI_CALL(env, napi_create_uint32(env, jsErrCode, &errCodeJs));
83 
84     napi_value errMsgJs = nullptr;
85     NAPI_CALL(env, napi_create_string_utf8(env, jsErrMsg.c_str(), NAPI_AUTO_LENGTH, &errMsgJs));
86 
87     napi_value errJs = nullptr;
88     NAPI_CALL(env, napi_create_error(env, nullptr, errMsgJs, &errJs));
89     NAPI_CALL(env, napi_set_named_property(env, errJs, "code", errCodeJs));
90     NAPI_CALL(env, napi_set_named_property(env, errJs, "message", errMsgJs));
91     return errJs;
92 }
93 
ConvertToJsErrMsg(int32_t jsErrCode)94 std::string ConvertToJsErrMsg(int32_t jsErrCode)
95 {
96     auto iter = g_errorStringMap.find(jsErrCode);
97     if (iter != g_errorStringMap.end()) {
98         return iter->second;
99     } else {
100         return "Unknown error, please reboot your device and try again";
101     }
102 }
103 
GetErrorCodeValue(napi_env env,int errCode)104 static napi_value GetErrorCodeValue(napi_env env, int errCode)
105 {
106     napi_value jsObject = nullptr;
107     napi_value jsValue = nullptr;
108     NAPI_CALL(env, napi_create_int32(env, errCode, &jsValue));
109     NAPI_CALL(env, napi_create_object(env, &jsObject));
110     NAPI_CALL(env, napi_set_named_property(env, jsObject, "code", jsValue));
111     return jsObject;
112 }
113 
GenerateBusinessSuccess(napi_env env,bool throwErr)114 napi_value GenerateBusinessSuccess(napi_env env, bool throwErr)
115 {
116     if (throwErr) {
117         napi_value errJs = nullptr;
118         napi_get_null(env, &errJs);
119         return errJs;
120     }
121     return GetErrorCodeValue(env, 0);
122 }
123 
GenerateBusinessError(napi_env env,int32_t nativeErrCode,bool throwErr)124 napi_value GenerateBusinessError(napi_env env, int32_t nativeErrCode, bool throwErr)
125 {
126     if (throwErr) {
127         return GenerateBusinessError(env, nativeErrCode);
128     }
129     return GetErrorCodeValue(env, nativeErrCode);
130 }
131 
GenerateBusinessError(napi_env env,int32_t nativeErrCode)132 napi_value GenerateBusinessError(napi_env env, int32_t nativeErrCode)
133 {
134     int32_t jsErrCode = nativeErrCode;
135     auto iter = g_errorStringMap.find(jsErrCode);
136     if (iter == g_errorStringMap.end()) {
137         jsErrCode = ConvertToJSErrCode(nativeErrCode);
138     }
139     std::string jsErrMsg = ConvertToJsErrMsg(jsErrCode);
140 
141     return GenerateBusinessError(env, jsErrCode, jsErrMsg);
142 }
143 
CheckJsErrorCode(int32_t errCode)144 bool CheckJsErrorCode(int32_t errCode)
145 {
146     auto iter = g_errorStringMap.find(errCode);
147     if (iter == g_errorStringMap.end()) {
148         return false;
149     }
150     return true;
151 }
152 
AccountNapiThrow(napi_env env,int32_t nativeErrCode,bool throwErr)153 void AccountNapiThrow(napi_env env, int32_t nativeErrCode, bool throwErr)
154 {
155     if (throwErr) {
156         napi_throw(env, GenerateBusinessError(env, nativeErrCode));
157     }
158 }
159 
AccountNapiThrow(napi_env env,int32_t jsErrCode,const std::string & jsErrMsg,bool throwErr)160 void AccountNapiThrow(napi_env env, int32_t jsErrCode, const std::string &jsErrMsg, bool throwErr)
161 {
162     if (throwErr) {
163         napi_throw(env, GenerateBusinessError(env, jsErrCode, jsErrMsg));
164     }
165 }
166 
AccountIAMNapiThrow(napi_env env,int32_t jsErrCode,bool throwErr)167 void AccountIAMNapiThrow(napi_env env, int32_t jsErrCode, bool throwErr)
168 {
169     if (throwErr) {
170         napi_throw(env, GenerateBusinessError(env, jsErrCode, ConvertToJsErrMsg(jsErrCode)));
171     }
172 }
173 } // namespace AccountJsKit
174 } // namespace OHOS