• 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 "account_error_no.h"
17 #include <map>
18 
19 namespace OHOS {
20 const std::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_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR:
89             return ERR_JS_ACCOUNT_NOT_FOUND;
90         case ERR_OSACCOUNT_SERVICE_INNER_DOMAIN_ALREADY_BIND_ERROR:
91             return ERR_JS_ACCOUNT_ALREADY_EXIST;
92         case ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_ALREADY_ACTIVE_ERROR:
93             return ERR_JS_ACCOUNT_ALREADY_ACTIVATED;
94         case ERR_OSACCOUNT_SERVICE_CONTROL_MAX_CAN_CREATE_ERROR:
95             return ERR_JS_ACCOUNT_NUMBER_REACH_LIMIT;
96         case ERR_OSACCOUNT_SERVICE_MANAGER_NOT_ENABLE_MULTI_ERROR:
97             return ERR_JS_MULTI_USER_NOT_SUPPORT;
98         case ERR_OSACCOUNT_SERVICE_MANAGER_CREATE_OSACCOUNT_TYPE_ERROR:
99             return ERR_JS_ACCOUNT_TYPE_NOT_SUPPORT;
100         case ERR_OSACCOUNT_SERVICE_MANAGER_ID_ERROR:
101         case ERR_OSACCOUNT_SERVICE_CONTROL_CANNOT_DELETE_ID_ERROR:
102         case ERR_OSACCOUNT_SERVICE_CONTROL_ID_CANNOT_CREATE_ERROR:
103             return ERR_JS_ACCOUNT_RESTRICTED;
104         case ERR_OSACCOUNT_KIT_NO_SPECIFIED_SUBSCRIBER_HAS_BEEN_REGISTERED:
105             return ERR_JS_LISTENER_NOT_REGISTERED;
106         case ERR_ACCOUNT_COMMON_PERMISSION_DENIED:
107             return ERR_JS_PERMISSION_DENIED;
108         case ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_OPERATING_ERROR:
109             return ERR_JS_ACCOUNT_SERVICE_BUSY;
110         default:
111             return ERR_JS_SYSTEM_SERVICE_EXCEPTION;
112     }
113 }
114 
DomainAccountConvertToJSErrCode(int32_t errCode)115 static int32_t DomainAccountConvertToJSErrCode(int32_t errCode)
116 {
117     switch (errCode) {
118         case ERR_DOMAIN_ACCOUNT_SERVICE_PLUGIN_ALREADY_EXIST:
119             return ERR_JS_DOMAIN_PLUGIN_ALREADY_REGISTERED;
120         case ERR_DOMAIN_ACCOUNT_SERVICE_NOT_DOMAIN_ACCOUNT:
121             return ERR_JS_ACCOUNT_NOT_FOUND;
122         case ERR_DOMAIN_ACCOUNT_SERVICE_PLUGIN_NOT_EXIST:
123             return ERR_JS_CAPABILITY_NOT_SUPPORTED;
124         default:
125             return ERR_JS_SYSTEM_SERVICE_EXCEPTION;
126     }
127 }
128 
IsAppAccountKitError(int32_t errCode)129 static bool IsAppAccountKitError(int32_t errCode)
130 {
131     return (errCode >= ERR_APPACCOUNT_KIT_GET_APP_ACCOUNT_SERVICE &&
132         errCode <= ERR_APPACCOUNT_KIT_READ_PARCELABLE_VECTOR_ACCOUNT_INFO);
133 }
134 
IsAppAccountServiceError(int32_t errCode)135 static bool IsAppAccountServiceError(int32_t errCode)
136 {
137     return (errCode >= ERR_APPACCOUNT_SERVICE_ACCOUNT_NOT_EXIST && errCode <= ERR_APPACCOUNT_SERVICE_OTHER);
138 }
139 
IsOsAccountKitError(int32_t errCode)140 static bool IsOsAccountKitError(int32_t errCode)
141 {
142     return (errCode >= ERR_OSACCOUNT_KIT_CREATE_OS_ACCOUNT_FOR_DOMAIN_ERROR &&
143         errCode <= ERR_OSACCOUNT_KIT_NO_SPECIFIED_SUBSCRIBER_HAS_BEEN_REGISTERED);
144 }
145 
IsOsAccountServiceError(int32_t errCode)146 static bool IsOsAccountServiceError(int32_t errCode)
147 {
148     return (errCode >= ERR_OSACCOUNT_SERVICE_MANAGER_QUERY_DISTRIBUTE_DATA_ERROR) &&
149            (errCode <= ERR_OSACCOUNT_SERVICE_STORAGE_PREPARE_ADD_USER_FAILED);
150 }
151 
IsDomainAccountServiceError(int32_t errCode)152 static bool IsDomainAccountServiceError(int32_t errCode)
153 {
154     return (errCode >= ERR_DOMAIN_ACCOUNT_SERVICE_PLUGIN_ALREADY_EXIST) &&
155         (errCode <= ERR_DOMAIN_ACCOUNT_SERVICE_ERR_CODE_LIMIT);
156 }
157 
ConvertToJSErrCode(int32_t nativeErrCode)158 int32_t ConvertToJSErrCode(int32_t nativeErrCode)
159 {
160     auto it = errorMap.find(nativeErrCode);
161     if (it != errorMap.end()) {
162         return it->second;
163     }
164     if (IsAppAccountKitError(nativeErrCode) || IsAppAccountServiceError(nativeErrCode)) {
165         return AppAccountConvertToJSErrCode(nativeErrCode);
166     } else if (IsOsAccountKitError(nativeErrCode) || IsOsAccountServiceError(nativeErrCode)) {
167         return OsAccountConvertToJSErrCode(nativeErrCode);
168     } else if (IsDomainAccountServiceError(nativeErrCode)) {
169         return DomainAccountConvertToJSErrCode(nativeErrCode);
170     } else if (nativeErrCode == ERR_ACCOUNT_COMMON_PERMISSION_DENIED) {
171         return ERR_JS_PERMISSION_DENIED;
172     } else {
173         return ERR_JS_SYSTEM_SERVICE_EXCEPTION;
174     }
175 }
176 }  // namespace OHOS