diff --git a/frameworks/base/error/error_code.h b/frameworks/base/error/error_code.h index 718b997ebeaef4deefd1e89d069a600afb382be6..dcc97415c6724115a9cb6b05c5815071a87d3698 100644 --- a/frameworks/base/error/error_code.h +++ b/frameworks/base/error/error_code.h @@ -62,6 +62,10 @@ constexpr int32_t ERROR_CODE_UIEXTENSION_BACKGROUND_FAILED = 100019; constexpr int32_t ERROR_CODE_UIEXTENSION_DESTRUCTION_FAILED = 100020; // The uiextension transparent node detected. constexpr int32_t ERROR_CODE_UIEXTENSION_TRANSPARENT = 100021; +// The inspector get ui context failed. +constexpr int32_t ERROR_CODE_INSPECTOR_GET_UI_CONTEXT_FAILED = 100022; +// The inspector parameter depth must bu greater than zero. +constexpr int32_t ERROR_CODE_INSPECTOR_PARAM_DEPTH_INVALID = 100023; // C-API errors constexpr int32_t ERROR_CODE_NATIVE_IMPL_LIBRARY_NOT_FOUND = 106101; diff --git a/interfaces/ets/ani/inspector/src/inspector.cpp b/interfaces/ets/ani/inspector/src/inspector.cpp index 9dabf2a7838cb51a842c03dc97513bf531b8a397..51939e141d28dbd1da7268d32d87c0ea6e4f079f 100644 --- a/interfaces/ets/ani/inspector/src/inspector.cpp +++ b/interfaces/ets/ani/inspector/src/inspector.cpp @@ -15,6 +15,7 @@ #include #include #include +#include "base/error/error_code.h" #include "base/log/log_wrapper.h" #include "base/memory/ace_type.h" #include "bridge/arkts_frontend/arkts_frontend.h" @@ -280,7 +281,7 @@ static ani_string AniGetFilteredInspectorTree(ani_env *env, ani_array_ref filter bool needThrow = false; auto nodeInfos = NG::Inspector::GetInspector(isLayoutInspector, inspectorFilter, needThrow); if (needThrow) { - AniThrow(env, "get inspector failed"); + AniThrow(env, "Unable to obtain current ui context", ERROR_CODE_INSPECTOR_GET_UI_CONTEXT_FAILED); return nullptr; } ani_string result; @@ -294,7 +295,8 @@ static ani_string AniGetFilteredInspectorTree(ani_env *env, ani_array_ref filter static ani_string AniGetFilteredInspectorTreeById(ani_env *env, ani_string id, ani_double depth, ani_array_ref filters) { if (depth < 0) { - AniThrow(env, "invalid filter depth"); + AniThrow(env, "The parameter depth must be greater than 0.", + ERROR_CODE_INSPECTOR_PARAM_DEPTH_INVALID); return nullptr; } bool isLayoutInspector = false; @@ -310,7 +312,7 @@ static ani_string AniGetFilteredInspectorTreeById(ani_env *env, ani_string id, a bool needThrow = false; auto nodeInfos = NG::Inspector::GetInspector(false, inspectorFilter, needThrow); if (needThrow) { - AniThrow(env, "get inspector failed"); + AniThrow(env, "Unable to obtain current UI context", ERROR_CODE_INSPECTOR_GET_UI_CONTEXT_FAILED); return nullptr; } ani_string result; diff --git a/interfaces/ets/ani/inspector/src/inspector_util.cpp b/interfaces/ets/ani/inspector/src/inspector_util.cpp index 0591084575e26cb97a15e83387492ba46af7c827..568415e69d5b435d5bd668a65beef661855e9104 100644 --- a/interfaces/ets/ani/inspector/src/inspector_util.cpp +++ b/interfaces/ets/ani/inspector/src/inspector_util.cpp @@ -17,22 +17,45 @@ #include "frameworks/base/error/error_code.h" namespace OHOS::Ace { -void AniThrow(ani_env *env, const std::string &errMsg) +void AniThrow(ani_env *env, const std::string &errMsg, int32_t errorCode) { ani_class errCls; - const char* className = "escompat.Error"; - env->FindClass(className, &errCls); + if (ANI_OK != env->FindClass("L@ohos/base/BusinessError", &errCls)) { + TAG_LOGE(AceLogTag::ACE_LAYOUT_INSPECTOR, "FindClass BusinessError failed."); + return; + } ani_method errCtor {}; - env->Class_FindMethod(errCls, "", "C{std.core.String}C{escompat.ErrorOptions}:", &errCtor); - + if (ANI_OK != env->Class_FindMethod(errCls, "", "C{std.core.String}C{escompat.ErrorOptions}:", &errCtor)) { + TAG_LOGE(AceLogTag::ACE_LAYOUT_INSPECTOR, "FindMethod escompat.Error failed."); + return; + } ani_string resultString {}; - env->String_NewUTF8(errMsg.c_str(), errMsg.size(), &resultString); + if (ANI_OK != env->String_NewUTF8(errMsg.c_str(), errMsg.size(), &resultString)) { + TAG_LOGE(AceLogTag::ACE_LAYOUT_INSPECTOR, "Covert string to ani string failed."); + return; + } ani_ref undefinedRef {}; - env->GetUndefined(&undefinedRef); - + if (ANI_OK != env->GetUndefined(&undefinedRef)) { + TAG_LOGE(AceLogTag::ACE_LAYOUT_INSPECTOR, "Get undefined failed."); + return; + } ani_object errObj {}; - env->Object_New(errCls, errCtor, &errObj, resultString, undefinedRef); - env->ThrowError(static_cast(errObj)); + if (ANI_OK != env->Object_New(errCls, errCtor, &errObj, resultString, undefinedRef)) { + TAG_LOGE(AceLogTag::ACE_LAYOUT_INSPECTOR, "Create ani error object failed."); + return; + } + if (ANI_OK != env->Object_SetFieldByName_Int(errObj, "code", static_cast(errorCode))) { + TAG_LOGE(AceLogTag::ACE_LAYOUT_INSPECTOR, "Set error code failed."); + return; + } + if (ANI_OK != env->Object_SetFieldByName_Ref(errObj, "message", resultString)) { + TAG_LOGE(AceLogTag::ACE_LAYOUT_INSPECTOR, "Set error message failed."); + return; + } + if (ANI_OK != env->ThrowError(static_cast(errObj))) { + TAG_LOGE(AceLogTag::ACE_LAYOUT_INSPECTOR, "Throw ani error object failed."); + return; + } } ani_status ANIUtils_ANIStringToStdString(ani_env *env, ani_string ani_str, std::string& str) diff --git a/interfaces/ets/ani/inspector/src/inspector_util.h b/interfaces/ets/ani/inspector/src/inspector_util.h index e0afbacd3e3d9732da0a43985135daeb46120911..e56c82d6b76d76091ea9c7d258dd80d195c36f50 100644 --- a/interfaces/ets/ani/inspector/src/inspector_util.h +++ b/interfaces/ets/ani/inspector/src/inspector_util.h @@ -22,7 +22,7 @@ namespace OHOS::Ace { -void AniThrow(ani_env *env, const std::string &errMsg); +void AniThrow(ani_env *env, const std::string &errMsg, int32_t errorCode = 0); ani_status ANIUtils_ANIStringToStdString(ani_env *env, ani_string ani_str, std::string& str); NG::InspectorFilter GetInspectorFilter(ani_env *env, const ani_array_ref& filters, bool& isLayoutInspector); } // namespace OHOS::Ace