• 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 "business_error.h"
17 
18 #include <unordered_map>
19 
20 #include "bundle_errors.h"
21 
22 namespace OHOS {
23 namespace AppExecFwk {
24 namespace {
25 constexpr const char* ERR_MSG_BUSINESS_ERROR = "BusinessError $: ";
26 constexpr const char* ERR_MSG_PERMISSION_DENIED_ERROR =
27     "Permission denied. An attempt was made to $ forbidden by permission: $.";
28 constexpr const char* ERR_MSG_NOT_SYSTEM_APP =
29     "Permission denied. Non-system APP calling system API";
30 constexpr const char* ERR_MSG_PARAM_TYPE_ERROR = "Parameter error. The type of $ must be $.";
31 constexpr const char* ERR_MSG_ABILITY_NOT_SUPPORTED =
32     "Capability not supported. Function $ can not work correctly due to limited device capabilities.";
33 constexpr const char* ERR_MSG_BUNDLE_NOT_EXIST = "The specified bundle is not found.";
34 constexpr const char* ERR_MSG_MODULE_NOT_EXIST = "The specified module is not found.";
35 constexpr const char* ERR_MSG_ABILITY_NOT_EXIST = "The specified ability is not found.";
36 constexpr const char* ERR_MSG_INVALID_USER_ID = "The specified user id is not found.";
37 constexpr const char* ERR_MSG_APPID_NOT_EXIST = "The specified appId is an empty string.";
38 constexpr const char* ERR_MSG_APPIDENTIFIER_NOT_EXIST = "The specified appIdentifier is an empty string.";
39 constexpr const char* ERR_MSG_PERMISSION_NOT_EXIST = "The specified permission is not found.";
40 constexpr const char* ERR_MSG_DEVICE_ID_NOT_EXIST = "The specified deviceId is not found.";
41 constexpr const char* ERR_MSG_INVALID_APP_INDEX = "The specified app index is invalid.";
42 constexpr const char* ERR_MSG_INSTALL_PARSE_FAILED = "Failed to install the hap since the hap fails to be parsed.";
43 constexpr const char* ERR_MSG_INSTALL_VERIFY_SIGNATURE_FAILED =
44     "Failed to install the hap since the hap signature fails to be verified.";
45 constexpr const char* ERR_MSG_INSTALL_HAP_FILEPATH_INVALID =
46     "Failed to install the hap since the path of the hap is invalid or too large size.";
47 constexpr const char* ERR_MSG_INSTALL_MULTIPLE_HAP_INFO_INCONSISTENT =
48     "Failed to install haps since the configuration information of multi haps is inconsistent.";
49 constexpr const char* ERR_MSG_INSTALL_NO_DISK_SPACE_LEFT =
50     "Failed to install the hap since the system disk space is insufficient.";
51 constexpr const char* ERR_MSG_INSTALL_VERSION_DOWNGRADE =
52     "Failed to install the hap since the version of the newly installed hap is too early.";
53 constexpr const char* ERR_MSG_INSTALL_DEPENDENT_MODULE_NOT_EXIST =
54     "Failed to install the HAP or HSP because the dependent module does not exist.";
55 constexpr const char* ERR_MSG_INSTALL_SHARE_APP_LIBRARY_NOT_ALLOWED =
56     "Failed to install the HSP due to the lack of required permission.";
57 constexpr const char* ERR_MSG_UNINSTALL_PREINSTALL_APP_FAILED = "The preinstalled app cannot be uninstalled.";
58 constexpr const char* ERR_MSG_BUNDLE_NOT_PREINSTALLED =
59     "Failed to uninstall updates because the HAP is not pre-installed.";
60 constexpr const char* ERR_ZLIB_SRC_FILE_INVALID_MSG = "The Input source file is invalid.";
61 constexpr const char* ERR_ZLIB_DEST_FILE_INVALID_MSG = "The Input destination file is invalid.";
62 constexpr const char* ERR_MSG_PARAM_NUMBER_ERROR =
63     "BusinessError 401: Parameter error. The number of parameters is incorrect.";
64 constexpr const char* ERR_MSG_BUNDLE_SERVICE_EXCEPTION = "Bundle manager service is excepted.";
65 constexpr const char* ERROR_MSG_BUNDLE_IS_DISABLED = "The specified bundle is disabled.";
66 constexpr const char* ERROR_MSG_ABILITY_IS_DISABLED = "The specified ability is disabled.";
67 constexpr const char* ERROR_MSG_PROFILE_NOT_EXIST = "The specified profile is not found in the HAP.";
68 constexpr const char* ERROR_INVALID_UID_MSG = "The specified uid is invalid.";
69 constexpr const char* ERROR_INVALID_HAP_PATH_MSG = "The input source file is invalid.";
70 constexpr const char* ERROR_DEFAULT_APP_NOT_EXIST_MSG = "The specified default app does not exist.";
71 constexpr const char* ERROR_INVALID_TYPE_MSG = "The specified type is invalid.";
72 constexpr const char* ERROR_MSG_DISTRIBUTED_SERVICE_NOT_RUNNING = "The distributed service is not running.";
73 constexpr const char* ERROR_ABILITY_AND_TYPE_MISMATCH_MSG = "The specified ability and type do not match.";
74 constexpr const char* ERROR_MSG_CLEAR_CACHE_FILES_UNSUPPORTED =
75     "The specified bundle does not support clearing cache files.";
76 constexpr const char* ERROR_MSG_INSTALL_HAP_OVERLAY_CHECK_FAILED =
77     "Failed to install the HAP because the overlay check of the HAP failed.";
78 constexpr const char* ERROR_MSG_SPECIFIED_BUNDLE_NOT_OVERLAY_BUNDLE =
79     "The specified bundleName is not overlay bundle.";
80 constexpr const char* ERROR_MSG_SPECIFIED_MODULE_NOT_OVERLAY_MODULE =
81     "The specified moduleName is not overlay module.";
82 constexpr const char* ERROR_MSG_SPECIFIED_MODULE_IS_OVERLAY_MODULE =
83     "The specified moduleName is overlay module.";
84 constexpr const char* ERROR_MSG_SPECIFIED_BUNDLE_IS_OVERLAY_BUNDLE =
85     "The specified bundle is overlay bundle.";
86 constexpr const char* ERROR_MSG_SHARE_APP_LIBRARY_IS_RELIED =
87     "The version of the shared bundle is dependent on other applications.";
88 constexpr const char* ERROR_MSG_SHARE_APP_LIBRARY_IS_NOT_EXIST =
89     "The specified shared library is not exist";
90 constexpr const char* ERR_MSG_UNINSTALL_SHARED_LIBRARY =
91     "The specified bundle is shared library";
92 constexpr const char* ERR_MSG_DISALLOW_INSTALL =
93     "Failed to install the HAP because the installation is forbidden by enterprise device management.";
94 constexpr const char* ERR_MSG_WRONG_PROXY_DATA_URI =
95     "The uri in data proxy is wrong";
96 constexpr const char* ERR_MSG_WRONG_PROXY_DATA_PERMISSION =
97     "The apl of required permission in non-system data proxy should be system_basic or system_core";
98 constexpr const char* ERR_MSG_WRONG_MODE_ISOLATION =
99     "Failed to install the HAP because the isolationMode configured is not supported";
100 constexpr const char* ERR_MSG_DISALLOW_UNINSTALL =
101     "Failed to uninstall the HAP because the uninstall is forbidden by enterprise device management.";
102 constexpr const char* ERR_MSG_ALREADY_EXIST =
103     "Failed to install the HAP because the VersionCode to be updated is not greater than the current VersionCode";
104 constexpr const char* ERR_ZLIB_SRC_FILE_FORMAT_ERROR_OR_DAMAGED_MSG =
105     "The input source file is not in ZIP format or is damaged.";
106 constexpr const char* ERR_MSG_CODE_SIGNATURE_FAILED =
107     "Failed to install the HAP because the code signature verification failed.";
108 constexpr const char* ERR_MSG_SELF_UPDATE_NOT_MDM =
109     "Failed to install the HAP because the distribution type of the caller application is not enterprise_mdm.";
110 constexpr const char* ERR_MSG_SELF_UPDATE_BUNDLENAME_NOT_SAME =
111     "Failed to install the HAP because the bundleName is different from the bundleName of the caller application.";
112 constexpr const char* ERR_MSG_ENTERPRISE_BUNDLE_NOT_ALLOWED =
113     "Failed to install the HAP because an enterprise normal/MDM bundle cannot be installed on non-enterprise devices.";
114 constexpr const char* ERR_MSG_INSTALL_EXISTED_ENTERPRISE_BUNDLE_NOT_ALLOWED =
115     "It is not allowed to install the enterprise bundle.";
116 constexpr const char* ERR_MSG_DEBUG_BUNDLE_NOT_ALLOWED =
117     "Failed to install the HAP because a debug bundle can be installed only in developer mode.";
118 constexpr const char* ERR_MSG_ERROR_VERIFY_ABC = "Failed to verify the abc file.";
119 constexpr const char* ERR_MSG_ERROR_DELETE_ABC = "Failed to delete the abc file.";
120 constexpr const char* ERR_MSG_ERROR_EXT_RESOURCE_ADD_ERROR = "Failed to add extended resources.";
121 constexpr const char* ERR_MSG_ERROR_EXT_RESOURCE_REMOVE_ERROR = "Failed to remove extended resources.";
122 constexpr const char* ERR_MSG_ERROR_EXT_RESOURCE_GET_ERROR = "Failed to obtain extended resources.";
123 constexpr const char* ERR_MSG_ERROR_DYNAMIC_ICON_ENABLE_ERROR = "Failed to enable the dynamic icon.";
124 constexpr const char* ERR_MSG_ERROR_DYNAMIC_ICON_DISABLE_ERROR = "Failed to disable the dynamic icon.";
125 constexpr const char* ERR_MSG_ERROR_DYNAMIC_ICON_GET_ERROR = "Failed to obtain the dynamic icon.";
126 constexpr const char* ERROR_MSG_NOT_APP_GALLERY_CALL = "The caller is not AppGallery.";
127 constexpr const char* ERROR_MSG_INSTALL_PERMISSION_CHECK_ERROR =
128     "Failed to install the HAP because the HAP requests wrong permissions.";
129 constexpr const char* ERR_MSG_INVALID_LINK = "The specified link is invalid.";
130 constexpr const char* ERR_MSG_SCHEME_NOT_IN_QUERYSCHEMES =
131     "The scheme of the specified link is not in the querySchemes.";
132 constexpr const char* ERR_MSG_INVALID_DEVELOPER_ID =
133     "The specified developerId is invalid.";
134 constexpr const char* ERR_MSG_ENUM_ERROR =
135     "Parameter error. The value of $ is not a valid enum $.";
136 constexpr const char* ERR_MSG_BUNDLE_CAN_NOT_BE_UNINSTALLED =
137     "The specified application cannot be uninstalled.";
138 constexpr const char* ERR_MSG_START_SHORTCUT =
139     "The ability specified by want in the ShortcutInfo struct cannot be started.";
140 constexpr const char* ERR_MSG_INSTALL_FAILED_CONTROLLED =
141     "Failed to install the HAP because the device has been controlled.";
142 constexpr const char* ERR_MSG_NATIVE_INSTALL_FAILED =
143     "Failed to install the HAP because installing the native package failed.";
144 constexpr const char* ERR_MSG_NATIVE_UNINSTALL_FAILED =
145     "Failed to uninstall the HAP because uninstalling the native package failed.";
146 constexpr const char* ERR_MSG_INVALID_APPINDEX =
147     "The appIndex is invalid.";
148 constexpr const char* ERROR_MSG_LOCKED_APPLICATION_UNINSTALL =
149     "Failed to uninstall the app because the app is locked.";
150 constexpr const char* ERR_MSG_APP_NOT_SUPPORTED_MULTI_TYPE =
151     "The app does not support the creation of an appClone instance.";
152 constexpr const char* ERR_MSG_SHORTCUT_ID_ILLEGAL =
153     "The specified shortcut id is illegal.";
154 constexpr const char* ERR_MSG_INSTALL_FAILED_INCONSISTENT_SIGNATURE =
155     "Failed to install the HAP because an application with the same bundle name "
156     "but different signature information exists on the device.";
157 constexpr const char* ERR_MSG_INVALID_UNINSTALL_RULE =
158     "The specified bundleName of want is not the same with caller.";
159 constexpr const char* ERR_MSG_APP_DISTRIBUTION_TYPE_NOT_ALLOW_INSTALL =
160     "Failed to install the HAP or HSP because the app distribution type is not allowed.";
161 constexpr const char* ERR_MSG_INSTALL_FAILED_AND_RESTORE_TO_PREINSTALLED =
162     "Failed to install the HAP and restore to preinstalled bundle.";
163 constexpr const char* ERR_MSG_PLUGIN_ID_CHECK_ERROR =
164     "Failed to install the plugin because the plugin id fails to be verified.";
165 constexpr const char* ERR_MSG_CHECK_SUPPORT_PERMISSION =
166     "Failed to install the plugin because the host application lacks ohos.permission.kernel.SUPPORT_PLUGIN.";
167 constexpr const char* ERROR_MSG_DEVICE_NOT_SUPPORT_PLUGIN =
168     "Failed to install the plugin because the current device does not support plugin.";
169 constexpr const char* ERROR_MSG_PLUGIN_ID_PARSE_ERROR =
170     "Failed to install the plugin because the plugin id fails to be parsed.";
171 constexpr const char* ERROR_MSG_PLUGIN_NOT_FOUND =
172     "Failed to uninstall the plugin because the specified plugin is not found.";
173 
174 static std::unordered_map<int32_t, const char*> ERR_MSG_MAP = {
175     { ERROR_PERMISSION_DENIED_ERROR, ERR_MSG_PERMISSION_DENIED_ERROR },
176     { ERROR_NOT_SYSTEM_APP, ERR_MSG_NOT_SYSTEM_APP },
177     { ERROR_PARAM_CHECK_ERROR, ERR_MSG_PARAM_TYPE_ERROR },
178     { ERROR_SYSTEM_ABILITY_NOT_FOUND, ERR_MSG_ABILITY_NOT_SUPPORTED },
179     { ERROR_BUNDLE_NOT_EXIST, ERR_MSG_BUNDLE_NOT_EXIST },
180     { ERROR_MODULE_NOT_EXIST, ERR_MSG_MODULE_NOT_EXIST },
181     { ERROR_ABILITY_NOT_EXIST, ERR_MSG_ABILITY_NOT_EXIST },
182     { ERROR_INVALID_USER_ID, ERR_MSG_INVALID_USER_ID },
183     { ERROR_INVALID_APPID, ERR_MSG_APPID_NOT_EXIST },
184     { ERROR_INVALID_APPIDENTIFIER, ERR_MSG_APPIDENTIFIER_NOT_EXIST },
185     { ERROR_INVALID_APPINDEX, ERR_MSG_INVALID_APP_INDEX },
186     { ERROR_PERMISSION_NOT_EXIST, ERR_MSG_PERMISSION_NOT_EXIST },
187     { ERROR_DEVICE_ID_NOT_EXIST, ERR_MSG_DEVICE_ID_NOT_EXIST },
188     { ERROR_INSTALL_PARSE_FAILED, ERR_MSG_INSTALL_PARSE_FAILED },
189     { ERROR_INSTALL_VERIFY_SIGNATURE_FAILED, ERR_MSG_INSTALL_VERIFY_SIGNATURE_FAILED },
190     { ERROR_INSTALL_HAP_FILEPATH_INVALID, ERR_MSG_INSTALL_HAP_FILEPATH_INVALID },
191     { ERROR_INSTALL_MULTIPLE_HAP_INFO_INCONSISTENT, ERR_MSG_INSTALL_MULTIPLE_HAP_INFO_INCONSISTENT },
192     { ERROR_INSTALL_NO_DISK_SPACE_LEFT, ERR_MSG_INSTALL_NO_DISK_SPACE_LEFT },
193     { ERROR_INSTALL_VERSION_DOWNGRADE, ERR_MSG_INSTALL_VERSION_DOWNGRADE },
194     { ERROR_INSTALL_DEPENDENT_MODULE_NOT_EXIST, ERR_MSG_INSTALL_DEPENDENT_MODULE_NOT_EXIST },
195     { ERROR_INSTALL_SHARE_APP_LIBRARY_NOT_ALLOWED, ERR_MSG_INSTALL_SHARE_APP_LIBRARY_NOT_ALLOWED },
196     { ERROR_UNINSTALL_PREINSTALL_APP_FAILED, ERR_MSG_UNINSTALL_PREINSTALL_APP_FAILED },
197     { ERROR_BUNDLE_NOT_PREINSTALLED, ERR_MSG_BUNDLE_NOT_PREINSTALLED },
198     { ERROR_BUNDLE_SERVICE_EXCEPTION, ERR_MSG_BUNDLE_SERVICE_EXCEPTION },
199     { ERR_ZLIB_SRC_FILE_INVALID, ERR_ZLIB_SRC_FILE_INVALID_MSG },
200     { ERR_ZLIB_DEST_FILE_INVALID, ERR_ZLIB_DEST_FILE_INVALID_MSG },
201     { ERROR_BUNDLE_IS_DISABLED, ERROR_MSG_BUNDLE_IS_DISABLED },
202     { ERROR_ABILITY_IS_DISABLED, ERROR_MSG_ABILITY_IS_DISABLED },
203     { ERROR_PROFILE_NOT_EXIST, ERROR_MSG_PROFILE_NOT_EXIST },
204     { ERROR_INVALID_UID, ERROR_INVALID_UID_MSG },
205     { ERROR_INVALID_HAP_PATH, ERROR_INVALID_HAP_PATH_MSG },
206     { ERROR_DEFAULT_APP_NOT_EXIST, ERROR_DEFAULT_APP_NOT_EXIST_MSG },
207     { ERROR_INVALID_TYPE, ERROR_INVALID_TYPE_MSG },
208     { ERROR_DISTRIBUTED_SERVICE_NOT_RUNNING, ERROR_MSG_DISTRIBUTED_SERVICE_NOT_RUNNING },
209     { ERROR_ABILITY_AND_TYPE_MISMATCH, ERROR_ABILITY_AND_TYPE_MISMATCH_MSG },
210     { ERROR_CLEAR_CACHE_FILES_UNSUPPORTED, ERROR_MSG_CLEAR_CACHE_FILES_UNSUPPORTED },
211     { ERROR_INSTALL_HAP_OVERLAY_CHECK_FAILED, ERROR_MSG_INSTALL_HAP_OVERLAY_CHECK_FAILED },
212     { ERROR_SPECIFIED_MODULE_NOT_OVERLAY_MODULE, ERROR_MSG_SPECIFIED_MODULE_NOT_OVERLAY_MODULE },
213     { ERROR_SPECIFIED_BUNDLE_NOT_OVERLAY_BUNDLE, ERROR_MSG_SPECIFIED_BUNDLE_NOT_OVERLAY_BUNDLE },
214     { ERROR_SPECIFIED_MODULE_IS_OVERLAY_MODULE, ERROR_MSG_SPECIFIED_MODULE_IS_OVERLAY_MODULE },
215     { ERROR_SPECIFIED_BUNDLE_IS_OVERLAY_BUNDLE, ERROR_MSG_SPECIFIED_BUNDLE_IS_OVERLAY_BUNDLE },
216     { ERROR_UNINSTALL_SHARE_APP_LIBRARY_IS_RELIED, ERROR_MSG_SHARE_APP_LIBRARY_IS_RELIED },
217     { ERROR_UNINSTALL_SHARE_APP_LIBRARY_IS_NOT_EXIST, ERROR_MSG_SHARE_APP_LIBRARY_IS_NOT_EXIST },
218     { ERROR_UNINSTALL_BUNDLE_IS_SHARED_BUNDLE, ERR_MSG_UNINSTALL_SHARED_LIBRARY },
219     { ERROR_DISALLOW_INSTALL, ERR_MSG_DISALLOW_INSTALL },
220     { ERROR_INSTALL_WRONG_DATA_PROXY_URI, ERR_MSG_WRONG_PROXY_DATA_URI },
221     { ERROR_INSTALL_WRONG_DATA_PROXY_PERMISSION, ERR_MSG_WRONG_PROXY_DATA_PERMISSION },
222     { ERROR_INSTALL_WRONG_MODE_ISOLATION, ERR_MSG_WRONG_MODE_ISOLATION },
223     { ERROR_DISALLOW_UNINSTALL, ERR_MSG_DISALLOW_UNINSTALL },
224     { ERROR_INSTALL_ALREADY_EXIST, ERR_MSG_ALREADY_EXIST },
225     { ERR_ZLIB_SRC_FILE_FORMAT_ERROR_OR_DAMAGED, ERR_ZLIB_SRC_FILE_FORMAT_ERROR_OR_DAMAGED_MSG },
226     { ERROR_INSTALL_CODE_SIGNATURE_FAILED, ERR_MSG_CODE_SIGNATURE_FAILED },
227     { ERROR_INSTALL_SELF_UPDATE_NOT_MDM, ERR_MSG_SELF_UPDATE_NOT_MDM},
228     { ERROR_INSTALL_SELF_UPDATE_BUNDLENAME_NOT_SAME, ERR_MSG_SELF_UPDATE_BUNDLENAME_NOT_SAME},
229     { ERROR_INSTALL_ENTERPRISE_BUNDLE_NOT_ALLOWED, ERR_MSG_ENTERPRISE_BUNDLE_NOT_ALLOWED },
230     { ERROR_INSTALL_EXISTED_ENTERPRISE_NOT_ALLOWED_ERROR, ERR_MSG_INSTALL_EXISTED_ENTERPRISE_BUNDLE_NOT_ALLOWED },
231     { ERROR_INSTALL_DEBUG_BUNDLE_NOT_ALLOWED, ERR_MSG_DEBUG_BUNDLE_NOT_ALLOWED},
232     { ERROR_VERIFY_ABC, ERR_MSG_ERROR_VERIFY_ABC},
233     { ERROR_NOT_APP_GALLERY_CALL, ERROR_MSG_NOT_APP_GALLERY_CALL},
234     { ERROR_DELETE_ABC, ERR_MSG_ERROR_DELETE_ABC},
235     { ERROR_ADD_EXTEND_RESOURCE, ERR_MSG_ERROR_EXT_RESOURCE_ADD_ERROR},
236     { ERROR_REMOVE_EXTEND_RESOURCE, ERR_MSG_ERROR_EXT_RESOURCE_REMOVE_ERROR},
237     { ERROR_GET_EXTEND_RESOURCE, ERR_MSG_ERROR_EXT_RESOURCE_GET_ERROR},
238     { ERROR_ENABLE_DYNAMIC_ICON, ERR_MSG_ERROR_DYNAMIC_ICON_ENABLE_ERROR},
239     { ERROR_DISABLE_DYNAMIC_ICON, ERR_MSG_ERROR_DYNAMIC_ICON_DISABLE_ERROR},
240     { ERROR_GET_DYNAMIC_ICON, ERR_MSG_ERROR_DYNAMIC_ICON_GET_ERROR},
241     { ERROR_INSTALL_PERMISSION_CHECK_ERROR, ERROR_MSG_INSTALL_PERMISSION_CHECK_ERROR},
242     { ERROR_INVALID_LINK, ERR_MSG_INVALID_LINK },
243     { ERROR_SCHEME_NOT_IN_QUERYSCHEMES, ERR_MSG_SCHEME_NOT_IN_QUERYSCHEMES },
244     { ERROR_INVALID_DEVELOPERID, ERR_MSG_INVALID_DEVELOPER_ID },
245     { ERROR_BUNDLE_CAN_NOT_BE_UNINSTALLED, ERR_MSG_BUNDLE_CAN_NOT_BE_UNINSTALLED },
246     { ERROR_START_SHORTCUT_ERROR, ERR_MSG_START_SHORTCUT },
247     { ERROR_INSTALL_FAILED_CONTROLLED, ERR_MSG_INSTALL_FAILED_CONTROLLED },
248     { ERROR_INSTALL_NATIVE_FAILED, ERR_MSG_NATIVE_INSTALL_FAILED },
249     { ERROR_UNINSTALL_NATIVE_FAILED, ERR_MSG_NATIVE_UNINSTALL_FAILED },
250     { ERROR_INVALID_APPINDEX, ERR_MSG_INVALID_APPINDEX },
251     { ERROR_APPLICATION_UNINSTALL, ERROR_MSG_LOCKED_APPLICATION_UNINSTALL },
252     { ERROR_APP_NOT_SUPPORTED_MULTI_TYPE, ERR_MSG_APP_NOT_SUPPORTED_MULTI_TYPE },
253     { ERROR_SHORTCUT_ID_ILLEGAL_ERROR, ERR_MSG_SHORTCUT_ID_ILLEGAL },
254     { ERROR_INSTALL_FAILED_INCONSISTENT_SIGNATURE, ERR_MSG_INSTALL_FAILED_INCONSISTENT_SIGNATURE },
255     { ERROR_INVALID_UNINSTALL_RULE, ERR_MSG_INVALID_UNINSTALL_RULE },
256     { ERROR_APP_DISTRIBUTION_TYPE_NOT_ALLOW_INSTALL, ERR_MSG_APP_DISTRIBUTION_TYPE_NOT_ALLOW_INSTALL },
257     { ERROR_INSTALL_FAILED_AND_RESTORE_TO_PREINSTALLED, ERR_MSG_INSTALL_FAILED_AND_RESTORE_TO_PREINSTALLED },
258     { ERROR_PLUGIN_ID_CHECK_ERROR, ERR_MSG_PLUGIN_ID_CHECK_ERROR},
259     { ERROR_CHECK_SUPPORT_PERMISSION, ERR_MSG_CHECK_SUPPORT_PERMISSION},
260     { ERROR_DEVICE_NOT_SUPPORT_PLUGIN, ERROR_MSG_DEVICE_NOT_SUPPORT_PLUGIN},
261     { ERROR_PLUGIN_ID_PARSE_ERROR, ERROR_MSG_PLUGIN_ID_PARSE_ERROR },
262     { ERROR_PLUGIN_NOT_FOUND, ERROR_MSG_PLUGIN_NOT_FOUND }
263 };
264 } // namespace
265 
ThrowError(napi_env env,int32_t err,const std::string & msg)266 void BusinessError::ThrowError(napi_env env, int32_t err, const std::string &msg)
267 {
268     napi_value error = CreateError(env, err, msg);
269     napi_throw(env, error);
270 }
271 
ThrowParameterTypeError(napi_env env,int32_t err,const std::string & parameter,const std::string & type)272 void BusinessError::ThrowParameterTypeError(napi_env env, int32_t err,
273     const std::string &parameter, const std::string &type)
274 {
275     napi_value error = CreateCommonError(env, err, parameter, type);
276     napi_throw(env, error);
277 }
278 
ThrowTooFewParametersError(napi_env env,int32_t err)279 void BusinessError::ThrowTooFewParametersError(napi_env env, int32_t err)
280 {
281     ThrowError(env, err, ERR_MSG_PARAM_NUMBER_ERROR);
282 }
283 
CreateError(napi_env env,int32_t err,const std::string & msg)284 napi_value BusinessError::CreateError(napi_env env, int32_t err, const std::string& msg)
285 {
286     napi_value businessError = nullptr;
287     napi_value errorCode = nullptr;
288     NAPI_CALL(env, napi_create_int32(env, err, &errorCode));
289     napi_value errorMessage = nullptr;
290     NAPI_CALL(env, napi_create_string_utf8(env, msg.c_str(), NAPI_AUTO_LENGTH, &errorMessage));
291     napi_create_error(env, nullptr, errorMessage, &businessError);
292     napi_set_named_property(env, businessError, "code", errorCode);
293     return businessError;
294 }
295 
CreateCommonError(napi_env env,int32_t err,const std::string & functionName,const std::string & permissionName)296 napi_value BusinessError::CreateCommonError(
297     napi_env env, int32_t err, const std::string &functionName, const std::string &permissionName)
298 {
299     std::string errMessage = ERR_MSG_BUSINESS_ERROR;
300     auto iter = errMessage.find("$");
301     if (iter != std::string::npos) {
302         errMessage = errMessage.replace(iter, 1, std::to_string(err));
303     }
304     if (ERR_MSG_MAP.find(err) != ERR_MSG_MAP.end()) {
305         errMessage += ERR_MSG_MAP[err];
306     }
307     iter = errMessage.find("$");
308     if (iter != std::string::npos) {
309         errMessage = errMessage.replace(iter, 1, functionName);
310         iter = errMessage.find("$");
311         if (iter != std::string::npos) {
312             errMessage = errMessage.replace(iter, 1, permissionName);
313         }
314     }
315     return CreateError(env, err, errMessage);
316 }
317 
ThrowEnumError(napi_env env,const std::string & parameter,const std::string & type)318 void BusinessError::ThrowEnumError(napi_env env,
319     const std::string &parameter, const std::string &type)
320 {
321     napi_value error = CreateEnumError(env, parameter, type);
322     napi_throw(env, error);
323 }
324 
CreateEnumError(napi_env env,const std::string & parameter,const std::string & enumClass)325 napi_value BusinessError::CreateEnumError(napi_env env,
326     const std::string &parameter, const std::string &enumClass)
327 {
328     std::string errMessage = ERR_MSG_BUSINESS_ERROR;
329     auto iter = errMessage.find("$");
330     if (iter != std::string::npos) {
331         errMessage = errMessage.replace(iter, 1, std::to_string(ERROR_PARAM_CHECK_ERROR));
332     }
333     errMessage += ERR_MSG_ENUM_ERROR;
334     iter = errMessage.find("$");
335     if (iter != std::string::npos) {
336         errMessage = errMessage.replace(iter, 1, parameter);
337         iter = errMessage.find("$");
338         if (iter != std::string::npos) {
339             errMessage = errMessage.replace(iter, 1, enumClass);
340         }
341     }
342     return CreateError(env, ERROR_PARAM_CHECK_ERROR, errMessage);
343 }
344 } // AppExecFwk
345 } // OHOS
346