diff --git a/frameworks/js/napi/mission_manager/mission_manager.cpp b/frameworks/js/napi/mission_manager/mission_manager.cpp index 1a308029ac..841d4f1bec 100644 --- a/frameworks/js/napi/mission_manager/mission_manager.cpp +++ b/frameworks/js/napi/mission_manager/mission_manager.cpp @@ -19,11 +19,13 @@ #include "event_handler.h" #include "event_runner.h" #include "hilog_wrapper.h" +#include "js_error_utils.h" #include "js_mission_info_utils.h" #include "js_mission_listener.h" #include "js_runtime_utils.h" #include "mission_snapshot.h" #include "napi_common_start_options.h" +#include "permission_constants.h" #ifdef SUPPORT_GRAPHICS #include "pixel_map_napi.h" #endif @@ -33,13 +35,11 @@ namespace OHOS { namespace AbilityRuntime { -constexpr size_t ARGC_ONE = 1; using namespace OHOS::AppExecFwk; using AbilityManagerClient = AAFwk::AbilityManagerClient; namespace { - constexpr int32_t ARG_COUNT_TWO = 2; - constexpr int32_t ARG_COUNT_THREE = 3; - constexpr int32_t ERR_NOT_OK = -1; +constexpr size_t ARGC_ONE = 1; +constexpr int32_t ARG_COUNT_TWO = 2; } class JsMissionManager { public: @@ -122,8 +122,9 @@ private: NativeValue* OnRegisterMissionListener(NativeEngine &engine, NativeCallbackInfo &info) { HILOG_INFO("%{public}s is called", __FUNCTION__); - if (info.argc != 1) { + if (info.argc < 1) { HILOG_ERROR("Params not match"); + ThrowTooFewParametersError(engine); return engine.CreateUndefined(); } @@ -141,6 +142,11 @@ private: } else { HILOG_ERROR("RegisterMissionListener failed, ret = %{public}d", ret); missionListener_ = nullptr; + if (ret == CHECK_PERMISSION_FAILED) { + ThrowNoPermissionError(engine, PermissionConstants::PERMISSION_MANAGE_MISSION); + } else { + ThrowError(engine, AbilityErrorCode::ERROR_CODE_INNER); + } return engine.CreateUndefined(); } } @@ -148,28 +154,27 @@ private: NativeValue* OnUnregisterMissionListener(NativeEngine &engine, NativeCallbackInfo &info) { HILOG_INFO("%{public}s is called", __FUNCTION__); - int32_t errCode = 0; if (info.argc < 1) { HILOG_ERROR("Not enough params"); - errCode = ERR_NOT_OK; + ThrowTooFewParametersError(engine); + return engine.CreateUndefined(); } + int32_t missionListenerId = -1; - if (!errCode && !ConvertFromJsValue(engine, info.argv[0], missionListenerId)) { + if (!ConvertFromJsValue(engine, info.argv[0], missionListenerId)) { HILOG_ERROR("Parse missionListenerId failed"); - errCode = ERR_NOT_OK; + ThrowError(engine, AbilityErrorCode::ERROR_CODE_INVALID_PARAM); + return engine.CreateUndefined(); } AsyncTask::CompleteCallback complete = - [&missionListener = missionListener_, missionListenerId, errCode] + [&missionListener = missionListener_, missionListenerId] (NativeEngine &engine, AsyncTask &task, int32_t status) { - if (errCode != 0) { - task.Reject(engine, CreateJsError(engine, errCode, "Invalidate params.")); - return; - } if (!missionListener || !missionListener->RemoveJsListenerObject(missionListenerId)) { - task.Reject(engine, CreateJsError(engine, ERR_NOT_OK, "Not registered yet.")); + task.Reject(engine, CreateJsError(engine, AbilityErrorCode::ERROR_CODE_NO_MISSION_LISTENER)); return; } + if (!missionListener->IsEmpty()) { task.Resolve(engine, engine.CreateUndefined()); return; @@ -179,7 +184,8 @@ private: task.Resolve(engine, engine.CreateUndefined()); missionListener = nullptr; } else { - task.Reject(engine, CreateJsError(engine, ret, "Unregister mission listener failed.")); + task.Reject(engine, + CreateJsErrorByNativeErr(engine, ret, PermissionConstants::PERMISSION_MANAGE_MISSION)); } }; @@ -193,34 +199,33 @@ private: NativeValue* OnGetMissionInfos(NativeEngine &engine, NativeCallbackInfo &info) { HILOG_INFO("%{public}s is called", __FUNCTION__); - int32_t errCode = 0; - if (info.argc < 2) { + if (info.argc < 2) { // at least 2 parameters. HILOG_ERROR("Not enough params"); - errCode = ERR_NOT_OK; + ThrowTooFewParametersError(engine); + return engine.CreateUndefined(); } std::string deviceId; - if (!errCode && !ConvertFromJsValue(engine, info.argv[0], deviceId)) { + if (!ConvertFromJsValue(engine, info.argv[0], deviceId)) { HILOG_ERROR("Parse deviceId failed"); - errCode = ERR_NOT_OK; + ThrowError(engine, AbilityErrorCode::ERROR_CODE_INVALID_PARAM); + return engine.CreateUndefined(); } int numMax = -1; - if (!errCode && !ConvertFromJsValue(engine, info.argv[1], numMax)) { + if (!ConvertFromJsValue(engine, info.argv[1], numMax)) { HILOG_ERROR("Parse numMax failed"); - errCode = ERR_NOT_OK; + ThrowError(engine, AbilityErrorCode::ERROR_CODE_INVALID_PARAM); + return engine.CreateUndefined(); } AsyncTask::CompleteCallback complete = - [deviceId, numMax, errCode](NativeEngine &engine, AsyncTask &task, int32_t status) { - if (errCode != 0) { - task.Reject(engine, CreateJsError(engine, errCode, "Invalidate params.")); - return; - } + [deviceId, numMax](NativeEngine &engine, AsyncTask &task, int32_t status) { std::vector missionInfos; auto ret = AbilityManagerClient::GetInstance()->GetMissionInfos(deviceId, numMax, missionInfos); if (ret == 0) { task.Resolve(engine, CreateJsMissionInfoArray(engine, missionInfos)); } else { - task.Reject(engine, CreateJsError(engine, ret, "Get mission infos failed.")); + task.Reject(engine, + CreateJsErrorByNativeErr(engine, ret, PermissionConstants::PERMISSION_MANAGE_MISSION)); } }; @@ -234,34 +239,33 @@ private: NativeValue* OnGetMissionInfo(NativeEngine &engine, NativeCallbackInfo &info) { HILOG_INFO("%{public}s is called", __FUNCTION__); - int32_t errCode = 0; if (info.argc < 2) { HILOG_ERROR("Not enough params"); - errCode = ERR_NOT_OK; + ThrowTooFewParametersError(engine); + return engine.CreateUndefined(); } std::string deviceId; - if (!errCode && !ConvertFromJsValue(engine, info.argv[0], deviceId)) { + if (!ConvertFromJsValue(engine, info.argv[0], deviceId)) { HILOG_ERROR("Parse deviceId failed"); - errCode = ERR_NOT_OK; + ThrowError(engine, AbilityErrorCode::ERROR_CODE_INVALID_PARAM); + return engine.CreateUndefined(); } int32_t missionId = -1; - if (!errCode && !ConvertFromJsValue(engine, info.argv[1], missionId)) { + if (!ConvertFromJsValue(engine, info.argv[1], missionId)) { HILOG_ERROR("Parse missionId failed"); - errCode = ERR_NOT_OK; + ThrowError(engine, AbilityErrorCode::ERROR_CODE_INVALID_PARAM); + return engine.CreateUndefined(); } AsyncTask::CompleteCallback complete = - [deviceId, missionId, errCode](NativeEngine &engine, AsyncTask &task, int32_t status) { - if (errCode != 0) { - task.Reject(engine, CreateJsError(engine, errCode, "Invalidate params.")); - return; - } + [deviceId, missionId](NativeEngine &engine, AsyncTask &task, int32_t status) { AAFwk::MissionInfo missionInfo; auto ret = AbilityManagerClient::GetInstance()->GetMissionInfo(deviceId, missionId, missionInfo); if (ret == 0) { task.Resolve(engine, CreateJsMissionInfo(engine, missionInfo)); } else { - task.Reject(engine, CreateJsError(engine, ret, "Get mission info failed.")); + task.Reject(engine, + CreateJsErrorByNativeErr(engine, ret, PermissionConstants::PERMISSION_MANAGE_MISSION)); } }; @@ -275,16 +279,14 @@ private: NativeValue* OnGetMissionSnapShot(NativeEngine &engine, NativeCallbackInfo &info, bool isLowResolution) { HILOG_INFO("%{public}s is called", __FUNCTION__); - int32_t errCode = 0; std::string deviceId; int32_t missionId = -1; - CheckMissionSnapShotParams(engine, info, errCode, deviceId, missionId); + if (!CheckMissionSnapShotParams(engine, info, deviceId, missionId)) { + return engine.CreateUndefined(); + } + AsyncTask::CompleteCallback complete = - [deviceId, missionId, errCode, isLowResolution](NativeEngine &engine, AsyncTask &task, int32_t status) { - if (errCode != 0) { - task.Reject(engine, CreateJsError(engine, errCode, "Invalidate params.")); - return; - } + [deviceId, missionId, isLowResolution](NativeEngine &engine, AsyncTask &task, int32_t status) { AAFwk::MissionSnapshot missionSnapshot; auto ret = AbilityManagerClient::GetInstance()->GetMissionSnapshot( deviceId, missionId, missionSnapshot, isLowResolution); @@ -305,62 +307,67 @@ private: #endif task.Resolve(engine, objValue); } else { - task.Reject(engine, CreateJsError(engine, ret, "Get mission snapshot failed.")); + task.Reject(engine, + CreateJsErrorByNativeErr(engine, ret, PermissionConstants::PERMISSION_MANAGE_MISSION)); } }; - NativeValue* lastParam = (info.argc <= ARG_COUNT_TWO) ? nullptr : info.argv[2]; + NativeValue* lastParam = (info.argc > ARG_COUNT_TWO) ? info.argv[ARG_COUNT_TWO] : nullptr; NativeValue* result = nullptr; AsyncTask::Schedule("MissioManager::OnGetMissionSnapShot", engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result)); return result; } - void CheckMissionSnapShotParams(NativeEngine &engine, NativeCallbackInfo &info, int32_t &errCode, + bool CheckMissionSnapShotParams(NativeEngine &engine, NativeCallbackInfo &info, std::string &deviceId, int32_t &missionId) { - if (info.argc != ARG_COUNT_TWO && info.argc != ARG_COUNT_THREE) { - HILOG_ERROR("missionSnapshot: need two or three params"); - errCode = ERR_NOT_OK; + if (info.argc < ARG_COUNT_TWO) { + ThrowTooFewParametersError(engine); + return false; } - if (!errCode && !ConvertFromJsValue(engine, info.argv[0], deviceId)) { + + if (!ConvertFromJsValue(engine, info.argv[0], deviceId)) { HILOG_ERROR("missionSnapshot: Parse deviceId failed"); - errCode = ERR_NOT_OK; + ThrowError(engine, AbilityErrorCode::ERROR_CODE_INVALID_PARAM); + return false; } - if (!errCode && !ConvertFromJsValue(engine, info.argv[1], missionId)) { + + if (!ConvertFromJsValue(engine, info.argv[1], missionId)) { HILOG_ERROR("missionSnapshot: Parse missionId failed"); - errCode = ERR_NOT_OK; + ThrowError(engine, AbilityErrorCode::ERROR_CODE_INVALID_PARAM); + return false; } + + return true; } NativeValue* OnLockMission(NativeEngine &engine, NativeCallbackInfo &info) { HILOG_INFO("%{public}s is called", __FUNCTION__); - int32_t errCode = 0; if (info.argc == 0) { - HILOG_ERROR("Not enough params"); - errCode = ERR_NOT_OK; + HILOG_ERROR("OnLockMission Not enough params"); + ThrowTooFewParametersError(engine); + return engine.CreateUndefined(); } int32_t missionId = -1; - if (!errCode && !ConvertFromJsValue(engine, info.argv[0], missionId)) { - HILOG_ERROR("Parse missionId failed"); - errCode = ERR_NOT_OK; + if (!ConvertFromJsValue(engine, info.argv[0], missionId)) { + HILOG_ERROR("OnLockMission Parse missionId failed"); + ThrowError(engine, AbilityErrorCode::ERROR_CODE_INVALID_PARAM); + return engine.CreateUndefined(); } AsyncTask::CompleteCallback complete = - [missionId, errCode](NativeEngine &engine, AsyncTask &task, int32_t status) { - if (errCode != 0) { - task.Reject(engine, CreateJsError(engine, errCode, "Invalidate params.")); - return; - } + [missionId](NativeEngine &engine, AsyncTask &task, int32_t status) { auto ret = AbilityManagerClient::GetInstance()->LockMissionForCleanup(missionId); if (ret == 0) { task.Resolve(engine, engine.CreateUndefined()); } else { - task.Reject(engine, CreateJsError(engine, ret, "Lock mission failed.")); + task.Reject(engine, + CreateJsErrorByNativeErr(engine, ret, PermissionConstants::PERMISSION_MANAGE_MISSION)); } }; - NativeValue* lastParam = (info.argc <= 1) ? nullptr : info.argv[1]; + NativeValue* lastParam = (info.argc > 1) ? info.argv[1] : nullptr; NativeValue* result = nullptr; AsyncTask::Schedule("MissioManager::OnLockMission", engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result)); @@ -370,32 +377,30 @@ private: NativeValue* OnUnlockMission(NativeEngine &engine, NativeCallbackInfo &info) { HILOG_INFO("%{public}s is called", __FUNCTION__); - int32_t errCode = 0; if (info.argc == 0) { - HILOG_ERROR("Not enough params"); - errCode = ERR_NOT_OK; + HILOG_ERROR("OnUnlockMission Not enough params"); + ThrowTooFewParametersError(engine); + return engine.CreateUndefined(); } int32_t missionId = -1; - if (!errCode && !ConvertFromJsValue(engine, info.argv[0], missionId)) { - HILOG_ERROR("Parse missionId failed"); - errCode = ERR_NOT_OK; + if (!ConvertFromJsValue(engine, info.argv[0], missionId)) { + HILOG_ERROR("OnUnlockMission Parse missionId failed"); + ThrowError(engine, AbilityErrorCode::ERROR_CODE_INVALID_PARAM); + return engine.CreateUndefined(); } AsyncTask::CompleteCallback complete = - [missionId, errCode](NativeEngine &engine, AsyncTask &task, int32_t status) { - if (errCode != 0) { - task.Reject(engine, CreateJsError(engine, errCode, "Invalidate params.")); - return; - } + [missionId](NativeEngine &engine, AsyncTask &task, int32_t status) { auto ret = AbilityManagerClient::GetInstance()->UnlockMissionForCleanup(missionId); if (ret == 0) { task.Resolve(engine, engine.CreateUndefined()); } else { - task.Reject(engine, CreateJsError(engine, ret, "Unlock mission failed.")); + task.Reject(engine, + CreateJsErrorByNativeErr(engine, ret, PermissionConstants::PERMISSION_MANAGE_MISSION)); } }; - NativeValue* lastParam = (info.argc <= 1) ? nullptr : info.argv[1]; + NativeValue* lastParam = (info.argc > 1) ? info.argv[1] : nullptr; NativeValue* result = nullptr; AsyncTask::Schedule("MissioManager::OnUnlockMission", engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result)); @@ -405,32 +410,30 @@ private: NativeValue* OnClearMission(NativeEngine &engine, NativeCallbackInfo &info) { HILOG_INFO("%{public}s is called", __FUNCTION__); - int32_t errCode = 0; if (info.argc == 0) { - HILOG_ERROR("Not enough params"); - errCode = ERR_NOT_OK; + HILOG_ERROR("OnClearMission Not enough params"); + ThrowTooFewParametersError(engine); + return engine.CreateUndefined(); } int32_t missionId = -1; - if (!errCode && !ConvertFromJsValue(engine, info.argv[0], missionId)) { - HILOG_ERROR("Parse missionId failed"); - errCode = ERR_NOT_OK; + if (!ConvertFromJsValue(engine, info.argv[0], missionId)) { + HILOG_ERROR("OnClearMission Parse missionId failed"); + ThrowError(engine, AbilityErrorCode::ERROR_CODE_INVALID_PARAM); + return engine.CreateUndefined(); } AsyncTask::CompleteCallback complete = - [missionId, errCode](NativeEngine &engine, AsyncTask &task, int32_t status) { - if (errCode != 0) { - task.Reject(engine, CreateJsError(engine, errCode, "Invalidate params.")); - return; - } + [missionId](NativeEngine &engine, AsyncTask &task, int32_t status) { auto ret = AbilityManagerClient::GetInstance()->CleanMission(missionId); if (ret == 0) { task.Resolve(engine, engine.CreateUndefined()); } else { - task.Reject(engine, CreateJsError(engine, ret, "Clear mission failed.")); + task.Reject(engine, + CreateJsErrorByNativeErr(engine, ret, PermissionConstants::PERMISSION_MANAGE_MISSION)); } }; - NativeValue* lastParam = (info.argc <= 1) ? nullptr : info.argv[1]; + NativeValue* lastParam = (info.argc > 1) ? info.argv[1] : nullptr; NativeValue* result = nullptr; AsyncTask::Schedule("MissioManager::OnClearMission", engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result)); @@ -446,11 +449,12 @@ private: if (ret == 0) { task.Resolve(engine, engine.CreateUndefined()); } else { - task.Reject(engine, CreateJsError(engine, ret, "Clear all missions failed.")); + task.Reject(engine, + CreateJsErrorByNativeErr(engine, ret, PermissionConstants::PERMISSION_MANAGE_MISSION)); } }; - NativeValue* lastParam = (info.argc == 0) ? nullptr : info.argv[0]; + NativeValue* lastParam = (info.argc > 0) ? info.argv[0] : nullptr; NativeValue* result = nullptr; AsyncTask::Schedule("MissioManager::OnMoveMissionToFront", engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result)); @@ -460,15 +464,16 @@ private: NativeValue* OnMoveMissionToFront(NativeEngine &engine, NativeCallbackInfo &info) { HILOG_INFO("%{public}s is called", __FUNCTION__); - int32_t errCode = 0; if (info.argc == 0) { - HILOG_ERROR("Not enough params"); - errCode = ERR_NOT_OK; + HILOG_ERROR("OnMoveMissionToFront Not enough params"); + ThrowTooFewParametersError(engine); + return engine.CreateUndefined(); } int32_t missionId = -1; - if (!errCode && !ConvertFromJsValue(engine, info.argv[0], missionId)) { - HILOG_ERROR("Parse missionId failed"); - errCode = ERR_NOT_OK; + if (!ConvertFromJsValue(engine, info.argv[0], missionId)) { + HILOG_ERROR("OnMoveMissionToFront Parse missionId failed"); + ThrowError(engine, AbilityErrorCode::ERROR_CODE_INVALID_PARAM); + return engine.CreateUndefined(); } decltype(info.argc) unwrapArgc = 1; @@ -480,21 +485,18 @@ private: unwrapArgc++; } AsyncTask::CompleteCallback complete = - [missionId, errCode, startOptions, unwrapArgc](NativeEngine &engine, AsyncTask &task, int32_t status) { - if (errCode != 0) { - task.Reject(engine, CreateJsError(engine, errCode, "Invalidate params.")); - return; - } + [missionId, startOptions, unwrapArgc](NativeEngine &engine, AsyncTask &task, int32_t status) { auto ret = (unwrapArgc == 1) ? AbilityManagerClient::GetInstance()->MoveMissionToFront(missionId) : AbilityManagerClient::GetInstance()->MoveMissionToFront(missionId, startOptions); if (ret == 0) { task.Resolve(engine, engine.CreateUndefined()); } else { - task.Reject(engine, CreateJsError(engine, ret, "Move mission to front failed.")); + task.Reject(engine, + CreateJsErrorByNativeErr(engine, ret, PermissionConstants::PERMISSION_MANAGE_MISSION)); } }; - NativeValue* lastParam = (info.argc <= unwrapArgc) ? nullptr : info.argv[unwrapArgc]; + NativeValue* lastParam = (info.argc > unwrapArgc) ? info.argv[unwrapArgc] : nullptr; NativeValue* result = nullptr; AsyncTask::Schedule("MissioManager::OnMoveMissionToFront", engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result)); diff --git a/frameworks/native/ability/native/BUILD.gn b/frameworks/native/ability/native/BUILD.gn index cc538600bb..d0607ad107 100644 --- a/frameworks/native/ability/native/BUILD.gn +++ b/frameworks/native/ability/native/BUILD.gn @@ -489,3 +489,28 @@ ohos_shared_library("service_extension_module") { subsystem_name = "ability" part_name = "ability_runtime" } + +config("ability_business_error_config") { + visibility = [ ":*" ] + include_dirs = [ + "${ability_runtime_innerkits_path}/ability_manager/include", + "${ability_runtime_path}/interfaces/kits/native/ability/native/ability_business_error", + "//base/hiviewdfx/hiview/adapter/utility/include/extra", + ] +} + +config("ability_business_error_public_config") { + visibility = [ ":*" ] + include_dirs = [ "${ability_runtime_path}/interfaces/kits/native/ability/native/ability_business_error" ] +} + +ohos_shared_library("ability_business_error") { + sources = [ "${ability_runtime_native_path}/ability/native/ability_business_error/ability_business_error.cpp" ] + + configs = [ ":ability_business_error_config" ] + + public_configs = [ ":ability_business_error_public_config" ] + + subsystem_name = "ability" + part_name = "ability_runtime" +} diff --git a/frameworks/native/ability/native/ability_business_error/ability_business_error.cpp b/frameworks/native/ability/native/ability_business_error/ability_business_error.cpp new file mode 100755 index 0000000000..9773c9cdfc --- /dev/null +++ b/frameworks/native/ability/native/ability_business_error/ability_business_error.cpp @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ability_business_error.h" + +#include + +#include "ability_manager_errors.h" + +namespace OHOS { +namespace AbilityRuntime { +using namespace OHOS::AAFwk; +namespace { +constexpr const char* TAG_PERMISSION = " permission:"; +constexpr const char* ERROR_MSG_OK = "OK."; +constexpr const char* ERROR_MSG_PERMISSION_DENIED = "The application does not have permission to call the interface."; +constexpr const char* ERROR_MSG_INVALID_PARAM = "Invalid input parameter."; +constexpr const char* ERROR_MSG_SYSTEMCAP = "The specified SystemCapability name was not found."; +constexpr const char* ERROR_MSG_INNER = "Inner Error."; +constexpr const char* ERROR_MSG_NO_MISSION_ID = "The specified mission id does not exist."; +constexpr const char* ERROR_MSG_NO_MISSION_LISTENER = "The specified mission listener does not exist."; + +static std::unordered_map ERR_CODE_MAP = { + { AbilityErrorCode::ERROR_OK, ERROR_MSG_OK }, + { AbilityErrorCode::ERROR_CODE_PERMISSION_DENIED, ERROR_MSG_PERMISSION_DENIED }, + { AbilityErrorCode::ERROR_CODE_INVALID_PARAM, ERROR_MSG_INVALID_PARAM }, + { AbilityErrorCode::ERROR_CODE_SYSTEMCAP, ERROR_MSG_SYSTEMCAP }, + { AbilityErrorCode::ERROR_CODE_INNER, ERROR_MSG_INNER }, + { AbilityErrorCode::ERROR_CODE_NO_MISSION_ID, ERROR_MSG_NO_MISSION_ID }, + { AbilityErrorCode::ERROR_CODE_NO_MISSION_LISTENER, ERROR_MSG_NO_MISSION_LISTENER } +}; + +static std::unordered_map INNER_TO_JS_ERROR_CODE_MAP { + {0, AbilityErrorCode::ERROR_OK}, + {CHECK_PERMISSION_FAILED, AbilityErrorCode::ERROR_CODE_PERMISSION_DENIED}, + {MISSION_NOT_FOUND, AbilityErrorCode::ERROR_CODE_NO_MISSION_ID} +}; +} + +std::string GetErrorMsg(const AbilityErrorCode& errCode) +{ + auto it = ERR_CODE_MAP.find(errCode); + if (it != ERR_CODE_MAP.end()) { + return it->second; + } + + return ""; +} + +std::string GetNoPermissionErrorMsg(const std::string& permission) +{ + return std::string(ERROR_MSG_PERMISSION_DENIED) + std::string(TAG_PERMISSION) + permission; +} + +AbilityErrorCode GetJsErrorCodeByNativeError(int32_t errCode) +{ + auto it = INNER_TO_JS_ERROR_CODE_MAP.find(errCode); + if (it != INNER_TO_JS_ERROR_CODE_MAP.end()) { + return it->second; + } + + return AbilityErrorCode::ERROR_CODE_INNER; +} +} // namespace AbilityRuntime +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/native/runtime/js_error_utils.cpp b/frameworks/native/runtime/js_error_utils.cpp new file mode 100755 index 0000000000..b64c6b695d --- /dev/null +++ b/frameworks/native/runtime/js_error_utils.cpp @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#include "js_error_utils.h" + +#include "js_runtime_utils.h" + +namespace OHOS { +namespace AbilityRuntime { +namespace { +constexpr const char* ERR_MSG_TOO_FEW_PARAM = "Parameter error. Too few parameters."; +} + +void ThrowError(NativeEngine& engine, int32_t errCode, const std::string& errorMsg) +{ + engine.Throw(CreateJsError(engine, errCode, errorMsg)); +} + +void ThrowError(NativeEngine& engine, const AbilityErrorCode& err) +{ + engine.Throw(CreateJsError(engine, static_cast(err), GetErrorMsg(err))); +} + +void ThrowTooFewParametersError(NativeEngine& engine) +{ + engine.Throw(CreateJsError(engine, + static_cast(AbilityErrorCode::ERROR_CODE_INVALID_PARAM), + ERR_MSG_TOO_FEW_PARAM)); +} + +void ThrowNoPermissionError(NativeEngine& engine, const std::string& permission) +{ + engine.Throw(CreateJsError(engine, + static_cast(AbilityErrorCode::ERROR_CODE_PERMISSION_DENIED), + GetNoPermissionErrorMsg(permission))); +} + +NativeValue* CreateJsError(NativeEngine& engine, const AbilityErrorCode& err) +{ + return CreateJsError(engine, static_cast(err), GetErrorMsg(err)); +} + +NativeValue* CreateNoPermissionError(NativeEngine& engine, const std::string& permission) +{ + return CreateJsError(engine, + static_cast(AbilityErrorCode::ERROR_CODE_PERMISSION_DENIED), + GetNoPermissionErrorMsg(permission)); +} + +NativeValue* CreateJsErrorByNativeErr(NativeEngine& engine, int32_t err, const std::string& permission) +{ + auto errCode = GetJsErrorCodeByNativeError(err); + auto errMsg = (errCode == AbilityErrorCode::ERROR_CODE_PERMISSION_DENIED && !permission.empty()) ? + GetNoPermissionErrorMsg(permission) : GetErrorMsg(errCode); + return CreateJsError(engine, static_cast(errCode), errMsg); +} +} // namespace AbilityRuntime +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/inner_api/runtime/BUILD.gn b/interfaces/inner_api/runtime/BUILD.gn index 3537b492dc..12745bad1e 100644 --- a/interfaces/inner_api/runtime/BUILD.gn +++ b/interfaces/inner_api/runtime/BUILD.gn @@ -28,7 +28,10 @@ config("runtime_config") { config("runtime_public_config") { visibility = [ ":*" ] - include_dirs = [ "include" ] + include_dirs = [ + "${ability_runtime_path}/interfaces/kits/native/ability/native/ability_business_error", + "include", + ] } config("runtime_all_deps_config") { @@ -41,6 +44,7 @@ ohos_shared_library("runtime") { "${ability_runtime_native_path}/runtime/hdc_register.cpp", "${ability_runtime_native_path}/runtime/js_console_log.cpp", "${ability_runtime_native_path}/runtime/js_data_struct_converter.cpp", + "${ability_runtime_native_path}/runtime/js_error_utils.cpp", "${ability_runtime_native_path}/runtime/js_module_reader.cpp", "${ability_runtime_native_path}/runtime/js_module_searcher.cpp", "${ability_runtime_native_path}/runtime/js_runtime.cpp", @@ -63,6 +67,7 @@ ohos_shared_library("runtime") { all_dependent_configs = [ ":runtime_all_deps_config" ] deps = [ + "${ability_runtime_native_path}/ability/native:ability_business_error", "${ability_runtime_native_path}/runtime:runtime_extractor", "${ability_runtime_native_path}/runtime:string_utils", "//arkcompiler/ets_runtime:libark_jsruntime", diff --git a/interfaces/inner_api/runtime/include/js_error_utils.h b/interfaces/inner_api/runtime/include/js_error_utils.h new file mode 100755 index 0000000000..64207dd164 --- /dev/null +++ b/interfaces/inner_api/runtime/include/js_error_utils.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_ABILITY_RUNTIME_JS_ERROR_UTILS_H +#define OHOS_ABILITY_RUNTIME_JS_ERROR_UTILS_H + +#include "ability_business_error.h" +#include "native_engine/native_engine.h" + +namespace OHOS { +namespace AbilityRuntime { + +void ThrowError(NativeEngine& engine, int32_t errCode, const std::string& errorMsg = ""); +void ThrowError(NativeEngine& engine, const AbilityErrorCode& err); +void ThrowTooFewParametersError(NativeEngine& engine); +void ThrowNoPermissionError(NativeEngine& engine, const std::string& permission); + +NativeValue* CreateJsError(NativeEngine& engine, const AbilityErrorCode& err); +NativeValue* CreateNoPermissionError(NativeEngine& engine, const std::string& permission); +NativeValue* CreateJsErrorByNativeErr(NativeEngine& engine, int32_t err, const std::string& permission = ""); +} // namespace AbilityRuntime +} // namespace OHOS + +#endif // OHOS_ABILITY_RUNTIME_JS_ERROR_UTILS_H diff --git a/interfaces/kits/native/ability/native/ability_business_error/ability_business_error.h b/interfaces/kits/native/ability/native/ability_business_error/ability_business_error.h new file mode 100755 index 0000000000..26b022588c --- /dev/null +++ b/interfaces/kits/native/ability/native/ability_business_error/ability_business_error.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_ABILITY_RUNTIME_ABILITY_BUSINESS_ERROR_H +#define OHOS_ABILITY_RUNTIME_ABILITY_BUSINESS_ERROR_H + +#include + +namespace OHOS { +namespace AbilityRuntime { +enum class AbilityErrorCode { + ERROR_OK = 0, + ERROR_CODE_PERMISSION_DENIED = 201, + ERROR_CODE_INVALID_PARAM = 401, + ERROR_CODE_SYSTEMCAP = 801, + ERROR_CODE_INNER = 16000050, // inner error. + ERROR_CODE_NO_MISSION_ID = 16300001, + ERROR_CODE_NO_MISSION_LISTENER = 16300002, +}; + +std::string GetErrorMsg(const AbilityErrorCode& errCode); +std::string GetNoPermissionErrorMsg(const std::string& permission); +AbilityErrorCode GetJsErrorCodeByNativeError(int32_t errCode); + +} // namespace AbilityRuntime +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/abilitymgr/src/mission_info_mgr.cpp b/services/abilitymgr/src/mission_info_mgr.cpp index 3d6fcd2c3c..ed05b8238a 100644 --- a/services/abilitymgr/src/mission_info_mgr.cpp +++ b/services/abilitymgr/src/mission_info_mgr.cpp @@ -272,7 +272,7 @@ int MissionInfoMgr::GetInnerMissionInfoById(int32_t missionId, InnerMissionInfo std::lock_guard lock(mutex_); if (missionIdMap_.find(missionId) == missionIdMap_.end()) { HILOG_ERROR("missionId %{public}d not exists, get inner mission info failed", missionId); - return -1; + return MISSION_NOT_FOUND; } auto it = std::find_if(missionInfoList_.begin(), missionInfoList_.end(), @@ -282,7 +282,7 @@ int MissionInfoMgr::GetInnerMissionInfoById(int32_t missionId, InnerMissionInfo ); if (it == missionInfoList_.end()) { HILOG_ERROR("no such mission:%{public}d", missionId); - return -1; + return MISSION_NOT_FOUND; } innerMissionInfo = *it; return 0;