• 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         default:
118             return ERR_JS_SYSTEM_SERVICE_EXCEPTION;
119     }
120 }
121 
DomainAccountConvertToJSErrCode(int32_t errCode)122 static int32_t DomainAccountConvertToJSErrCode(int32_t errCode)
123 {
124     switch (errCode) {
125         case ERR_DOMAIN_ACCOUNT_SERVICE_PLUGIN_ALREADY_EXIST:
126             return ERR_JS_DOMAIN_PLUGIN_ALREADY_REGISTERED;
127         case ERR_DOMAIN_ACCOUNT_SERVICE_NOT_DOMAIN_ACCOUNT:
128             return ERR_JS_ACCOUNT_NOT_FOUND;
129         case ERR_DOMAIN_ACCOUNT_SERVICE_PLUGIN_NOT_EXIST:
130         case ERR_DOMAIN_ACCOUNT_NOT_SUPPORT:
131             return ERR_JS_CAPABILITY_NOT_SUPPORTED;
132         case ERR_DOMAIN_ACCOUNT_NOT_SUPPORT_BACKGROUND_ACCOUNT_REQUEST:
133             return ERR_JS_CAPABILITY_NOT_SUPPORTED;
134         default:
135             return ERR_JS_SYSTEM_SERVICE_EXCEPTION;
136     }
137 }
138 
IsAppAccountKitError(int32_t errCode)139 static bool IsAppAccountKitError(int32_t errCode)
140 {
141     return (errCode >= ERR_APPACCOUNT_KIT_GET_APP_ACCOUNT_SERVICE &&
142         errCode <= ERR_APPACCOUNT_KIT_READ_PARCELABLE_VECTOR_ACCOUNT_INFO);
143 }
144 
IsAppAccountServiceError(int32_t errCode)145 static bool IsAppAccountServiceError(int32_t errCode)
146 {
147     return (errCode >= ERR_APPACCOUNT_SERVICE_ACCOUNT_NOT_EXIST && errCode <= ERR_APPACCOUNT_SERVICE_OTHER);
148 }
149 
IsOsAccountKitError(int32_t errCode)150 static bool IsOsAccountKitError(int32_t errCode)
151 {
152     return (errCode >= ERR_OSACCOUNT_KIT_CREATE_OS_ACCOUNT_FOR_DOMAIN_ERROR &&
153         errCode <= ERR_OSACCOUNT_KIT_NO_SPECIFIED_SUBSCRIBER_HAS_BEEN_REGISTERED);
154 }
155 
IsOsAccountServiceError(int32_t errCode)156 static bool IsOsAccountServiceError(int32_t errCode)
157 {
158     return ((errCode >= ERR_OSACCOUNT_SERVICE_MANAGER_QUERY_DISTRIBUTE_DATA_ERROR) &&
159            (errCode <= ERR_OSACCOUNT_SERVICE_STORAGE_PREPARE_ADD_USER_FAILED)) ||
160            (errCode == ERR_ACCOUNT_COMMON_NAME_HAD_EXISTED) || (errCode == ERR_ACCOUNT_COMMON_SHORT_NAME_HAD_EXISTED);
161 }
162 
IsDomainAccountServiceError(int32_t errCode)163 static bool IsDomainAccountServiceError(int32_t errCode)
164 {
165     return (errCode >= ERR_DOMAIN_ACCOUNT_SERVICE_PLUGIN_ALREADY_EXIST) &&
166         (errCode <= ERR_DOMAIN_ACCOUNT_SERVICE_ERR_CODE_LIMIT);
167 }
168 
ConvertToJSErrCode(int32_t nativeErrCode)169 int32_t ConvertToJSErrCode(int32_t nativeErrCode)
170 {
171     auto it = errorMap.find(nativeErrCode);
172     if (it != errorMap.end()) {
173         return it->second;
174     }
175     if (IsAppAccountKitError(nativeErrCode) || IsAppAccountServiceError(nativeErrCode)) {
176         return AppAccountConvertToJSErrCode(nativeErrCode);
177     } else if (IsOsAccountKitError(nativeErrCode) || IsOsAccountServiceError(nativeErrCode)) {
178         return OsAccountConvertToJSErrCode(nativeErrCode);
179     } else if (IsDomainAccountServiceError(nativeErrCode)) {
180         return DomainAccountConvertToJSErrCode(nativeErrCode);
181     } else if (nativeErrCode == ERR_ACCOUNT_COMMON_PERMISSION_DENIED) {
182         return ERR_JS_PERMISSION_DENIED;
183     } else {
184         return ERR_JS_SYSTEM_SERVICE_EXCEPTION;
185     }
186 }
187 }  // namespace OHOS