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_business_error.h"
17
18 #include <unordered_map>
19
20 #include "ability_manager_errors.h"
21
22 namespace OHOS {
23 namespace AbilityRuntime {
24 using namespace OHOS::AAFwk;
25 namespace {
26 constexpr const char* TAG_PERMISSION = " permission:";
27 constexpr const char* ERROR_MSG_OK = "OK.";
28 constexpr const char* ERROR_MSG_PERMISSION_DENIED = "The application does not have permission to call the interface.";
29 constexpr const char* ERROR_MSG_INVALID_PARAM = "Invalid input parameter.";
30 constexpr const char* ERROR_MSG_SYSTEMCAP = "The specified SystemCapability name was not found.";
31 constexpr const char* ERROR_MSG_INNER = "Inner Error.";
32 constexpr const char* ERROR_MSG_RESOLVE_ABILITY = "The specified ability does not exist.";
33 constexpr const char* ERROR_MSG_INVALID_ABILITY_TYPE = "Ability type error. The specified ability type is wrong";
34 constexpr const char* ERROR_MSG_INVALID_ID = "The specified id does not exist.";
35 constexpr const char* ERROR_MSG_INVISIBLE = "Can not start invisible component.";
36 constexpr const char* ERROR_MSG_STATIC_CFG_PERMISSION = "The specified process does not have the permission.";
37 constexpr const char* ERROR_MSG_CROSS_USER = "Can not cross user operations.";
38 constexpr const char* ERROR_MSG_CROWDTEST_EXPIRED = "Crowdtest App Expiration.";
39 constexpr const char* ERROR_MSG_WUKONG_MODE = "Cannot operate in wukong mode.";
40 constexpr const char* ERROR_MSG_CONTINUATION_FLAG = "StartAbility with continuation flags is not allowed!";
41 constexpr const char* ERROR_MSG_INVALID_CONTEXT = "Context does not exist!";
42 constexpr const char* ERROR_MSG_NETWORK_ABNORMAL = "Network error. The network is abnormal when free install.";
43 constexpr const char* ERROR_MSG_NOT_SUPPORT_FREE_INSTALL = "The Application does not support free install.";
44 constexpr const char* ERROR_MSG_NOT_TOP_ABILITY = "Not top ability, Can not free install ability.";
45 constexpr const char* ERROR_MSG_FREE_INSTALL_TOO_BUSY =
46 "Free install busyness. There are concurrent tasks waiting for retry.";
47 constexpr const char* ERROR_MSG_FREE_INSTALL_TIMEOUT = "Free install timeout.";
48 constexpr const char* ERROR_MSG_FREE_INSTALL_OTHERS = "Can not free install other ability.";
49 constexpr const char* ERROR_MSG_INVALID_CALLER = "Caller released, The caller has been released.";
50 constexpr const char* ERROR_MSG_NO_MISSION_ID = "The specified mission id does not exist.";
51 constexpr const char* ERROR_MSG_NO_MISSION_LISTENER = "The specified mission listener does not exist.";
52 constexpr const char* ERROR_MSG_START_ABILITY_WAITTING = "The previous ability is starting, wait start later.";
53
54 static std::unordered_map<AbilityErrorCode, const char*> ERR_CODE_MAP = {
55 { AbilityErrorCode::ERROR_OK, ERROR_MSG_OK },
56 { AbilityErrorCode::ERROR_CODE_PERMISSION_DENIED, ERROR_MSG_PERMISSION_DENIED },
57 { AbilityErrorCode::ERROR_CODE_INVALID_PARAM, ERROR_MSG_INVALID_PARAM },
58 { AbilityErrorCode::ERROR_CODE_SYSTEMCAP, ERROR_MSG_SYSTEMCAP },
59 { AbilityErrorCode::ERROR_CODE_INNER, ERROR_MSG_INNER },
60 { AbilityErrorCode::ERROR_CODE_RESOLVE_ABILITY, ERROR_MSG_RESOLVE_ABILITY },
61 { AbilityErrorCode::ERROR_CODE_INVALID_ABILITY_TYPE, ERROR_MSG_INVALID_ABILITY_TYPE },
62 { AbilityErrorCode::ERROR_CODE_INVALID_ID, ERROR_MSG_INVALID_ID },
63 { AbilityErrorCode::ERROR_CODE_NO_INVISIBLE_PERMISSION, ERROR_MSG_INVISIBLE },
64 { AbilityErrorCode::ERROR_CODE_STATIC_CFG_PERMISSION, ERROR_MSG_STATIC_CFG_PERMISSION },
65 { AbilityErrorCode::ERROR_CODE_CROSS_USER, ERROR_MSG_CROSS_USER },
66 { AbilityErrorCode::ERROR_CODE_CROWDTEST_EXPIRED, ERROR_MSG_CROWDTEST_EXPIRED },
67 { AbilityErrorCode::ERROR_CODE_WUKONG_MODE, ERROR_MSG_WUKONG_MODE },
68 { AbilityErrorCode::ERROR_CODE_CONTINUATION_FLAG, ERROR_MSG_CONTINUATION_FLAG},
69 { AbilityErrorCode::ERROR_CODE_INVALID_CONTEXT, ERROR_MSG_INVALID_CONTEXT},
70 { AbilityErrorCode::ERROR_CODE_NETWORK_ABNORMAL, ERROR_MSG_NETWORK_ABNORMAL },
71 { AbilityErrorCode::ERROR_CODE_NOT_SUPPORT_FREE_INSTALL, ERROR_MSG_NOT_SUPPORT_FREE_INSTALL },
72 { AbilityErrorCode::ERROR_CODE_NOT_TOP_ABILITY, ERROR_MSG_NOT_TOP_ABILITY },
73 { AbilityErrorCode::ERROR_CODE_FREE_INSTALL_TOO_BUSY, ERROR_MSG_FREE_INSTALL_TOO_BUSY },
74 { AbilityErrorCode::ERROR_CODE_FREE_INSTALL_TIMEOUT, ERROR_MSG_FREE_INSTALL_TIMEOUT },
75 { AbilityErrorCode::ERROR_CODE_FREE_INSTALL_OTHERS, ERROR_MSG_FREE_INSTALL_OTHERS },
76 { AbilityErrorCode::ERROR_CODE_INVALID_CALLER, ERROR_MSG_INVALID_CALLER },
77 { AbilityErrorCode::ERROR_CODE_NO_MISSION_ID, ERROR_MSG_NO_MISSION_ID },
78 { AbilityErrorCode::ERROR_CODE_NO_MISSION_LISTENER, ERROR_MSG_NO_MISSION_LISTENER },
79 { AbilityErrorCode::ERROR_START_ABILITY_WAITTING, ERROR_MSG_START_ABILITY_WAITTING }
80 };
81
82 static std::unordered_map<int32_t, AbilityErrorCode> INNER_TO_JS_ERROR_CODE_MAP {
83 {0, AbilityErrorCode::ERROR_OK},
84 {CHECK_PERMISSION_FAILED, AbilityErrorCode::ERROR_CODE_PERMISSION_DENIED},
85 {RESOLVE_ABILITY_ERR, AbilityErrorCode::ERROR_CODE_RESOLVE_ABILITY},
86 {ERR_WRONG_INTERFACE_CALL, AbilityErrorCode::ERROR_CODE_INVALID_ABILITY_TYPE},
87 {TARGET_ABILITY_NOT_SERVICE, AbilityErrorCode::ERROR_CODE_INVALID_ABILITY_TYPE},
88 {RESOLVE_CALL_ABILITY_TYPE_ERR, AbilityErrorCode::ERROR_CODE_INVALID_ABILITY_TYPE},
89 {ABILITY_VISIBLE_FALSE_DENY_REQUEST, AbilityErrorCode::ERROR_CODE_NO_INVISIBLE_PERMISSION},
90 {ERR_STATIC_CFG_PERMISSION, AbilityErrorCode::ERROR_CODE_STATIC_CFG_PERMISSION},
91 {ERR_CROSS_USER, AbilityErrorCode::ERROR_CODE_CROSS_USER},
92 {ERR_CROWDTEST_EXPIRED, AbilityErrorCode::ERROR_CODE_CROWDTEST_EXPIRED},
93 {ERR_WOULD_BLOCK, AbilityErrorCode::ERROR_CODE_WUKONG_MODE},
94 {ERR_INVALID_CONTINUATION_FLAG, AbilityErrorCode::ERROR_CODE_CONTINUATION_FLAG},
95 {HAP_PACKAGE_DOWNLOAD_TIMED_OUT, AbilityErrorCode::ERROR_CODE_NETWORK_ABNORMAL},
96 {FA_PACKAGE_DOES_NOT_SUPPORT_FREE_INSTALL, AbilityErrorCode::ERROR_CODE_NOT_SUPPORT_FREE_INSTALL},
97 {NOT_TOP_ABILITY, AbilityErrorCode::ERROR_CODE_NOT_TOP_ABILITY},
98 {CONCURRENT_TASKS_WAITING_FOR_RETRY, AbilityErrorCode::ERROR_CODE_FREE_INSTALL_TOO_BUSY},
99 {FREE_INSTALL_TIMEOUT, AbilityErrorCode::ERROR_CODE_FREE_INSTALL_TIMEOUT},
100 {NOT_ALLOWED_TO_PULL_THIS_FA, AbilityErrorCode::ERROR_CODE_FREE_INSTALL_OTHERS},
101 {ERR_INVALID_CALLER, AbilityErrorCode::ERROR_CODE_INVALID_CALLER},
102 {MISSION_NOT_FOUND, AbilityErrorCode::ERROR_CODE_NO_MISSION_ID},
103 {START_ABILITY_WAITING, AbilityErrorCode::ERROR_START_ABILITY_WAITTING}
104 };
105 }
106
GetErrorMsg(const AbilityErrorCode & errCode)107 std::string GetErrorMsg(const AbilityErrorCode& errCode)
108 {
109 auto it = ERR_CODE_MAP.find(errCode);
110 if (it != ERR_CODE_MAP.end()) {
111 return it->second;
112 }
113
114 return "";
115 }
116
GetNoPermissionErrorMsg(const std::string & permission)117 std::string GetNoPermissionErrorMsg(const std::string& permission)
118 {
119 return std::string(ERROR_MSG_PERMISSION_DENIED) + std::string(TAG_PERMISSION) + permission;
120 }
121
GetJsErrorCodeByNativeError(int32_t errCode)122 AbilityErrorCode GetJsErrorCodeByNativeError(int32_t errCode)
123 {
124 auto it = INNER_TO_JS_ERROR_CODE_MAP.find(errCode);
125 if (it != INNER_TO_JS_ERROR_CODE_MAP.end()) {
126 return it->second;
127 }
128
129 return AbilityErrorCode::ERROR_CODE_INNER;
130 }
131 } // namespace AbilityRuntime
132 } // namespace OHOS