From c53cb2a8d97a48fa3db75d0e47b4e34c86a03197 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=94=B0=E8=BF=85?= Date: Thu, 14 Nov 2024 20:21:44 +0800 Subject: [PATCH] fix napi errCode MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 田迅 --- .../print_helper/include/napi_print_utils.h | 1 + .../print_helper/src/napi_print_utils.cpp | 9 +++ .../print_napi/include/napi_inner_print.h | 1 + .../napi/print_napi/src/napi_inner_print.cpp | 79 ++++++++++--------- utils/include/print_constant.h | 10 +++ 5 files changed, 63 insertions(+), 37 deletions(-) diff --git a/frameworks/helper/print_helper/include/napi_print_utils.h b/frameworks/helper/print_helper/include/napi_print_utils.h index 42d7a6a..7753b36 100644 --- a/frameworks/helper/print_helper/include/napi_print_utils.h +++ b/frameworks/helper/print_helper/include/napi_print_utils.h @@ -78,6 +78,7 @@ public: static size_t GetJsVal(napi_env env, napi_callback_info info, napi_value argv[], size_t length); static bool VerifyProperty(std::vector &names, std::map &propertyList); + static std::string GetPrintErrorMsg(int32_t errorCode); }; } // namespace OHOS::Print #endif // NAPI_PRINT_UTILS_H \ No newline at end of file diff --git a/frameworks/helper/print_helper/src/napi_print_utils.cpp b/frameworks/helper/print_helper/src/napi_print_utils.cpp index 2112032..9472402 100644 --- a/frameworks/helper/print_helper/src/napi_print_utils.cpp +++ b/frameworks/helper/print_helper/src/napi_print_utils.cpp @@ -346,4 +346,13 @@ bool NapiPrintUtils::VerifyProperty( } return true; } + +std::string NapiPrintUtils::GetPrintErrorMsg(int32_t errorCode) +{ + auto msg = PRINT_ERROR_MSG_MAP.find(static_cast(errorCode)); + if (msg != PRINT_ERROR_MSG_MAP.end()) { + return msg->second; + } + return ""; +} } // namespace OHOS::Print diff --git a/interfaces/kits/napi/print_napi/include/napi_inner_print.h b/interfaces/kits/napi/print_napi/include/napi_inner_print.h index c6e85c2..a805416 100644 --- a/interfaces/kits/napi/print_napi/include/napi_inner_print.h +++ b/interfaces/kits/napi/print_napi/include/napi_inner_print.h @@ -59,6 +59,7 @@ private: static bool IsSupportType(const std::string& type); static bool IsValidApplicationEvent(uint32_t event); static bool IsValidDefaultPrinterType(uint32_t type); + static napi_value NapiThrowError(napi_env env, int32_t errCode); private: struct InnerPrintContext : public PrintAsyncCall::Context { diff --git a/interfaces/kits/napi/print_napi/src/napi_inner_print.cpp b/interfaces/kits/napi/print_napi/src/napi_inner_print.cpp index 4e01cd0..b712e09 100644 --- a/interfaces/kits/napi/print_napi/src/napi_inner_print.cpp +++ b/interfaces/kits/napi/print_napi/src/napi_inner_print.cpp @@ -533,7 +533,7 @@ napi_value NapiInnerPrint::On(napi_env env, napi_callback_info info) if (!NapiInnerPrint::IsSupportType(type)) { PRINT_HILOGE("Event On type : %{public}s not support", type.c_str()); - return nullptr; + return NapiThrowError(env, E_PRINT_INVALID_PARAMETER); } valuetype = napi_undefined; @@ -550,7 +550,7 @@ napi_value NapiInnerPrint::On(napi_env env, napi_callback_info info) int32_t ret = PrintManagerClient::GetInstance()->On("", type, callback); if (ret != E_PRINT_NONE) { PRINT_HILOGE("Failed to register event"); - return nullptr; + return NapiThrowError(env, ret); } return nullptr; } @@ -558,40 +558,36 @@ napi_value NapiInnerPrint::On(napi_env env, napi_callback_info info) napi_value NapiInnerPrint::Off(napi_env env, napi_callback_info info) { PRINT_HILOGD("Enter ---->"); - auto context = std::make_shared(); - auto input = - [context]( - napi_env env, size_t argc, napi_value *argv, napi_value self, napi_callback_info info) -> napi_status { - PRINT_ASSERT_BASE(env, argc == NapiPrintUtils::ARGC_ONE, " should 1 parameter!", napi_invalid_arg); - napi_valuetype valuetype; - PRINT_CALL_BASE(env, napi_typeof(env, argv[NapiPrintUtils::INDEX_ZERO], &valuetype), napi_invalid_arg); - PRINT_ASSERT_BASE(env, valuetype == napi_string, "type is not a string", napi_string_expected); - std::string type = NapiPrintUtils::GetStringFromValueUtf8(env, argv[NapiPrintUtils::INDEX_ZERO]); - if (!NapiInnerPrint::IsSupportType(type)) { - PRINT_HILOGE("Event Off type : %{public}s not support", context->type.c_str()); - context->SetErrorIndex(E_PRINT_INVALID_PARAMETER); - return napi_invalid_arg; - } - context->type = type; - PRINT_HILOGD("event type : %{public}s", context->type.c_str()); - return napi_ok; - }; - auto output = [context](napi_env env, napi_value *result) -> napi_status { - napi_status status = napi_get_boolean(env, context->result, result); - PRINT_HILOGD("context->result = %{public}d", context->result); - return status; - }; - auto exec = [context](PrintAsyncCall::Context *ctx) { - int32_t ret = PrintManagerClient::GetInstance()->Off("", context->type); - context->result = ret == E_PRINT_NONE; - if (ret != E_PRINT_NONE) { - PRINT_HILOGE("Failed to unregister event"); - context->SetErrorIndex(ret); - } - }; - context->SetAction(std::move(input), std::move(output)); - PrintAsyncCall asyncCall(env, info, std::dynamic_pointer_cast(context)); - return asyncCall.Call(env, exec); + size_t argc = NapiPrintUtils::MAX_ARGC; + napi_value argv[NapiPrintUtils::MAX_ARGC] = { nullptr }; + napi_value thisVal = nullptr; + void *data = nullptr; + PRINT_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVal, &data)); + PRINT_ASSERT(env, argc == NapiPrintUtils::ARGC_ONE || argc == NapiPrintUtils::ARGC_TWO, "need 1-2 parameter!"); + + napi_valuetype valuetype; + PRINT_CALL(env, napi_typeof(env, argv[0], &valuetype)); + PRINT_ASSERT(env, valuetype == napi_string, "type is not a string"); + std::string type = NapiPrintUtils::GetStringFromValueUtf8(env, argv[0]); + PRINT_HILOGD("type : %{public}s", type.c_str()); + + if (!NapiInnerPrint::IsSupportType(type)) { + PRINT_HILOGE("Event Off type : %{public}s not support", type.c_str()); + return NapiThrowError(env, E_PRINT_INVALID_PARAMETER); + } + + if (argc == NapiPrintUtils::ARGC_TWO) { + valuetype = napi_undefined; + napi_typeof(env, argv[1], &valuetype); + PRINT_ASSERT(env, valuetype == napi_function, "callback is not a function"); + } + + int32_t ret = PrintManagerClient::GetInstance()->Off("", type); + if (ret != E_PRINT_NONE) { + PRINT_HILOGE("Failed to unregister event"); + return NapiThrowError(env, ret); + } + return nullptr; } napi_value NapiInnerPrint::StartGetPrintFile(napi_env env, napi_callback_info info) @@ -631,7 +627,7 @@ napi_value NapiInnerPrint::StartGetPrintFile(napi_env env, napi_callback_info in int32_t ret = PrintManagerClient::GetInstance()->StartGetPrintFile(jobId, *printAttributes, fd); if (ret != E_PRINT_NONE) { PRINT_HILOGE("Failed to StartGetPrintFile"); - return nullptr; + return NapiThrowError(env, ret); } } return nullptr; @@ -896,4 +892,13 @@ bool NapiInnerPrint::IsValidDefaultPrinterType(uint32_t type) } return false; } + +napi_value NapiInnerPrint::NapiThrowError(napi_env env, const int32_t errCode) +{ + napi_value result = nullptr; + napi_create_error(env, NapiPrintUtils::CreateInt32(env, errCode), + NapiPrintUtils::CreateStringUtf8(env, NapiPrintUtils::GetPrintErrorMsg(errCode)), &result); + napi_throw(env, result); + return nullptr; +} } // namespace OHOS::Print diff --git a/utils/include/print_constant.h b/utils/include/print_constant.h index 8245311..c03fc1d 100644 --- a/utils/include/print_constant.h +++ b/utils/include/print_constant.h @@ -17,6 +17,7 @@ #define PRINT_CONSTANT_H #include +#include namespace OHOS::Print { #define PRINT_RET_NONE @@ -239,5 +240,14 @@ const std::string PRINTER_LIST_FILE = "printer_list.json"; const std::string PRINTER_LIST_VERSION = "v1"; const std::string PRINT_USER_DATA_FILE = "print_user_data.json"; const std::string PRINT_USER_DATA_VERSION = "v1"; + +const std::string E_PRINT_MSG_NONE = "none"; +const std::string E_PRINT_MSG_NO_PERMISSION = "the application does not hace permission"; +const std::string E_PRINT_MSG_INVALID_PARAMETER = "parameter error"; +static std::map PRINT_ERROR_MSG_MAP { + {E_PRINT_NONE, E_PRINT_MSG_NONE }, + {E_PRINT_NO_PERMISSION, E_PRINT_MSG_NO_PERMISSION }, + {E_PRINT_INVALID_PARAMETER, E_PRINT_MSG_INVALID_PARAMETER }, +}; } // namespace OHOS::Print #endif // PRINT_CONSTANT_H