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