• 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 "ability_runtime_error_util.h"
17 
18 #include <map>
19 #include "errors.h"
20 #include "hilog_tag_wrapper.h"
21 #include "js_runtime_utils.h"
22 #include "napi/native_api.h"
23 #include "runtime.h"
24 
25 namespace OHOS {
26 namespace AbilityRuntime {
27 namespace {
28 const std::map<int32_t, int32_t> ERROR_CODE_MAP = {
29     { ERR_OK, ERR_OK }
30 };
31 
32 const std::map<int32_t, std::string> ERROR_MSG_MAP = {
33     { ERR_OK,
34         "success" },
35     { ERR_ABILITY_RUNTIME_EXTERNAL_NO_SUCH_ABILITY_NAME,
36         "Input error. The specified ability name not exsit." },
37     { ERR_ABILITY_RUNTIME_EXTERNAL_NOT_SUPPORT_OPERATION,
38         "Incorrect ability type." },
39     { ERR_ABILITY_RUNTIME_EXTERNAL_NO_SUCH_ID,
40         "The specified ID does not exist." },
41     { ERR_ABILITY_RUNTIME_EXTERNAL_VISIBILITY_VERIFICATION_FAILED,
42         "Failed to start the invisible ability." },
43     { ERR_ABILITY_RUNTIME_EXTERNAL_CROSS_USER_OPERATION,
44         "Cannot cross user operations." },
45     { ERR_ABILITY_RUNTIME_EXTERNAL_SERVICE_BUSY,
46         "Service busy. There are concurrent tasks. Try again later." },
47     { ERR_ABILITY_RUNTIME_EXTERNAL_CROWDTEST_APP_EXPIRATION,
48         "Crowdtest App Expiration." },
49     { ERR_ABILITY_RUNTIME_EXTERNAL_WUKONG_MODE,
50         "Cannot start ability in wukong mode." },
51     { ERR_ABILITY_RUNTIME_EXTERNAL_OPERATION_WITH_CONTINUE_FLAG,
52         "Cannot operation with continue flag." },
53     { ERR_ABILITY_RUNTIME_EXTERNAL_CONTEXT_NOT_EXIST,
54         "Context does not exist." },
55     { ERR_ABILITY_RUNTIME_EXTERNAL_ABILITY_ALREADY_AT_TOP,
56         "Ability is already at the top." },
57     { ERR_ABILITY_RUNTIME_EXTERNAL_CONNECTION_NOT_EXIST,
58         "Connection not exist." },
59     { ERR_ABILITY_RUNTIME_EXTERNAL_CONNECTION_STATE_ABNORMAL,
60         "Connection state is abnormal." },
61     { ERR_ABILITY_RUNTIME_EXTERNAL_SERVICE_TIMEOUT,
62         "Service timeout." },
63     { ERR_ABILITY_RUNTIME_EXTERNAL_APP_UNDER_CONTROL,
64         "Application is under control." },
65     { ERR_ABILITY_RUNTIME_EXTERNAL_INTERNAL_ERROR,
66         "Internal error." },
67     { ERR_ABILITY_RUNTIME_EXTERNAL_NETWORK_ERROR,
68         "Network error. The network is abnormal." },
69     { ERR_ABILITY_RUNTIME_EXTERNAL_FREE_INSTALL_NOT_SUPPORT,
70         "Free install not support. The application does not support free install." },
71     { ERR_ABILITY_RUNTIME_EXTERNAL_NOT_TOP_ABILITY,
72         "Not top ability. The application is not top ability." },
73     { ERR_ABILITY_RUNTIME_EXTERNAL_FREE_INSTALL_BUSY,
74         "The installation-free service is busy. Try again later." },
75     { ERR_ABILITY_RUNTIME_EXTERNAL_FREE_INSTALL_TIMEOUT,
76         "Free install timeout." },
77     { ERR_ABILITY_RUNTIME_EXTERNAL_CANNOT_FREE_INSTALL_OTHER_ABILITY,
78         "Cannot free install other ability." },
79     { ERR_ABILITY_RUNTIME_EXTERNAL_NOT_SUPPORT_CROSS_DEVICE_FREE_INSTALL,
80         "Not support cross device free install." },
81     { ERR_ABILITY_RUNTIME_EXTERNAL_EXECUTE_SHELL_COMMAND_FAILED,
82         "Execute shell command failed." },
83     { ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_WANTAGENT,
84         "Invalid wantagent object." },
85     { ERR_ABILITY_RUNTIME_EXTERNAL_WANTAGENT_NOT_FOUND,
86         "The wantAgent object does not exist." },
87     { ERR_ABILITY_RUNTIME_EXTERNAL_WANTAGENT_CANCELED,
88         "The wantAgent object has been canceled." },
89     { ERR_ABILITY_RUNTIME_EXTERNAL_NO_SUCH_URI_ABILITY,
90         "Input error. The specified uri does not exist." },
91     { ERR_ABILITY_RUNTIME_EXTERNAL_FA_NOT_SUPPORT_OPERATION,
92         "Ability type error. The specified ability type is wrong." },
93     { ERR_ABILITY_RUNTIME_EXTERNAL_CALLER_RELEASED,
94         "Caller released. The caller has been released." },
95     { ERR_ABILITY_RUNTIME_EXTERNAL_CALLEE_INVALID,
96         "The callee does not exist." },
97     { ERR_ABILITY_RUNTIME_EXTERNAL_RELEASE_ERROR,
98         "Release error. The caller does not call any callee." },
99     { ERR_ABILITY_RUNTIME_EXTERNAL_METHOED_REGISTERED,
100         "The method has been registered." },
101     { ERR_ABILITY_RUNTIME_EXTERNAL_METHOED_NOT_REGISTERED,
102         "The method has not been registered." },
103     { ERR_ABILITY_RUNTIME_EXTERNAL_NO_SUCH_MISSION,
104         "Mission id error. The specified mission id does not exist." },
105     { ERR_ABILITY_RUNTIME_EXTERNAL_NO_SUCH_MISSION_LISTENER,
106         "The specified mission listener does not exist." },
107     { ERR_ABILITY_RUNTIME_EXTERNAL_NO_SUCH_BUNDLENAME,
108         "The bundle does not exist or no patch has been applied." },
109     { ERR_ABILITY_RUNTIME_EXTERNAL_NO_SUCH_HQF,
110         "The specified hqf is invalid. Hqf may not exist or inaccessible." },
111     { ERR_ABILITY_RUNTIME_EXTERNAL_DEPLOY_HQF_FAILED,
112         "Deploy hqf failed." },
113     { ERR_ABILITY_RUNTIME_EXTERNAL_SWITCH_HQF_FAILED,
114         "Switch hqf failed." },
115     { ERR_ABILITY_RUNTIME_EXTERNAL_DELETE_HQF_FAILED,
116         "Failed to remove the patch package." },
117     { ERR_ABILITY_RUNTIME_EXTERNAL_LOAD_PATCH_FAILED,
118         "Load patch failed." },
119     { ERR_ABILITY_RUNTIME_EXTERNAL_UNLOAD_PATCH_FAILED,
120         "Unload patch failed." },
121     { ERR_ABILITY_RUNTIME_EXTERNAL_QUICK_FIX_INTERNAL_ERROR,
122         "Internal error." },
123     { ERR_ABILITY_RUNTIME_EXTERNAL_NO_ACCESS_PERMISSION,
124         "The application does not have permission to call the interface." },
125     { ERR_ABILITY_RUNTIME_NOT_SYSTEM_APP,
126         "The application is not system-app, can not use system-api." },
127     { ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER,
128         "Invalid input parameter." },
129     { ERR_ABILITY_RUNTIME_EXTERNAL_NO_SUCH_SYSCAP,
130         "The specified SystemCapability name was not found." },
131     { ERR_ABILITY_RUNTIME_EXTERNAL_START_ABILITY_WAITTING,
132         "Another ability is being started. Wait until it finishes starting." },
133     { ERR_ABILITY_RUNTIME_EXTERNAL_NOT_SUPPORT_CROSS_APP_START,
134         "Redirection to a third-party application is not allowed in API version 11 or later." },
135     { ERR_ABILITY_RUNTIME_EXTERNAL_CANNOT_MATCH_ANY_COMPONENT,
136         "No matching ability is found." },
137     { ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_URI_FLAG,
138         "Invalid URI flag." },
139     { ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_URI_TYPE,
140         "Invalid URI type, only support file uri" },
141     { ERR_ABILITY_RUNTIME_EXTERNAL_GRANT_URI_PERMISSION,
142         "Sandbox application can not grant URI permission." },
143     { ERR_ABILITY_RUNTIME_OPERATION_NOT_SUPPORTED,
144         "Operation not supported." },
145     { ERR_ABILITY_RUNTIME_CHILD_PROCESS_NUMBER_EXCEEDS_UPPER_BOUND,
146         "The number of child processes exceeds the upper limit." },
147     { ERR_ABILITY_RUNTIME_RESTART_APP_INCORRECT_ABILITY,
148         "The target to restart does not belong to the current application or is not a UIAbility." },
149     { ERR_ABILITY_RUNTIME_RESTART_APP_FREQUENT,
150         "Restart too frequently. Try again at least 3s later." },
151     { ERR_ABILITY_RUNTIME_EXTERNAL_NOT_SYSTEM_HSP,
152         "The input bundleName and moduleName is not system HSP" },
153 };
154 }
155 
Throw(napi_env env,int32_t errCode,const std::string & errMessage)156 bool AbilityRuntimeErrorUtil::Throw(napi_env env, int32_t errCode, const std::string &errMessage)
157 {
158     std::string eMes = errMessage;
159     if (eMes.empty()) {
160         eMes = ERROR_MSG_MAP.at(errCode);
161     }
162     napi_value error = nullptr;
163     napi_create_error(env, CreateJsValue(env, errCode), CreateJsValue(env, eMes), &error);
164     if (error == nullptr) {
165         TAG_LOGE(AAFwkTag::DEFAULT, "create error");
166         return false;
167     }
168     napi_throw(env, error);
169     return true;
170 }
171 
ThrowByInternalErrCode(napi_env env,int32_t errCode)172 bool AbilityRuntimeErrorUtil::ThrowByInternalErrCode(napi_env env, int32_t errCode)
173 {
174     if (ERROR_CODE_MAP.find(errCode) == ERROR_CODE_MAP.end()) {
175         TAG_LOGE(AAFwkTag::DEFAULT, "Invalid errCode");
176         return false;
177     }
178     return Throw(env, ERROR_CODE_MAP.at(errCode));
179 }
180 
CreateErrorByInternalErrCode(napi_env env,int32_t errCode)181 napi_value AbilityRuntimeErrorUtil::CreateErrorByInternalErrCode(napi_env env, int32_t errCode)
182 {
183     if (ERROR_CODE_MAP.find(errCode) == ERROR_CODE_MAP.end()) {
184         TAG_LOGE(AAFwkTag::DEFAULT, "Invalid errCode");
185         return nullptr;
186     }
187     int32_t externalErrCode = ERROR_CODE_MAP.at(errCode);
188     return CreateJsError(env, externalErrCode, ERROR_MSG_MAP.at(externalErrCode));
189 }
190 
GetErrMessage(int32_t errCode)191 std::string AbilityRuntimeErrorUtil::GetErrMessage(int32_t errCode)
192 {
193     auto iter = ERROR_MSG_MAP.find(errCode);
194     std::string errMsg = (iter != ERROR_MSG_MAP.end()) ? iter->second : "";
195     return errMsg;
196 }
197 } // namespace AbilityRuntime
198 } // namespace OHOS
199