diff --git a/frameworks/native/runtime/ets_error_utils.cpp b/frameworks/native/runtime/ets_error_utils.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8a62938f17520bd493973f90b38d72e5ad49978a --- /dev/null +++ b/frameworks/native/runtime/ets_error_utils.cpp @@ -0,0 +1,237 @@ +/* + * Copyright (c) 2025 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 "ets_error_utils.h" +#include "hilog_tag_wrapper.h" + +namespace OHOS { +namespace AbilityRuntime { +namespace { +constexpr const char* ERR_MSG_TOO_FEW_PARAM = "Parameter error. Too few parameters."; +constexpr const char* ERR_MSG_NOT_MAINTHREAD = "Caller error. Caller from non-main thread."; +constexpr const char* ERR_MSG_INVALID_NUM_PARAMS = "Parameter error. The number of parameters is invalid."; +constexpr const char* NOT_SYSTEM_APP = "The application is not system-app, can not use system-api."; +constexpr const char* BUSINESS_ERROR_CLASS = "L@ohos/base/BusinessError;"; +} // namespace + +void ThrowEtsError(ani_env *env, ani_object err) +{ + if (env == nullptr) { + TAG_LOGE(AAFwkTag::ANI, "null env"); + return; + } + env->ThrowError(static_cast(err)); +} + +void ThrowEtsError(ani_env *env, int32_t errCode, const std::string &errorMsg) +{ + if (env == nullptr) { + TAG_LOGE(AAFwkTag::ANI, "null env"); + return; + } + ThrowEtsError(env, CreateEtsError(env, errCode, errorMsg)); +} + +void ThrowEtsError(ani_env *env, const AbilityErrorCode &err) +{ + if (env == nullptr) { + TAG_LOGE(AAFwkTag::ANI, "null env"); + return; + } + ThrowEtsError(env, CreateEtsError(env, static_cast(err), GetErrorMsg(err))); +} + +void ThrowEtsInvalidCallerError(ani_env *env) +{ + if (env == nullptr) { + TAG_LOGE(AAFwkTag::ANI, "null env"); + return; + } + ThrowEtsError(env, CreateEtsError(env, + static_cast(AbilityErrorCode::ERROR_CODE_INVALID_CALLER), + ERR_MSG_NOT_MAINTHREAD)); +} + +void ThrowEtsTooFewParametersError(ani_env *env) +{ + if (env == nullptr) { + TAG_LOGE(AAFwkTag::ANI, "null env"); + return; + } + ThrowEtsError(env, CreateEtsError(env, + static_cast(AbilityErrorCode::ERROR_CODE_INVALID_PARAM), + ERR_MSG_TOO_FEW_PARAM)); +} + +void ThrowEtsInvalidNumParametersError(ani_env *env) +{ + if (env == nullptr) { + TAG_LOGE(AAFwkTag::ANI, "null env"); + return; + } + ThrowEtsError(env, CreateEtsError(env, + static_cast(AbilityErrorCode::ERROR_CODE_INVALID_PARAM), + ERR_MSG_INVALID_NUM_PARAMS)); +} + +void ThrowEtsNoPermissionError(ani_env *env, const std::string &permission) +{ + if (env == nullptr) { + TAG_LOGE(AAFwkTag::ANI, "null env"); + return; + } + ThrowEtsError(env, CreateEtsError(env, + static_cast(AbilityErrorCode::ERROR_CODE_PERMISSION_DENIED), + GetNoPermissionErrorMsg(permission))); +} + +void ThrowEtsNotSystemAppError(ani_env *env) +{ + if (env == nullptr) { + TAG_LOGE(AAFwkTag::ANI, "null env"); + return; + } + ThrowEtsError(env, CreateEtsError(env, + static_cast(AbilityErrorCode::ERROR_CODE_NOT_SYSTEM_APP), + NOT_SYSTEM_APP)); +} + +void ThrowEtsInvalidParamError(ani_env *env, const std::string &message) +{ + if (env == nullptr) { + TAG_LOGE(AAFwkTag::ANI, "null env"); + return; + } + ThrowEtsError(env, CreateEtsInvalidParamError(env, message)); +} + +void ThrowEtsErrorByNativeErr(ani_env *env, int32_t err) +{ + if (env == nullptr) { + TAG_LOGE(AAFwkTag::ANI, "null env"); + return; + } + ThrowEtsError(env, CreateEtsErrorByNativeErr(env, err)); +} + +ani_object CreateEtsError(ani_env *env, const AbilityErrorCode &err) +{ + if (env == nullptr) { + TAG_LOGE(AAFwkTag::ANI, "null env"); + return nullptr; + } + return CreateEtsError(env, static_cast(err), GetErrorMsg(err)); +} + +ani_object CreateEtsInvalidParamError(ani_env *env, const std::string &message) +{ + if (env == nullptr) { + TAG_LOGE(AAFwkTag::ANI, "null env"); + return nullptr; + } + return CreateEtsError(env, static_cast(AbilityErrorCode::ERROR_CODE_INVALID_PARAM), message); +} + +ani_object CreateEtsNoPermissionError(ani_env *env, const std::string &permission) +{ + if (env == nullptr) { + TAG_LOGE(AAFwkTag::ANI, "null env"); + return nullptr; + } + return CreateEtsError(env, + static_cast(AbilityErrorCode::ERROR_CODE_PERMISSION_DENIED), + GetNoPermissionErrorMsg(permission)); +} + +ani_object CreateEtsErrorByNativeErr(ani_env *env, int32_t err, const std::string &permission) +{ + if (env == nullptr) { + TAG_LOGE(AAFwkTag::ANI, "null env"); + return nullptr; + } + auto errCode = GetJsErrorCodeByNativeError(err); + auto errMsg = (errCode == AbilityErrorCode::ERROR_CODE_PERMISSION_DENIED && !permission.empty()) ? + GetNoPermissionErrorMsg(permission) : GetErrorMsg(errCode); + return CreateEtsError(env, static_cast(errCode), errMsg); +} + +ani_object WrapEtsError(ani_env *env, const std::string &msg) +{ + if (env == nullptr) { + TAG_LOGE(AAFwkTag::ANI, "null env"); + return nullptr; + } + ani_status status = ANI_ERROR; + ani_string aniMsg = nullptr; + if ((status = env->String_NewUTF8(msg.c_str(), msg.size(), &aniMsg)) != ANI_OK) { + TAG_LOGE(AAFwkTag::ANI, "String_NewUTF8 failed %{public}d", status); + return nullptr; + } + ani_ref undefRef; + if ((status = env->GetUndefined(&undefRef)) != ANI_OK) { + TAG_LOGE(AAFwkTag::ANI, "GetUndefined failed %{public}d", status); + return nullptr; + } + ani_class cls = nullptr; + if ((status = env->FindClass("Lescompat/Error;", &cls)) != ANI_OK) { + TAG_LOGE(AAFwkTag::ANI, "FindClass failed %{public}d", status); + return nullptr; + } + ani_method method = nullptr; + if ((status = env->Class_FindMethod(cls, "", "Lstd/core/String;Lescompat/ErrorOptions;:V", &method)) != + ANI_OK) { + TAG_LOGE(AAFwkTag::ANI, "Class_FindMethod failed %{public}d", status); + return nullptr; + } + ani_object obj = nullptr; + if ((status = env->Object_New(cls, method, &obj, aniMsg, undefRef)) != ANI_OK) { + TAG_LOGE(AAFwkTag::ANI, "Object_New failed %{public}d", status); + return nullptr; + } + return obj; +} + +ani_object CreateEtsError(ani_env *env, ani_int code, const std::string &msg) +{ + if (env == nullptr) { + TAG_LOGE(AAFwkTag::ANI, "null env"); + return nullptr; + } + ani_status status = ANI_ERROR; + ani_class cls = nullptr; + if ((status = env->FindClass(BUSINESS_ERROR_CLASS, &cls)) != ANI_OK) { + TAG_LOGE(AAFwkTag::ANI, "FindClass failed %{public}d", status); + return nullptr; + } + ani_method method = nullptr; + if ((status = env->Class_FindMethod(cls, "", "DLescompat/Error;:V", &method)) != ANI_OK) { + TAG_LOGE(AAFwkTag::ANI, "Class_FindMethod failed %{public}d", status); + return nullptr; + } + ani_object error = WrapEtsError(env, msg); + if (error == nullptr) { + TAG_LOGE(AAFwkTag::ANI, "error nulll"); + return nullptr; + } + ani_object obj = nullptr; + ani_double dCode(code); + if ((status = env->Object_New(cls, method, &obj, dCode, error)) != ANI_OK) { + TAG_LOGE(AAFwkTag::ANI, "Object_New failed %{public}d", status); + return nullptr; + } + return obj; +} +} // 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 ace9ee9c4fa064a4378198a4aa06e711275dd0ce..a43b39b5421de2a170ecd0b3bf49c0139f5b80fd 100644 --- a/interfaces/inner_api/runtime/BUILD.gn +++ b/interfaces/inner_api/runtime/BUILD.gn @@ -60,6 +60,7 @@ ohos_shared_library("runtime") { sources = [ "${ability_runtime_native_path}/appkit/ability_bundle_manager_helper/bundle_mgr_helper.cpp", + "${ability_runtime_native_path}/runtime/ets_error_utils.cpp", "${ability_runtime_native_path}/runtime/ets_data_struct_converter.cpp", "${ability_runtime_native_path}/runtime/hdc_register.cpp", "${ability_runtime_native_path}/runtime/js_app_process_state.cpp", diff --git a/interfaces/inner_api/runtime/include/ets_error_utils.h b/interfaces/inner_api/runtime/include/ets_error_utils.h new file mode 100644 index 0000000000000000000000000000000000000000..78ce8dc990a4ef89ddfe9d37a11171679055d66e --- /dev/null +++ b/interfaces/inner_api/runtime/include/ets_error_utils.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 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_ETS_ERROR_UTILS_H +#define OHOS_ABILITY_RUNTIME_ETS_ERROR_UTILS_H + +#include "ability_business_error.h" +#include "ani.h" + +namespace OHOS { +namespace AbilityRuntime { +void ThrowEtsError(ani_env *env, ani_object err); +void ThrowEtsError(ani_env *env, int32_t errCode, const std::string &errorMsg = ""); +void ThrowEtsError(ani_env *env, const AbilityErrorCode &err); +void ThrowEtsInvalidCallerError(ani_env *env); +void ThrowEtsTooFewParametersError(ani_env *env); +void ThrowEtsInvalidNumParametersError(ani_env *env); +void ThrowEtsNoPermissionError(ani_env *env, const std::string &permission); +void ThrowEtsInvalidParamError(ani_env *env, const std::string &message); +void ThrowEtsErrorByNativeErr(ani_env *env, int32_t err); +void ThrowEtsNotSystemAppError(ani_env *env); + +ani_object CreateEtsError(ani_env *env, const AbilityErrorCode &err); +ani_object CreateEtsError(ani_env *env, ani_int code, const std::string &msg); +ani_object CreateEtsInvalidParamError(ani_env *env, const std::string &message); +ani_object CreateEtsNoPermissionError(ani_env *env, const std::string &permission); +ani_object CreateEtsErrorByNativeErr(ani_env *env, int32_t err, const std::string &permission = ""); +ani_object WrapEtsError(ani_env *env, const std::string &msg); +} // namespace AbilityRuntime +} // namespace OHOS +#endif // OHOS_ABILITY_RUNTIME_ETS_ERROR_UTILS_H