fix napi errCode

Signed-off-by: 田迅 <tianxun3@huawei.com>
This commit is contained in:
田迅 2024-11-14 20:21:44 +08:00
parent d98ee143b9
commit c53cb2a8d9
5 changed files with 63 additions and 37 deletions

View File

@ -78,6 +78,7 @@ public:
static size_t GetJsVal(napi_env env, napi_callback_info info, napi_value argv[], size_t length); static size_t GetJsVal(napi_env env, napi_callback_info info, napi_value argv[], size_t length);
static bool VerifyProperty(std::vector<std::string> &names, std::map<std::string, PrintParamStatus> &propertyList); static bool VerifyProperty(std::vector<std::string> &names, std::map<std::string, PrintParamStatus> &propertyList);
static std::string GetPrintErrorMsg(int32_t errorCode);
}; };
} // namespace OHOS::Print } // namespace OHOS::Print
#endif // NAPI_PRINT_UTILS_H #endif // NAPI_PRINT_UTILS_H

View File

@ -346,4 +346,13 @@ bool NapiPrintUtils::VerifyProperty(
} }
return true; return true;
} }
std::string NapiPrintUtils::GetPrintErrorMsg(int32_t errorCode)
{
auto msg = PRINT_ERROR_MSG_MAP.find(static_cast<PrintErrorCode>(errorCode));
if (msg != PRINT_ERROR_MSG_MAP.end()) {
return msg->second;
}
return "";
}
} // namespace OHOS::Print } // namespace OHOS::Print

View File

@ -59,6 +59,7 @@ private:
static bool IsSupportType(const std::string& type); static bool IsSupportType(const std::string& type);
static bool IsValidApplicationEvent(uint32_t event); static bool IsValidApplicationEvent(uint32_t event);
static bool IsValidDefaultPrinterType(uint32_t type); static bool IsValidDefaultPrinterType(uint32_t type);
static napi_value NapiThrowError(napi_env env, int32_t errCode);
private: private:
struct InnerPrintContext : public PrintAsyncCall::Context { struct InnerPrintContext : public PrintAsyncCall::Context {

View File

@ -533,7 +533,7 @@ napi_value NapiInnerPrint::On(napi_env env, napi_callback_info info)
if (!NapiInnerPrint::IsSupportType(type)) { if (!NapiInnerPrint::IsSupportType(type)) {
PRINT_HILOGE("Event On type : %{public}s not support", type.c_str()); PRINT_HILOGE("Event On type : %{public}s not support", type.c_str());
return nullptr; return NapiThrowError(env, E_PRINT_INVALID_PARAMETER);
} }
valuetype = napi_undefined; 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); int32_t ret = PrintManagerClient::GetInstance()->On("", type, callback);
if (ret != E_PRINT_NONE) { if (ret != E_PRINT_NONE) {
PRINT_HILOGE("Failed to register event"); PRINT_HILOGE("Failed to register event");
return nullptr; return NapiThrowError(env, ret);
} }
return nullptr; 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) napi_value NapiInnerPrint::Off(napi_env env, napi_callback_info info)
{ {
PRINT_HILOGD("Enter ---->"); PRINT_HILOGD("Enter ---->");
auto context = std::make_shared<InnerPrintContext>(); size_t argc = NapiPrintUtils::MAX_ARGC;
auto input = napi_value argv[NapiPrintUtils::MAX_ARGC] = { nullptr };
[context]( napi_value thisVal = nullptr;
napi_env env, size_t argc, napi_value *argv, napi_value self, napi_callback_info info) -> napi_status { void *data = nullptr;
PRINT_ASSERT_BASE(env, argc == NapiPrintUtils::ARGC_ONE, " should 1 parameter!", napi_invalid_arg); 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; napi_valuetype valuetype;
PRINT_CALL_BASE(env, napi_typeof(env, argv[NapiPrintUtils::INDEX_ZERO], &valuetype), napi_invalid_arg); PRINT_CALL(env, napi_typeof(env, argv[0], &valuetype));
PRINT_ASSERT_BASE(env, valuetype == napi_string, "type is not a string", napi_string_expected); PRINT_ASSERT(env, valuetype == napi_string, "type is not a string");
std::string type = NapiPrintUtils::GetStringFromValueUtf8(env, argv[NapiPrintUtils::INDEX_ZERO]); std::string type = NapiPrintUtils::GetStringFromValueUtf8(env, argv[0]);
PRINT_HILOGD("type : %{public}s", type.c_str());
if (!NapiInnerPrint::IsSupportType(type)) { if (!NapiInnerPrint::IsSupportType(type)) {
PRINT_HILOGE("Event Off type : %{public}s not support", context->type.c_str()); PRINT_HILOGE("Event Off type : %{public}s not support", type.c_str());
context->SetErrorIndex(E_PRINT_INVALID_PARAMETER); return NapiThrowError(env, E_PRINT_INVALID_PARAMETER);
return napi_invalid_arg;
} }
context->type = type;
PRINT_HILOGD("event type : %{public}s", context->type.c_str()); if (argc == NapiPrintUtils::ARGC_TWO) {
return napi_ok; valuetype = napi_undefined;
}; napi_typeof(env, argv[1], &valuetype);
auto output = [context](napi_env env, napi_value *result) -> napi_status { PRINT_ASSERT(env, valuetype == napi_function, "callback is not a function");
napi_status status = napi_get_boolean(env, context->result, result); }
PRINT_HILOGD("context->result = %{public}d", context->result);
return status; int32_t ret = PrintManagerClient::GetInstance()->Off("", type);
};
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) { if (ret != E_PRINT_NONE) {
PRINT_HILOGE("Failed to unregister event"); PRINT_HILOGE("Failed to unregister event");
context->SetErrorIndex(ret); return NapiThrowError(env, ret);
} }
}; return nullptr;
context->SetAction(std::move(input), std::move(output));
PrintAsyncCall asyncCall(env, info, std::dynamic_pointer_cast<PrintAsyncCall::Context>(context));
return asyncCall.Call(env, exec);
} }
napi_value NapiInnerPrint::StartGetPrintFile(napi_env env, napi_callback_info info) 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); int32_t ret = PrintManagerClient::GetInstance()->StartGetPrintFile(jobId, *printAttributes, fd);
if (ret != E_PRINT_NONE) { if (ret != E_PRINT_NONE) {
PRINT_HILOGE("Failed to StartGetPrintFile"); PRINT_HILOGE("Failed to StartGetPrintFile");
return nullptr; return NapiThrowError(env, ret);
} }
} }
return nullptr; return nullptr;
@ -896,4 +892,13 @@ bool NapiInnerPrint::IsValidDefaultPrinterType(uint32_t type)
} }
return false; 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 } // namespace OHOS::Print

View File

@ -17,6 +17,7 @@
#define PRINT_CONSTANT_H #define PRINT_CONSTANT_H
#include <string> #include <string>
#include <map>
namespace OHOS::Print { namespace OHOS::Print {
#define PRINT_RET_NONE #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 PRINTER_LIST_VERSION = "v1";
const std::string PRINT_USER_DATA_FILE = "print_user_data.json"; const std::string PRINT_USER_DATA_FILE = "print_user_data.json";
const std::string PRINT_USER_DATA_VERSION = "v1"; 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<PrintErrorCode, const std::string> 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 } // namespace OHOS::Print
#endif // PRINT_CONSTANT_H #endif // PRINT_CONSTANT_H