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 empty string.";
38 constexpr const char* ERR_MSG_PERMISSION_NOT_EXIST = "The specified permission is not found.";
39 constexpr const char* ERR_MSG_DEVICE_ID_NOT_EXIST = "The specified deviceId is not found.";
40 constexpr const char* ERR_MSG_INSTALL_PARSE_FAILED = "Failed to install the hap since the hap fails to be parsed.";
41 constexpr const char* ERR_MSG_INSTALL_VERIFY_SIGNATURE_FAILED =
42 "Failed to install the hap since the hap signature fails to be verified.";
43 constexpr const char* ERR_MSG_INSTALL_HAP_FILEPATH_INVALID =
44 "Failed to install the hap since the path of the hap is invalid or too large size.";
45 constexpr const char* ERR_MSG_INSTALL_MULTIPLE_HAP_INFO_INCONSISTENT =
46 "Failed to install haps since the configuration information of multi haps is inconsistent.";
47 constexpr const char* ERR_MSG_INSTALL_NO_DISK_SPACE_LEFT =
48 "Failed to install the hap since the system disk space is insufficient.";
49 constexpr const char* ERR_MSG_INSTALL_VERSION_DOWNGRADE =
50 "Failed to install the hap since the version of the newly installed hap is too early.";
51 constexpr const char* ERR_MSG_INSTALL_DEPENDENT_MODULE_NOT_EXIST =
52 "Failed to install because the dependent module does not exist.";
53 constexpr const char* ERR_MSG_INSTALL_SHARE_APP_LIBRARY_NOT_ALLOWED =
54 "Failed to install because not allowed to share app library";
55 constexpr const char* ERR_MSG_UNINSTALL_PREINSTALL_APP_FAILED = "The preinstalled app cannot be uninstalled.";
56 constexpr const char* ERR_ZLIB_SRC_FILE_INVALID_MSG = "The Input source file is invalid.";
57 constexpr const char* ERR_ZLIB_DEST_FILE_INVALID_MSG = "The Input destination file is invalid.";
58 constexpr const char* ERR_MSG_PARAM_NUMBER_ERROR =
59 "BusinessError 401: Parameter error. The number of parameters is incorrect.";
60 constexpr const char* ERR_MSG_BUNDLE_SERVICE_EXCEPTION = "Bundle manager service is excepted.";
61 constexpr const char* ERROR_MSG_BUNDLE_IS_DISABLED = "The specified bundle is disabled.";
62 constexpr const char* ERROR_MSG_ABILITY_IS_DISABLED = "The specified ability is disabled.";
63 constexpr const char* ERROR_MSG_PROFILE_NOT_EXIST = "The specified profile is not found in the HAP.";
64 constexpr const char* ERROR_INVALID_UID_MSG = "The specified uid is invalid.";
65 constexpr const char* ERROR_INVALID_HAP_PATH_MSG = "The input source file is invalid.";
66 constexpr const char* ERROR_DEFAULT_APP_NOT_EXIST_MSG = "The specified default app does not exist.";
67 constexpr const char* ERROR_INVALID_TYPE_MSG = "The specified type is invalid.";
68 constexpr const char* ERROR_MSG_DISTRIBUTED_SERVICE_NOT_RUNNING = "The distributed service is not running.";
69 constexpr const char* ERROR_ABILITY_AND_TYPE_MISMATCH_MSG = "The specified ability and type do not match.";
70 constexpr const char* ERROR_MSG_CLEAR_CACHE_FILES_UNSUPPORTED =
71 "The specified bundle does not support clearing cache files.";
72 constexpr const char* ERROR_MSG_INSTALL_HAP_OVERLAY_CHECK_FAILED =
73 "Failed to install the HAP because the overlay check of the HAP is failed";
74 constexpr const char* ERROR_MSG_SPECIFIED_BUNDLE_NOT_OVERLAY_BUNDLE =
75 "The specified bundleName is not overlay bundle.";
76 constexpr const char* ERROR_MSG_SPECIFIED_MODULE_NOT_OVERLAY_MODULE =
77 "The specified moduleName is not overlay module.";
78 constexpr const char* ERROR_MSG_SPECIFIED_MODULE_IS_OVERLAY_MODULE =
79 "The specified moduleName is overlay module.";
80 constexpr const char* ERROR_MSG_SPECIFIED_BUNDLE_IS_OVERLAY_BUNDLE =
81 "The specified bundle is overlay bundle.";
82 constexpr const char* ERROR_MSG_SHARE_APP_LIBRARY_IS_RELIED =
83 "The specified shared library is dependened.";
84 constexpr const char* ERROR_MSG_SHARE_APP_LIBRARY_IS_NOT_EXIST =
85 "The specified shared library is not exist";
86 constexpr const char* ERR_MSG_UNINSTALL_SHARED_LIBRARY =
87 "The specified bundle is shared library";
88 constexpr const char* ERR_MSG_DISALLOW_INSTALL =
89 "Failed to install because enterprise device management disallow install";
90 constexpr const char* ERR_MSG_WRONG_PROXY_DATA_URI =
91 "The uri in data proxy is wrong";
92 constexpr const char* ERR_MSG_WRONG_PROXY_DATA_PERMISSION =
93 "The apl of required permission in non-system data proxy should be system_basic or system_core";
94 constexpr const char* ERR_MSG_WRONG_MODE_ISOLATION =
95 "Failed to install the HAP because the isolationMode configured is not supported";
96 constexpr const char* ERR_MSG_DISALLOW_UNINSTALL =
97 "Failed to uninstall because enterprise device management disallow uninstall";
98 constexpr const char* ERR_MSG_ALREADY_EXIST =
99 "Failed to install the HAP because the VersionCode to be updated is not greater than the current VersionCode";
100 constexpr const char* ERR_ZLIB_SRC_FILE_FORMAT_ERROR_OR_DAMAGED_MSG =
101 "The input source file is not ZIP format or damaged.";
102 constexpr const char* ERR_MSG_CODE_SIGNATURE_FAILED =
103 "The specified code-signature file or corresponding module are incorrect.";
104 constexpr const char* ERR_MSG_SELF_UPDATE_NOT_MDM =
105 "Failed to install the HAP because the distribution type of caller application is not enterprise_mdm.";
106 constexpr const char* ERR_MSG_SELF_UPDATE_BUNDLENAME_NOT_SAME =
107 "Failed to install the HAP because the bundleName is different from the bundleName of the caller application.";
108 constexpr const char* ERR_MSG_ENTERPRISE_BUNDLE_NOT_ALLOWED =
109 "Failed to install the HAP because enterprise normal/mdm bundle cannot be installed on non-enterprise device.";
110 constexpr const char* ERR_MSG_DEBUG_BUNDLE_NOT_ALLOWED =
111 "Failed to install the HAP because debug bundle cannot be installed under non-developer mode.";
112 constexpr const char* ERR_MSG_ERROR_VERIFY_ABC = "Failed to verify abc.";
113 constexpr const char* ERR_MSG_ERROR_DELETE_ABC = "Failed to delete abc.";
114 constexpr const char* ERROR_MSG_NOT_APP_GALLERY_CALL = "Not app gallery call.";
115 constexpr const char* ERROR_MSG_INSTALL_PERMISSION_CHECK_ERROR =
116 "Failed to install the HAP because the HAP requests wrong permissions.";
117
118 static std::unordered_map<int32_t, const char*> ERR_MSG_MAP = {
119 { ERROR_PERMISSION_DENIED_ERROR, ERR_MSG_PERMISSION_DENIED_ERROR },
120 { ERROR_NOT_SYSTEM_APP, ERR_MSG_NOT_SYSTEM_APP },
121 { ERROR_PARAM_CHECK_ERROR, ERR_MSG_PARAM_TYPE_ERROR },
122 { ERROR_SYSTEM_ABILITY_NOT_FOUND, ERR_MSG_ABILITY_NOT_SUPPORTED },
123 { ERROR_BUNDLE_NOT_EXIST, ERR_MSG_BUNDLE_NOT_EXIST },
124 { ERROR_MODULE_NOT_EXIST, ERR_MSG_MODULE_NOT_EXIST },
125 { ERROR_ABILITY_NOT_EXIST, ERR_MSG_ABILITY_NOT_EXIST },
126 { ERROR_INVALID_USER_ID, ERR_MSG_INVALID_USER_ID },
127 { ERROR_INVALID_APPID, ERR_MSG_APPID_NOT_EXIST },
128 { ERROR_PERMISSION_NOT_EXIST, ERR_MSG_PERMISSION_NOT_EXIST },
129 { ERROR_DEVICE_ID_NOT_EXIST, ERR_MSG_DEVICE_ID_NOT_EXIST },
130 { ERROR_INSTALL_PARSE_FAILED, ERR_MSG_INSTALL_PARSE_FAILED },
131 { ERROR_INSTALL_VERIFY_SIGNATURE_FAILED, ERR_MSG_INSTALL_VERIFY_SIGNATURE_FAILED },
132 { ERROR_INSTALL_HAP_FILEPATH_INVALID, ERR_MSG_INSTALL_HAP_FILEPATH_INVALID },
133 { ERROR_INSTALL_MULTIPLE_HAP_INFO_INCONSISTENT, ERR_MSG_INSTALL_MULTIPLE_HAP_INFO_INCONSISTENT },
134 { ERROR_INSTALL_NO_DISK_SPACE_LEFT, ERR_MSG_INSTALL_NO_DISK_SPACE_LEFT },
135 { ERROR_INSTALL_VERSION_DOWNGRADE, ERR_MSG_INSTALL_VERSION_DOWNGRADE },
136 { ERROR_INSTALL_DEPENDENT_MODULE_NOT_EXIST, ERR_MSG_INSTALL_DEPENDENT_MODULE_NOT_EXIST },
137 { ERROR_INSTALL_SHARE_APP_LIBRARY_NOT_ALLOWED, ERR_MSG_INSTALL_SHARE_APP_LIBRARY_NOT_ALLOWED },
138 { ERROR_UNINSTALL_PREINSTALL_APP_FAILED, ERR_MSG_UNINSTALL_PREINSTALL_APP_FAILED },
139 { ERROR_BUNDLE_SERVICE_EXCEPTION, ERR_MSG_BUNDLE_SERVICE_EXCEPTION },
140 { ERR_ZLIB_SRC_FILE_INVALID, ERR_ZLIB_SRC_FILE_INVALID_MSG },
141 { ERR_ZLIB_DEST_FILE_INVALID, ERR_ZLIB_DEST_FILE_INVALID_MSG },
142 { ERROR_BUNDLE_IS_DISABLED, ERROR_MSG_BUNDLE_IS_DISABLED },
143 { ERROR_ABILITY_IS_DISABLED, ERROR_MSG_ABILITY_IS_DISABLED },
144 { ERROR_PROFILE_NOT_EXIST, ERROR_MSG_PROFILE_NOT_EXIST },
145 { ERROR_INVALID_UID, ERROR_INVALID_UID_MSG },
146 { ERROR_INVALID_HAP_PATH, ERROR_INVALID_HAP_PATH_MSG },
147 { ERROR_DEFAULT_APP_NOT_EXIST, ERROR_DEFAULT_APP_NOT_EXIST_MSG },
148 { ERROR_INVALID_TYPE, ERROR_INVALID_TYPE_MSG },
149 { ERROR_DISTRIBUTED_SERVICE_NOT_RUNNING, ERROR_MSG_DISTRIBUTED_SERVICE_NOT_RUNNING },
150 { ERROR_ABILITY_AND_TYPE_MISMATCH, ERROR_ABILITY_AND_TYPE_MISMATCH_MSG },
151 { ERROR_CLEAR_CACHE_FILES_UNSUPPORTED, ERROR_MSG_CLEAR_CACHE_FILES_UNSUPPORTED },
152 { ERROR_INSTALL_HAP_OVERLAY_CHECK_FAILED, ERROR_MSG_INSTALL_HAP_OVERLAY_CHECK_FAILED },
153 { ERROR_SPECIFIED_MODULE_NOT_OVERLAY_MODULE, ERROR_MSG_SPECIFIED_MODULE_NOT_OVERLAY_MODULE },
154 { ERROR_SPECIFIED_BUNDLE_NOT_OVERLAY_BUNDLE, ERROR_MSG_SPECIFIED_BUNDLE_NOT_OVERLAY_BUNDLE },
155 { ERROR_SPECIFIED_MODULE_IS_OVERLAY_MODULE, ERROR_MSG_SPECIFIED_MODULE_IS_OVERLAY_MODULE },
156 { ERROR_SPECIFIED_BUNDLE_IS_OVERLAY_BUNDLE, ERROR_MSG_SPECIFIED_BUNDLE_IS_OVERLAY_BUNDLE },
157 { ERROR_UNINSTALL_SHARE_APP_LIBRARY_IS_RELIED, ERROR_MSG_SHARE_APP_LIBRARY_IS_RELIED },
158 { ERROR_UNINSTALL_SHARE_APP_LIBRARY_IS_NOT_EXIST, ERROR_MSG_SHARE_APP_LIBRARY_IS_NOT_EXIST },
159 { ERROR_UNINSTALL_BUNDLE_IS_SHARED_BUNDLE, ERR_MSG_UNINSTALL_SHARED_LIBRARY },
160 { ERROR_DISALLOW_INSTALL, ERR_MSG_DISALLOW_INSTALL },
161 { ERROR_INSTALL_WRONG_DATA_PROXY_URI, ERR_MSG_WRONG_PROXY_DATA_URI },
162 { ERROR_INSTALL_WRONG_DATA_PROXY_PERMISSION, ERR_MSG_WRONG_PROXY_DATA_PERMISSION },
163 { ERROR_INSTALL_WRONG_MODE_ISOLATION, ERR_MSG_WRONG_MODE_ISOLATION },
164 { ERROR_DISALLOW_UNINSTALL, ERR_MSG_DISALLOW_UNINSTALL },
165 { ERROR_INSTALL_ALREADY_EXIST, ERR_MSG_ALREADY_EXIST },
166 { ERR_ZLIB_SRC_FILE_FORMAT_ERROR_OR_DAMAGED, ERR_ZLIB_SRC_FILE_FORMAT_ERROR_OR_DAMAGED_MSG },
167 { ERROR_INSTALL_CODE_SIGNATURE_FAILED, ERR_MSG_CODE_SIGNATURE_FAILED },
168 { ERROR_INSTALL_SELF_UPDATE_NOT_MDM, ERR_MSG_SELF_UPDATE_NOT_MDM},
169 { ERROR_INSTALL_SELF_UPDATE_BUNDLENAME_NOT_SAME, ERR_MSG_SELF_UPDATE_BUNDLENAME_NOT_SAME},
170 { ERROR_INSTALL_ENTERPRISE_BUNDLE_NOT_ALLOWED, ERR_MSG_ENTERPRISE_BUNDLE_NOT_ALLOWED },
171 { ERROR_INSTALL_DEBUG_BUNDLE_NOT_ALLOWED, ERR_MSG_DEBUG_BUNDLE_NOT_ALLOWED},
172 { ERROR_VERIFY_ABC, ERR_MSG_ERROR_VERIFY_ABC},
173 { ERROR_NOT_APP_GALLERY_CALL, ERROR_MSG_NOT_APP_GALLERY_CALL},
174 { ERROR_DELETE_ABC, ERR_MSG_ERROR_DELETE_ABC},
175 { ERROR_INSTALL_PERMISSION_CHECK_ERROR, ERROR_MSG_INSTALL_PERMISSION_CHECK_ERROR},
176 };
177 } // namespace
178
ThrowError(napi_env env,int32_t err,const std::string & msg)179 void BusinessError::ThrowError(napi_env env, int32_t err, const std::string &msg)
180 {
181 napi_value error = CreateError(env, err, msg);
182 napi_throw(env, error);
183 }
184
ThrowParameterTypeError(napi_env env,int32_t err,const std::string & parameter,const std::string & type)185 void BusinessError::ThrowParameterTypeError(napi_env env, int32_t err,
186 const std::string ¶meter, const std::string &type)
187 {
188 napi_value error = CreateCommonError(env, err, parameter, type);
189 napi_throw(env, error);
190 }
191
ThrowTooFewParametersError(napi_env env,int32_t err)192 void BusinessError::ThrowTooFewParametersError(napi_env env, int32_t err)
193 {
194 ThrowError(env, err, ERR_MSG_PARAM_NUMBER_ERROR);
195 }
196
CreateError(napi_env env,int32_t err,const std::string & msg)197 napi_value BusinessError::CreateError(napi_env env, int32_t err, const std::string& msg)
198 {
199 napi_value businessError = nullptr;
200 napi_value errorCode = nullptr;
201 NAPI_CALL(env, napi_create_int32(env, err, &errorCode));
202 napi_value errorMessage = nullptr;
203 NAPI_CALL(env, napi_create_string_utf8(env, msg.c_str(), NAPI_AUTO_LENGTH, &errorMessage));
204 napi_create_error(env, nullptr, errorMessage, &businessError);
205 napi_set_named_property(env, businessError, "code", errorCode);
206 return businessError;
207 }
208
CreateCommonError(napi_env env,int32_t err,const std::string & functionName,const std::string & permissionName)209 napi_value BusinessError::CreateCommonError(
210 napi_env env, int32_t err, const std::string &functionName, const std::string &permissionName)
211 {
212 std::string errMessage = ERR_MSG_BUSINESS_ERROR;
213 auto iter = errMessage.find("$");
214 if (iter != std::string::npos) {
215 errMessage = errMessage.replace(iter, 1, std::to_string(err));
216 }
217 if (ERR_MSG_MAP.find(err) != ERR_MSG_MAP.end()) {
218 errMessage += ERR_MSG_MAP[err];
219 }
220 iter = errMessage.find("$");
221 if (iter != std::string::npos) {
222 errMessage = errMessage.replace(iter, 1, functionName);
223 iter = errMessage.find("$");
224 if (iter != std::string::npos) {
225 errMessage = errMessage.replace(iter, 1, permissionName);
226 }
227 }
228 return CreateError(env, err, errMessage);
229 }
230 } // AppExecFwk
231 } // OHOS