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