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_HAP_SIZE_TOO_LARGE =
46 "Failed to install the hap since the hap is too large.";
47 constexpr const char* ERR_MSG_INSTALL_INCORRECT_SUFFIX =
48 "Failed to install the hap since the extension name of the hap is not .hap.";
49 constexpr const char* ERR_MSG_INSTALL_MULTIPLE_HAP_INFO_INCONSISTENT =
50 "Failed to install haps since the configuration information of multi haps is inconsistent.";
51 constexpr const char* ERR_MSG_INSTALL_NO_DISK_SPACE_LEFT =
52 "Failed to install the hap since the system disk space is insufficient.";
53 constexpr const char* ERR_MSG_INSTALL_VERSION_DOWNGRADE =
54 "Failed to install the hap since the version of the newly installed hap is too early.";
55 constexpr const char* ERR_MSG_INSTALL_DEPENDENT_MODULE_NOT_EXIST =
56 "Failed to install because the dependent module does not exist.";
57 constexpr const char* ERR_MSG_UNINSTALL_PREINSTALL_APP_FAILED = "The preinstalled app cannot be uninstalled.";
58 constexpr const char* ERR_ZLIB_SRC_FILE_INVALID_MSG = "The Input source file is invalid.";
59 constexpr const char* ERR_ZLIB_DEST_FILE_INVALID_MSG = "The Input destination file is invalid.";
60 constexpr const char* ERR_MSG_PARAM_NUMBER_ERROR =
61 "BusinessError 401: Parameter error. The number of parameters is incorrect.";
62 constexpr const char* ERR_MSG_BUNDLE_SERVICE_EXCEPTION = "Bundle manager service is excepted.";
63 constexpr const char* ERROR_MSG_BUNDLE_IS_DISABLED = "The specified bundle is disabled.";
64 constexpr const char* ERROR_MSG_ABILITY_IS_DISABLED = "The specified ability is disabled.";
65 constexpr const char* ERROR_MSG_PROFILE_NOT_EXIST = "No profile in the hap.";
66 constexpr const char* ERROR_INVALID_UID_MSG = "The specified uid is invalid.";
67 constexpr const char* ERROR_INVALID_HAP_PATH_MSG = "The input source file is invalid.";
68 constexpr const char* ERROR_DEFAULT_APP_NOT_EXIST_MSG = "The specified default app does not exist.";
69 constexpr const char* ERROR_INVALID_TYPE_MSG = "The specified type is invalid.";
70 constexpr const char* ERROR_MSG_DISTRIBUTED_SERVICE_NOT_RUNNING = "The distributed service is not running.";
71 constexpr const char* ERROR_ABILITY_AND_TYPE_MISMATCH_MSG = "The specified ability and type do not match.";
72 constexpr const char* ERROR_MSG_CLEAR_CACHE_FILES_UNSUPPORTED =
73 "The specified bundle does not support clearing cache files.";
74 static std::unordered_map<int32_t, const char*> ERR_MSG_MAP = {
75 { ERROR_PERMISSION_DENIED_ERROR, ERR_MSG_PERMISSION_DENIED_ERROR },
76 { ERROR_NOT_SYSTEM_APP, ERR_MSG_NOT_SYSTEM_APP},
77 { ERROR_PARAM_CHECK_ERROR, ERR_MSG_PARAM_TYPE_ERROR },
78 { ERROR_SYSTEM_ABILITY_NOT_FOUND, ERR_MSG_ABILITY_NOT_SUPPORTED },
79 { ERROR_BUNDLE_NOT_EXIST, ERR_MSG_BUNDLE_NOT_EXIST },
80 { ERROR_MODULE_NOT_EXIST, ERR_MSG_MODULE_NOT_EXIST },
81 { ERROR_ABILITY_NOT_EXIST, ERR_MSG_ABILITY_NOT_EXIST },
82 { ERROR_INVALID_USER_ID, ERR_MSG_INVALID_USER_ID },
83 { ERROR_INVALID_APPID, ERR_MSG_APPID_NOT_EXIST },
84 { ERROR_PERMISSION_NOT_EXIST, ERR_MSG_PERMISSION_NOT_EXIST },
85 { ERROR_DEVICE_ID_NOT_EXIST, ERR_MSG_DEVICE_ID_NOT_EXIST },
86 { ERROR_INSTALL_PARSE_FAILED, ERR_MSG_INSTALL_PARSE_FAILED },
87 { ERROR_INSTALL_VERIFY_SIGNATURE_FAILED, ERR_MSG_INSTALL_VERIFY_SIGNATURE_FAILED },
88 { ERROR_INSTALL_HAP_FILEPATH_INVALID, ERR_MSG_INSTALL_HAP_FILEPATH_INVALID },
89 { ERROR_INSTALL_HAP_SIZE_TOO_LARGE, ERR_MSG_INSTALL_HAP_SIZE_TOO_LARGE },
90 { ERROR_INSTALL_INCORRECT_SUFFIX, ERR_MSG_INSTALL_INCORRECT_SUFFIX },
91 { ERROR_INSTALL_MULTIPLE_HAP_INFO_INCONSISTENT, ERR_MSG_INSTALL_MULTIPLE_HAP_INFO_INCONSISTENT },
92 { ERROR_INSTALL_NO_DISK_SPACE_LEFT, ERR_MSG_INSTALL_NO_DISK_SPACE_LEFT },
93 { ERROR_INSTALL_VERSION_DOWNGRADE, ERR_MSG_INSTALL_VERSION_DOWNGRADE },
94 { ERROR_INSTALL_DEPENDENT_MODULE_NOT_EXIST, ERR_MSG_INSTALL_DEPENDENT_MODULE_NOT_EXIST },
95 { ERROR_UNINSTALL_PREINSTALL_APP_FAILED, ERR_MSG_UNINSTALL_PREINSTALL_APP_FAILED },
96 { ERROR_BUNDLE_SERVICE_EXCEPTION, ERR_MSG_BUNDLE_SERVICE_EXCEPTION },
97 { ERR_ZLIB_SRC_FILE_INVALID, ERR_ZLIB_SRC_FILE_INVALID_MSG },
98 { ERR_ZLIB_DEST_FILE_INVALID, ERR_ZLIB_DEST_FILE_INVALID_MSG },
99 { ERROR_BUNDLE_IS_DISABLED, ERROR_MSG_BUNDLE_IS_DISABLED },
100 { ERROR_ABILITY_IS_DISABLED, ERROR_MSG_ABILITY_IS_DISABLED },
101 { ERROR_PROFILE_NOT_EXIST, ERROR_MSG_PROFILE_NOT_EXIST },
102 { ERROR_INVALID_UID, ERROR_INVALID_UID_MSG },
103 { ERROR_INVALID_HAP_PATH, ERROR_INVALID_HAP_PATH_MSG },
104 { ERROR_DEFAULT_APP_NOT_EXIST, ERROR_DEFAULT_APP_NOT_EXIST_MSG },
105 { ERROR_INVALID_TYPE, ERROR_INVALID_TYPE_MSG },
106 { ERROR_DISTRIBUTED_SERVICE_NOT_RUNNING, ERROR_MSG_DISTRIBUTED_SERVICE_NOT_RUNNING },
107 { ERROR_ABILITY_AND_TYPE_MISMATCH, ERROR_ABILITY_AND_TYPE_MISMATCH_MSG },
108 { ERROR_CLEAR_CACHE_FILES_UNSUPPORTED, ERROR_MSG_CLEAR_CACHE_FILES_UNSUPPORTED }
109 };
110 } // namespace
111
ThrowError(napi_env env,int32_t err,const std::string & msg)112 void BusinessError::ThrowError(napi_env env, int32_t err, const std::string &msg)
113 {
114 napi_value error = CreateError(env, err, msg);
115 napi_throw(env, error);
116 }
117
ThrowParameterTypeError(napi_env env,int32_t err,const std::string & parameter,const std::string & type)118 void BusinessError::ThrowParameterTypeError(napi_env env, int32_t err,
119 const std::string ¶meter, const std::string &type)
120 {
121 napi_value error = CreateCommonError(env, err, parameter, type);
122 napi_throw(env, error);
123 }
124
ThrowTooFewParametersError(napi_env env,int32_t err)125 void BusinessError::ThrowTooFewParametersError(napi_env env, int32_t err)
126 {
127 ThrowError(env, err, ERR_MSG_PARAM_NUMBER_ERROR);
128 }
129
CreateError(napi_env env,int32_t err,const std::string & msg)130 napi_value BusinessError::CreateError(napi_env env, int32_t err, const std::string& msg)
131 {
132 napi_value businessError = nullptr;
133 napi_value errorCode = nullptr;
134 NAPI_CALL(env, napi_create_int32(env, err, &errorCode));
135 napi_value errorMessage = nullptr;
136 NAPI_CALL(env, napi_create_string_utf8(env, msg.c_str(), NAPI_AUTO_LENGTH, &errorMessage));
137 napi_create_error(env, nullptr, errorMessage, &businessError);
138 napi_set_named_property(env, businessError, "code", errorCode);
139 return businessError;
140 }
141
CreateCommonError(napi_env env,int32_t err,const std::string & functionName,const std::string & permissionName)142 napi_value BusinessError::CreateCommonError(
143 napi_env env, int32_t err, const std::string &functionName, const std::string &permissionName)
144 {
145 std::string errMessage = ERR_MSG_BUSINESS_ERROR;
146 auto iter = errMessage.find("$");
147 if (iter != std::string::npos) {
148 errMessage = errMessage.replace(iter, 1, std::to_string(err));
149 }
150 if (ERR_MSG_MAP.find(err) != ERR_MSG_MAP.end()) {
151 errMessage += ERR_MSG_MAP[err];
152 }
153 iter = errMessage.find("$");
154 if (iter != std::string::npos) {
155 errMessage = errMessage.replace(iter, 1, functionName);
156 iter = errMessage.find("$");
157 if (iter != std::string::npos) {
158 errMessage = errMessage.replace(iter, 1, permissionName);
159 }
160 }
161 return CreateError(env, err, errMessage);
162 }
163 } // AppExecFwk
164 } // OHOS