diff --git a/js_environment/frameworks/js_environment/src/uncaught_exception_callback.cpp b/js_environment/frameworks/js_environment/src/uncaught_exception_callback.cpp index 914d90c686d30fcae83df2ca17da05b1ea5bd616..2a4c5b9967908c0fb13a41583e0a8bd5684c139d 100644 --- a/js_environment/frameworks/js_environment/src/uncaught_exception_callback.cpp +++ b/js_environment/frameworks/js_environment/src/uncaught_exception_callback.cpp @@ -81,6 +81,7 @@ void NapiUncaughtExceptionCallback::operator()(panda::TryCatch& trycatch) void NapiUncaughtExceptionCallback::CallbackTask(napi_value& obj) { + HandleAndLogIfNotJsError(obj); std::string errorMsg = GetNativeStrFromJsTaggedObj(obj, "message"); std::string errorName = GetNativeStrFromJsTaggedObj(obj, "name"); std::string errorStack = GetNativeStrFromJsTaggedObj(obj, "stack"); @@ -122,6 +123,43 @@ void NapiUncaughtExceptionCallback::CallbackTask(napi_value& obj) } } +void NapiUncaughtExceptionCallback::HandleAndLogIfNotJsError(napi_value obj) +{ + bool isJsError = false; + napi_status napiRet = napi_is_error(env_, obj, &isJsError); + if (napiRet != napi_ok) { + TAG_LOGE(AAFwkTag::JSENV, "napi_is_error failed"); + return; + } + if (isJsError) { + TAG_LOGD(AAFwkTag::JSENV, "obj is JS Error"); + return; + } + panda::Local localVal = NapiValueToLocalValue(obj); + auto engine = reinterpret_cast(env_); + if (engine == nullptr) { + TAG_LOGE(AAFwkTag::JSENV, "null engine"); + return; + } + EcmaVM *vm = const_cast(reinterpret_cast(env_)->GetEcmaVm()); + if (vm == nullptr) { + TAG_LOGE(AAFwkTag::JSENV, "null vm"); + return; + } + panda::Local jsonVal = panda::JSON::Stringify(vm, localVal); + if (jsonVal.IsEmpty() || jsonVal->IsUndefined() || jsonVal->IsNull()) { + panda::JSNApi::GetAndClearUncaughtException(vm); + TAG_LOGE(AAFwkTag::JSENV, "null jsonVal"); + return; + } + panda::StringRef *strPtr = panda::StringRef::Cast(*jsonVal); + if (strPtr == nullptr) { + TAG_LOGE(AAFwkTag::JSENV, "null strPtr"); + return; + } + TAG_LOGE(AAFwkTag::JSENV, "Uncaught exception: %{public}s", strPtr->ToString(vm).c_str()); +} + std::string NapiUncaughtExceptionCallback::GetFuncNameAndBuildId(std::string nativeStack) { std::stringstream ss(nativeStack); diff --git a/js_environment/interfaces/inner_api/uncaught_exception_callback.h b/js_environment/interfaces/inner_api/uncaught_exception_callback.h index 3838a527cc8e6522ab6e046a99eab73dc7ba5eb6..43f9e35c44a58cb7e2fecee8d2a2401d43b2d3e6 100644 --- a/js_environment/interfaces/inner_api/uncaught_exception_callback.h +++ b/js_environment/interfaces/inner_api/uncaught_exception_callback.h @@ -52,6 +52,8 @@ public: void CallbackTask(napi_value& obj); + void HandleAndLogIfNotJsError(napi_value obj); + std::string GetNativeStrFromJsTaggedObj(napi_value obj, const char* key); static std::string GetFuncNameAndBuildId(std::string nativeStack);