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 bool VerifyProperty(std::vector<std::string> &names, std::map<std::string, PrintParamStatus> &propertyList);
static std::string GetPrintErrorMsg(int32_t errorCode);
};
} // namespace OHOS::Print
#endif // NAPI_PRINT_UTILS_H

View File

@ -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<PrintErrorCode>(errorCode));
if (msg != PRINT_ERROR_MSG_MAP.end()) {
return msg->second;
}
return "";
}
} // namespace OHOS::Print

View File

@ -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 {

View File

@ -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<InnerPrintContext>();
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<PrintAsyncCall::Context>(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

View File

@ -17,6 +17,7 @@
#define PRINT_CONSTANT_H
#include <string>
#include <map>
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<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
#endif // PRINT_CONSTANT_H