• 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 "ability_runtime_error_util.h"
17 
18 #include <map>
19 #include "errors.h"
20 #include "hilog_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_EXTERNAL_INVALID_PARAMETER,
125         "Invalid input parameter." },
126     { ERR_ABILITY_RUNTIME_EXTERNAL_NO_SUCH_SYSCAP,
127         "The specified SystemCapability name was not found." },
128     { ERR_ABILITY_RUNTIME_EXTERNAL_START_ABILITY_WAITTING,
129         "The previous ability is starting, wait start later." }
130 };
131 }
132 
Throw(NativeEngine & engine,int32_t errCode,const std::string & errMessage)133 bool AbilityRuntimeErrorUtil::Throw(NativeEngine &engine, int32_t errCode, const std::string &errMessage)
134 {
135     std::string eMes = errMessage;
136     if (eMes.empty()) {
137         eMes = ERROR_MSG_MAP.at(errCode);
138     }
139     NativeValue *error = engine.CreateError(CreateJsValue(engine, errCode), CreateJsValue(engine, eMes));
140     if (error == nullptr) {
141         HILOG_ERROR("Failed to create error.");
142         return false;
143     }
144     return engine.Throw(error);
145 }
146 
ThrowByInternalErrCode(NativeEngine & engine,int32_t errCode)147 bool AbilityRuntimeErrorUtil::ThrowByInternalErrCode(NativeEngine &engine, int32_t errCode)
148 {
149     if (ERROR_CODE_MAP.find(errCode) == ERROR_CODE_MAP.end()) {
150         HILOG_ERROR("Invalid inner errCode, check ERROR_CODE_MAP");
151         return false;
152     }
153     return Throw(engine, ERROR_CODE_MAP.at(errCode));
154 }
155 
CreateErrorByInternalErrCode(NativeEngine & engine,int32_t errCode)156 NativeValue *AbilityRuntimeErrorUtil::CreateErrorByInternalErrCode(NativeEngine &engine, int32_t errCode)
157 {
158     if (ERROR_CODE_MAP.find(errCode) == ERROR_CODE_MAP.end()) {
159         HILOG_ERROR("Invalid inner errCode, check ERROR_CODE_MAP");
160         return nullptr;
161     }
162     int32_t externalErrCode = ERROR_CODE_MAP.at(errCode);
163     return CreateJsError(engine, externalErrCode, ERROR_MSG_MAP.at(externalErrCode));
164 }
165 
GetErrMessage(int32_t errCode)166 std::string AbilityRuntimeErrorUtil::GetErrMessage(int32_t errCode)
167 {
168     auto iter = ERROR_MSG_MAP.find(errCode);
169     std::string errMsg = (iter != ERROR_MSG_MAP.end()) ? iter->second : "";
170     return errMsg;
171 }
172 } // namespace AbilityRuntime
173 } // namespace OHOS