From c8279282a339b12871e18f452b1a5e757297171b Mon Sep 17 00:00:00 2001 From: liuhui Date: Fri, 12 Sep 2025 12:17:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96inspector=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liuhui --- frameworks/base/error/error_code.h | 4 ++ .../ets/ani/inspector/src/inspector.cpp | 8 ++-- .../ets/ani/inspector/src/inspector_util.cpp | 43 ++++++++++++++----- .../ets/ani/inspector/src/inspector_util.h | 2 +- 4 files changed, 43 insertions(+), 14 deletions(-) diff --git a/frameworks/base/error/error_code.h b/frameworks/base/error/error_code.h index 718b997ebea..dcc97415c67 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 9dabf2a7838..51939e141d2 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 0591084575e..568415e69d5 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 e0afbacd3e3..e56c82d6b76 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 -- Gitee