• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2024 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 "account_error_no.h"
17 #include <unordered_map>
18 
19 namespace OHOS {
20 const std::unordered_map<int32_t, int32_t> errorMap = {
21     { ERR_OK, ERR_JS_SUCCESS },
22     { ERR_ACCOUNT_COMMON_NOT_SYSTEM_APP_ERROR, ERR_JS_IS_NOT_SYSTEM_APP },
23     { ERR_ACCOUNT_COMMON_INVALID_PARAMETER, ERR_JS_INVALID_PARAMETER },
24     { ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR, ERR_JS_ACCOUNT_NOT_FOUND },
25     { ERR_ACCOUNT_COMMON_NOT_AUTHENTICATED, ERR_JS_ACCOUNT_NOT_AUTHENTICATED }
26 };
27 
AppAccountConvertOtherJSErrCode(int32_t errCode)28 int32_t AppAccountConvertOtherJSErrCode(int32_t errCode)
29 {
30     switch (errCode) {
31         case ERR_APPACCOUNT_SERVICE_ACCOUNT_NOT_EXIST:
32         case ERR_APPACCOUNT_SERVICE_GET_ACCOUNT_INFO_BY_ID:
33             return ERR_JS_ACCOUNT_NOT_FOUND;
34         case ERR_APPACCOUNT_SERVICE_ADD_EXISTING_ACCOUNT:
35             return ERR_JS_ACCOUNT_ALREADY_EXIST;
36         case ERR_APPACCOUNT_SERVICE_ACCOUNT_MAX_SIZE:
37             return ERR_JS_ACCOUNT_NUMBER_REACH_LIMIT;
38         case ERR_APPACCOUNT_SUBSCRIBER_ALREADY_REGISTERED:
39             return ERR_JS_LISTENER_ALREADY_REGISTERED;
40         case ERR_APPACCOUNT_SERVICE_ASSOCIATED_DATA_OVER_SIZE:
41             return ERR_JS_CUSTOM_DATA_NUMBER_REACH_LIMIT;
42         case ERR_APPACCOUNT_SERVICE_ASSOCIATED_DATA_KEY_NOT_EXIST:
43             return ERR_JS_CUSTOM_DATA_NOT_FOUND;
44         case ERR_APPACCOUNT_SERVICE_ACCOUNT_CREDENTIAL_NOT_EXIST:
45             return ERR_JS_CREDENTIAL_NOT_EXIST;
46         case ERR_APPACCOUNT_KIT_NO_SPECIFIED_SUBSCRIBER_HAS_BEEN_REGISTERED:
47             return ERR_JS_LISTENER_NOT_REGISTERED;
48         case ERR_APPACCOUNT_SERVICE_OAUTH_AUTHENTICATOR_NOT_EXIST:
49             return ERR_JS_ACCOUNT_AUTHENTICATOR_NOT_EXIST;
50         case ERR_APPACCOUNT_SERVICE_OAUTH_BUSY:
51             return ERR_JS_ACCOUNT_SERVICE_BUSY;
52         case ERR_APPACCOUNT_SERVICE_OAUTH_LIST_MAX_SIZE:
53             return ERR_JS_AUTHORIZATION_LIST_TOO_LARGE;
54         case ERR_APPACCOUNT_SERVICE_OAUTH_SESSION_NOT_EXIST:
55         case ERR_APPACCOUNT_SERVICE_OAUTH_AUTHENTICATOR_CALLBACK_NOT_EXIST:
56             return ERR_JS_SESSION_NOT_EXIST;
57         case ERR_APPACCOUNT_SERVICE_OAUTH_TYPE_NOT_EXIST:
58         case ERR_APPACCOUNT_SERVICE_OAUTH_TOKEN_NOT_EXIST:
59             return ERR_JS_AUTH_TYPE_NOT_FOUND;
60         case ERR_APPACCOUNT_SERVICE_OAUTH_TOKEN_MAX_SIZE:
61             return ERR_JS_TOKEN_NUMBER_REACH_LIMIT;
62         default:
63             return ERR_JS_SYSTEM_SERVICE_EXCEPTION;
64     }
65 }
66 
AppAccountConvertToJSErrCode(int32_t errCode)67 int32_t AppAccountConvertToJSErrCode(int32_t errCode)
68 {
69     if (errCode == ERR_ACCOUNT_COMMON_PERMISSION_DENIED) {
70         return ERR_JS_PERMISSION_DENIED;
71     } else if (errCode == ERR_APPACCOUNT_SERVICE_GET_BUNDLE_NAME ||
72         errCode == ERR_APPACCOUNT_SERVICE_GET_BUNDLE_INFO ||
73         errCode == ERR_APPACCOUNT_SERVICE_DISABLE_APP_ACCESS_NOT_EXISTED) {
74         return ERR_JS_APPLICATION_NOT_EXIST;
75     } else if (errCode == ERR_APPACCOUNT_SERVICE_OAUTH_SERVICE_EXCEPTION) {
76         return ERR_JS_ACCOUNT_AUTHENTICATOR_SERVICE_EXCEPTION;
77     } else {
78         return AppAccountConvertOtherJSErrCode(errCode);
79     }
80 }
81 
OsAccountConvertToJSErrCode(int32_t errCode)82 int32_t OsAccountConvertToJSErrCode(int32_t errCode)
83 {
84     if (errCode == ERR_ACCOUNT_COMMON_INVALID_PARAMETER) {
85         return ERR_JS_INVALID_PARAMETER;
86     }
87     switch (errCode) {
88         case ERR_OSACCOUNT_SERVICE_INNER_SELECT_ERROR:
89         case ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR:
90             return ERR_JS_ACCOUNT_NOT_FOUND;
91         case ERR_OSACCOUNT_SERVICE_INNER_DOMAIN_ALREADY_BIND_ERROR:
92         case ERR_ACCOUNT_COMMON_NAME_HAD_EXISTED:
93             return ERR_JS_ACCOUNT_ALREADY_EXIST;
94         case ERR_ACCOUNT_COMMON_SHORT_NAME_HAD_EXISTED:
95             return ERR_JS_ACCOUNT_SHORT_NAME_ALREADY_EXIST;
96         case ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_ALREADY_ACTIVE_ERROR:
97             return ERR_JS_ACCOUNT_ALREADY_ACTIVATED;
98         case ERR_OSACCOUNT_SERVICE_CONTROL_MAX_CAN_CREATE_ERROR:
99             return ERR_JS_ACCOUNT_NUMBER_REACH_LIMIT;
100         case ERR_OSACCOUNT_SERVICE_LOGGED_IN_ACCOUNTS_OVERSIZE:
101             return ERR_JS_ACCOUNT_LOGGED_IN_ACCOUNTS_OVERSIZE;
102         case ERR_OSACCOUNT_SERVICE_MANAGER_NOT_ENABLE_MULTI_ERROR:
103             return ERR_JS_MULTI_USER_NOT_SUPPORT;
104         case ERR_OSACCOUNT_SERVICE_MANAGER_CREATE_OSACCOUNT_TYPE_ERROR:
105             return ERR_JS_ACCOUNT_TYPE_NOT_SUPPORT;
106         case ERR_OSACCOUNT_SERVICE_MANAGER_ID_ERROR:
107         case ERR_OSACCOUNT_SERVICE_CONTROL_CANNOT_DELETE_ID_ERROR:
108         case ERR_OSACCOUNT_SERVICE_CONTROL_ID_CANNOT_CREATE_ERROR:
109         case ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_STOP_ACTIVE_ERROR:
110             return ERR_JS_ACCOUNT_RESTRICTED;
111         case ERR_OSACCOUNT_KIT_NO_SPECIFIED_SUBSCRIBER_HAS_BEEN_REGISTERED:
112             return ERR_JS_LISTENER_NOT_REGISTERED;
113         case ERR_ACCOUNT_COMMON_PERMISSION_DENIED:
114             return ERR_JS_PERMISSION_DENIED;
115         case ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_OPERATING_ERROR:
116             return ERR_JS_ACCOUNT_SERVICE_BUSY;
117         case ERR_OSACCOUNT_SERVICE_INNER_OS_ACCOUNT_ALREADY_BOUND:
118             return ERR_JS_OS_ACCOUNT_ALREADY_BOUND;
119         case ERR_OSACCOUNT_SERVICE_INNER_DOMAIN_ACCOUNT_ALREADY_BOUND:
120             return ERR_JS_DOMAIN_ACCOUNT_ALREADY_BOUND;
121         default:
122             return ERR_JS_SYSTEM_SERVICE_EXCEPTION;
123     }
124 }
125 
DomainAccountConvertToJSErrCode(int32_t errCode)126 static int32_t DomainAccountConvertToJSErrCode(int32_t errCode)
127 {
128     switch (errCode) {
129         case ERR_DOMAIN_ACCOUNT_SERVICE_PLUGIN_ALREADY_EXIST:
130             return ERR_JS_DOMAIN_PLUGIN_ALREADY_REGISTERED;
131         case ERR_DOMAIN_ACCOUNT_SERVICE_NOT_DOMAIN_ACCOUNT:
132             return ERR_JS_ACCOUNT_NOT_FOUND;
133         case ERR_DOMAIN_ACCOUNT_SERVICE_PLUGIN_NOT_EXIST:
134         case ERR_DOMAIN_ACCOUNT_NOT_SUPPORT:
135             return ERR_JS_CAPABILITY_NOT_SUPPORTED;
136         case ERR_DOMAIN_ACCOUNT_NOT_SUPPORT_BACKGROUND_ACCOUNT_REQUEST:
137             return ERR_JS_CAPABILITY_NOT_SUPPORTED;
138         default:
139             return ERR_JS_SYSTEM_SERVICE_EXCEPTION;
140     }
141 }
142 
IsAppAccountKitError(int32_t errCode)143 static bool IsAppAccountKitError(int32_t errCode)
144 {
145     return (errCode >= ERR_APPACCOUNT_KIT_GET_APP_ACCOUNT_SERVICE &&
146         errCode <= ERR_APPACCOUNT_KIT_READ_PARCELABLE_VECTOR_ACCOUNT_INFO);
147 }
148 
IsAppAccountServiceError(int32_t errCode)149 static bool IsAppAccountServiceError(int32_t errCode)
150 {
151     return (errCode >= ERR_APPACCOUNT_SERVICE_ACCOUNT_NOT_EXIST && errCode <= ERR_APPACCOUNT_SERVICE_OTHER);
152 }
153 
IsOsAccountKitError(int32_t errCode)154 static bool IsOsAccountKitError(int32_t errCode)
155 {
156     return (errCode >= ERR_OSACCOUNT_KIT_CREATE_OS_ACCOUNT_FOR_DOMAIN_ERROR &&
157         errCode <= ERR_OSACCOUNT_KIT_NO_SPECIFIED_SUBSCRIBER_HAS_BEEN_REGISTERED);
158 }
159 
IsOsAccountServiceError(int32_t errCode)160 static bool IsOsAccountServiceError(int32_t errCode)
161 {
162     return ((errCode >= ERR_OSACCOUNT_SERVICE_MANAGER_QUERY_DISTRIBUTE_DATA_ERROR) &&
163             (errCode <= ERR_OS_ACCOUNT_SERVICE_CODE_END)) ||
164            (errCode == ERR_ACCOUNT_COMMON_NAME_HAD_EXISTED) || (errCode == ERR_ACCOUNT_COMMON_SHORT_NAME_HAD_EXISTED);
165 }
166 
IsDomainAccountServiceError(int32_t errCode)167 static bool IsDomainAccountServiceError(int32_t errCode)
168 {
169     return (errCode >= ERR_DOMAIN_ACCOUNT_SERVICE_PLUGIN_ALREADY_EXIST) &&
170         (errCode <= ERR_DOMAIN_ACCOUNT_SERVICE_ERR_CODE_LIMIT);
171 }
172 
ConvertToJSErrCode(int32_t nativeErrCode)173 int32_t ConvertToJSErrCode(int32_t nativeErrCode)
174 {
175     auto it = errorMap.find(nativeErrCode);
176     if (it != errorMap.end()) {
177         return it->second;
178     }
179     if (IsAppAccountKitError(nativeErrCode) || IsAppAccountServiceError(nativeErrCode)) {
180         return AppAccountConvertToJSErrCode(nativeErrCode);
181     } else if (IsOsAccountKitError(nativeErrCode) || IsOsAccountServiceError(nativeErrCode)) {
182         return OsAccountConvertToJSErrCode(nativeErrCode);
183     } else if (IsDomainAccountServiceError(nativeErrCode)) {
184         return DomainAccountConvertToJSErrCode(nativeErrCode);
185     } else if (nativeErrCode == ERR_ACCOUNT_COMMON_PERMISSION_DENIED) {
186         return ERR_JS_PERMISSION_DENIED;
187     } else {
188         return ERR_JS_SYSTEM_SERVICE_EXCEPTION;
189     }
190 }
191 
NativeErrMsg()192 std::string &NativeErrMsg()
193 {
194     thread_local static std::string nativeErrMsg;
195     return nativeErrMsg;
196 }
197 }  // namespace OHOS