From 1a1362e3ac45d719403fd492feffe706b1b004f4 Mon Sep 17 00:00:00 2001 From: zhuruigan Date: Tue, 6 May 2025 20:37:35 +0800 Subject: [PATCH] error add data Signed-off-by: zhuruigan Change-Id: I5c7bf531f5ed4d03feb2b4d900f310e80092eede --- .../common/helper/error_helper.h | 21 ++++++++++++++++++- js_concurrent_module/taskpool/taskpool.cpp | 4 ++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/js_concurrent_module/common/helper/error_helper.h b/js_concurrent_module/common/helper/error_helper.h index f43ca40e..1fc2a080 100644 --- a/js_concurrent_module/common/helper/error_helper.h +++ b/js_concurrent_module/common/helper/error_helper.h @@ -34,7 +34,8 @@ public: ErrorHelper() = default; ~ErrorHelper() = default; - static napi_value NewError(napi_env env, int32_t errCode, const char* errMessage = nullptr) + static napi_value NewError(napi_env env, int32_t errCode, const char* errMessage = nullptr, + napi_value result = nullptr) { std::string errTitle = ""; napi_value concurrentError = nullptr; @@ -149,6 +150,24 @@ public: napi_create_error(env, nullptr, msg, &concurrentError); napi_set_named_property(env, concurrentError, "code", code); napi_set_named_property(env, concurrentError, "name", name); + if (errCode == ERR_ASYNCRUNNER_TASK_CANCELED || errCode == 0) { + napi_value data = nullptr; + napi_create_object(env, &data); + napi_value resultValue = nullptr; + napi_value errorValue = msg; + napi_get_undefined(env, &resultValue); + if (result != nullptr) { + napi_value error = NapiHelper::GetNameProperty(env, result, "error"); + if (NapiHelper::IsNotUndefined(env, error)) { + errorValue = error; + } else { + resultValue = result; + } + } + napi_set_named_property(env, data, "result", resultValue); + napi_set_named_property(env, data, "error", errorValue); + napi_set_named_property(env, concurrentError, "data", data); + } return concurrentError; } diff --git a/js_concurrent_module/taskpool/taskpool.cpp b/js_concurrent_module/taskpool/taskpool.cpp index e2637293..6bfc93c1 100644 --- a/js_concurrent_module/taskpool/taskpool.cpp +++ b/js_concurrent_module/taskpool/taskpool.cpp @@ -417,8 +417,8 @@ void TaskPool::HandleTaskResultInner(Task* task) if (task->taskState_ == ExecuteState::CANCELED) { strTrace += ", performResult : IsCanceled"; napiTaskResult = task->IsAsyncRunnerTask() ? - ErrorHelper::NewError(task->env_, ErrorHelper::ERR_ASYNCRUNNER_TASK_CANCELED) : - ErrorHelper::NewError(task->env_, 0, "taskpool:: task has been canceled"); + ErrorHelper::NewError(task->env_, ErrorHelper::ERR_ASYNCRUNNER_TASK_CANCELED, nullptr, napiTaskResult) : + ErrorHelper::NewError(task->env_, 0, "taskpool:: task has been canceled", napiTaskResult); } else if (status != napi_ok) { strTrace += ", performResult : DeserializeFailed"; taskLog += ", DeserializeFailed"; -- Gitee