From 15b470fd2c5d46fcb026a5fa2cdaaddaa454fa50 Mon Sep 17 00:00:00 2001 From: cff-gite Date: Sat, 25 May 2024 09:30:57 +0800 Subject: [PATCH 01/12] miscdevice error code rectification Signed-off-by: cff-gite Change-Id: I1c8d3dadbcdf4d8bbd46de6082814adeb9f450be --- .../vibrator/include/vibrator_napi_error.h | 8 ++-- .../js/napi/vibrator/src/vibrator_js.cpp | 44 ++++++++++--------- .../napi/vibrator/src/vibrator_napi_error.cpp | 10 +++-- .../napi/vibrator/src/vibrator_napi_utils.cpp | 3 +- 4 files changed, 36 insertions(+), 29 deletions(-) diff --git a/frameworks/js/napi/vibrator/include/vibrator_napi_error.h b/frameworks/js/napi/vibrator/include/vibrator_napi_error.h index acf5e97..196e3e1 100644 --- a/frameworks/js/napi/vibrator/include/vibrator_napi_error.h +++ b/frameworks/js/napi/vibrator/include/vibrator_napi_error.h @@ -26,14 +26,14 @@ namespace OHOS { namespace Sensors { const std::map ERROR_MESSAGES = { {DEVICE_OPERATION_FAILED, "Device operation failed."}, - {PERMISSION_DENIED, "Permission denied."}, - {PARAMETER_ERROR, "The parameter invalid."}, + {PERMISSION_DENIED, "Permission denied. An attempt was made to %s forbidden by permission:%s."}, + {PARAMETER_ERROR, "Parameter error. The type of %s must be %s."}, {IS_NOT_SUPPORTED, "Capability not supported."}, }; napi_value CreateBusinessError(const napi_env &env, const int32_t errCode, const std::string &errMessage); -void ThrowErr(const napi_env &env, const int32_t errCode, const std::string &printMsg); -std::optional GetNapiError(int32_t errorCode); +void ThrowErr(const napi_env &env, const int32_t errCode, const std::string &printMsg1, const std::string &printMsg2); +std::optional GetNapiError(int32_t errorCode, const std::string &codeMsg); } // namespace Sensors } // namespace OHOS #endif // VIBRATOR_NAPI_ERROR_H \ No newline at end of file diff --git a/frameworks/js/napi/vibrator/src/vibrator_js.cpp b/frameworks/js/napi/vibrator/src/vibrator_js.cpp index 4c931ac..4856b55 100644 --- a/frameworks/js/napi/vibrator/src/vibrator_js.cpp +++ b/frameworks/js/napi/vibrator/src/vibrator_js.cpp @@ -232,14 +232,14 @@ static napi_value VibrateEffect(napi_env env, napi_value args[], size_t argc) { VibrateInfo info; if (!ParseParameter(env, args, argc, info)) { - ThrowErr(env, PARAMETER_ERROR, "parameter fail"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "parameter fail"); return nullptr; } sptr asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo(env); CHKPP(asyncCallbackInfo); asyncCallbackInfo->error.code = StartVibrate(info); if ((asyncCallbackInfo->error.code != SUCCESS) && (asyncCallbackInfo->error.code == PARAMETER_ERROR)) { - ThrowErr(env, PARAMETER_ERROR, "parameters invalid"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "parameters invalid"); return nullptr; } if (argc >= PARAMETER_THREE && IsMatchType(env, args[2], napi_function)) { @@ -257,11 +257,13 @@ static napi_value StartVibrate(napi_env env, napi_callback_info info) napi_value thisArg = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisArg, nullptr); if (status != napi_ok || argc < PARAMETER_TWO) { - ThrowErr(env, PARAMETER_ERROR, "napi_get_cb_info fail or number of parameter invalid"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", + "parameter must be greater than 3"); return nullptr; } if (!IsMatchType(env, args[0], napi_object) || !IsMatchType(env, args[1], napi_object)) { - ThrowErr(env, PARAMETER_ERROR, "args[0] and args[1] should is napi_object"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", + "args[0] and args[1] should is napi_object"); return nullptr; } return VibrateEffect(env, args, argc); @@ -276,7 +278,7 @@ static napi_value Vibrate(napi_env env, napi_callback_info info) napi_value thisArg = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisArg, nullptr); if (status != napi_ok) { - ThrowErr(env, PARAMETER_ERROR, "napi_get_cb_info fail"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "napi_get_cb_info fail"); return nullptr; } if (argc >= 1 && IsMatchType(env, args[0], napi_number)) { @@ -295,7 +297,7 @@ static napi_value Cancel(napi_env env, napi_callback_info info) napi_value thisArg = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisArg, nullptr); if (status != napi_ok) { - ThrowErr(env, PARAMETER_ERROR, "napi_get_cb_info fail"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "napi_get_cb_info fail"); return nullptr; } sptr asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo(env); @@ -314,20 +316,20 @@ static napi_value Stop(napi_env env, napi_callback_info info) napi_value thisArg = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisArg, nullptr); if (status != napi_ok) { - ThrowErr(env, PARAMETER_ERROR, "napi_get_cb_info fail"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "napi_get_cb_info fail"); return nullptr; } if (argc >= 1 && IsMatchType(env, args[0], napi_string)) { string mode; if (!GetStringValue(env, args[0], mode)) { - ThrowErr(env, PARAMETER_ERROR, "Parameters invalid"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "args[0] should is napi_object"); return nullptr; } sptr asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo(env); CHKPP(asyncCallbackInfo); asyncCallbackInfo->error.code = StopVibrator(mode.c_str()); if ((asyncCallbackInfo->error.code != SUCCESS) && (asyncCallbackInfo->error.code == PARAMETER_ERROR)) { - ThrowErr(env, PARAMETER_ERROR, "Parameters invalid"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Parameters invalid"); return nullptr; } if (argc >= PARAMETER_TWO && IsMatchType(env, args[1], napi_function)) { @@ -347,12 +349,12 @@ static napi_value StopVibrationSync(napi_env env, napi_callback_info info) size_t argc = 0; napi_status status = napi_get_cb_info(env, info, &argc, nullptr, &thisArg, nullptr); if (status != napi_ok) { - ThrowErr(env, PARAMETER_ERROR, "Get the parameter info fail"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Get the parameter info fail"); return result; } int32_t ret = Cancel(); if (ret != SUCCESS) { - ThrowErr(env, ret, "Cancel execution fail"); + ThrowErr(env, ret, "Cancel execution fail", "must be positive"); } return result; } @@ -365,12 +367,12 @@ static napi_value IsHdHapticSupported(napi_env env, napi_callback_info info) size_t argc = 0; napi_status status = napi_get_cb_info(env, info, &argc, nullptr, &thisArg, nullptr); if (status != napi_ok) { - ThrowErr(env, PARAMETER_ERROR, "Get the parameter info fail"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Get the parameter info fail"); return result; } status= napi_get_boolean(env, IsHdHapticSupported(), &result); if (status != napi_ok) { - ThrowErr(env, PARAMETER_ERROR, "Get the value of boolean fail"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Must get the value of boolean"); } return result; } @@ -382,12 +384,13 @@ static napi_value IsSupportEffect(napi_env env, napi_callback_info info) napi_value thisArg = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisArg, nullptr); if ((status != napi_ok) || (argc == 0)) { - ThrowErr(env, PARAMETER_ERROR, "napi_get_cb_info fail or number of parameter invalid"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", + "parameter must be greater than 2"); return nullptr; } string effectId; if (!GetStringValue(env, args[0], effectId)) { - ThrowErr(env, PARAMETER_ERROR, "GetStringValue fail"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "args[0] should is effectId"); return nullptr; } sptr asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo(env); @@ -409,23 +412,24 @@ static napi_value IsSupportEffectSync(napi_env env, napi_callback_info info) napi_value thisArg = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisArg, nullptr); if ((status != napi_ok) || (argc == 0)) { - ThrowErr(env, PARAMETER_ERROR, "Get the parameter info fail or number of parameter invalid"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", + "parameter must be greater than 1"); return result; } string effectId; if (!GetStringValue(env, args[0], effectId)) { - ThrowErr(env, PARAMETER_ERROR, "Get the value of string fail"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Must get the value of string"); return result; } bool isSupportEffect = false; int32_t ret = IsSupportEffect(effectId.c_str(), &isSupportEffect); if (ret != SUCCESS) { - ThrowErr(env, ret, "IsSupportEffect execution failed"); + ThrowErr(env, ret, "Parameter verification failed", "must be positive"); return result; } - status= napi_get_boolean(env, isSupportEffect, &result); + status = napi_get_boolean(env, isSupportEffect, &result); if (status != napi_ok) { - ThrowErr(env, PARAMETER_ERROR, "Get the value of boolean fail"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Must get the value of boolean"); } return result; } diff --git a/frameworks/js/napi/vibrator/src/vibrator_napi_error.cpp b/frameworks/js/napi/vibrator/src/vibrator_napi_error.cpp index e58bcf5..f77f10e 100644 --- a/frameworks/js/napi/vibrator/src/vibrator_napi_error.cpp +++ b/frameworks/js/napi/vibrator/src/vibrator_napi_error.cpp @@ -17,6 +17,7 @@ #include +#include "securec.h" #undef LOG_TAG #define LOG_TAG "VibratorNapiError" @@ -34,7 +35,7 @@ napi_value CreateBusinessError(const napi_env &env, const int32_t errCode, const return businessError; } -std::optional GetNapiError(int32_t errorCode) +std::optional GetNapiError(int32_t errorCode, const std::string &codeMsg) { auto iter = ERROR_MESSAGES.find(errorCode); if (iter != ERROR_MESSAGES.end()) { @@ -43,10 +44,11 @@ std::optional GetNapiError(int32_t errorCode) return std::nullopt; } -void ThrowErr(const napi_env &env, const int32_t errCode, const std::string &printMsg) +void ThrowErr(const napi_env &env, const int32_t errCode, const std::string &printMsg1, const std::string &printMsg2) { - MISC_HILOGE("Message:%{public}s, code:%{public}d", printMsg.c_str(), errCode); - auto msg = GetNapiError(errCode); + MISC_HILOGE("cff Message1:%{public}s, Message2:%{public}s,code:%{public}d", printMsg1.c_str(), printMsg2.c_str(), errCode); + std::string codeMsg; + auto msg = GetNapiError(errCode, codeMsg); if (!msg) { MISC_HILOGE("ErrCode:%{public}d is invalid", errCode); return; diff --git a/frameworks/js/napi/vibrator/src/vibrator_napi_utils.cpp b/frameworks/js/napi/vibrator/src/vibrator_napi_utils.cpp index 27a16dd..a32abb4 100644 --- a/frameworks/js/napi/vibrator/src/vibrator_napi_utils.cpp +++ b/frameworks/js/napi/vibrator/src/vibrator_napi_utils.cpp @@ -187,7 +187,8 @@ bool ConvertErrorToResult(const napi_env &env, sptr asyncCall { CHKPF(asyncCallbackInfo); int32_t code = asyncCallbackInfo->error.code; - auto msg = GetNapiError(code); + std::string codeMsg = asyncCallbackInfo->error.message; + auto msg = GetNapiError(code, codeMsg); if (!msg) { MISC_HILOGE("ErrCode:%{public}d is invalid", code); return false; -- Gitee From 50fd7e4a74e96edeff8ad76f322e4253f07abf00 Mon Sep 17 00:00:00 2001 From: cff-gite Date: Sat, 25 May 2024 09:35:33 +0800 Subject: [PATCH 02/12] miscdevice error code rectification Signed-off-by: cff-gite Change-Id: Ib200e3e6fa804540a04b497574f669f18aced5a1 --- frameworks/js/napi/vibrator/src/vibrator_js.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/js/napi/vibrator/src/vibrator_js.cpp b/frameworks/js/napi/vibrator/src/vibrator_js.cpp index 4856b55..7aa414b 100644 --- a/frameworks/js/napi/vibrator/src/vibrator_js.cpp +++ b/frameworks/js/napi/vibrator/src/vibrator_js.cpp @@ -258,7 +258,7 @@ static napi_value StartVibrate(napi_env env, napi_callback_info info) napi_status status = napi_get_cb_info(env, info, &argc, args, &thisArg, nullptr); if (status != napi_ok || argc < PARAMETER_TWO) { ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", - "parameter must be greater than 3"); + "parameter must be greater than 3"); return nullptr; } if (!IsMatchType(env, args[0], napi_object) || !IsMatchType(env, args[1], napi_object)) { -- Gitee From 414fd4e5ca953035e44a0b01d330bf66ff4fc578 Mon Sep 17 00:00:00 2001 From: cff-gite Date: Sat, 25 May 2024 09:50:10 +0800 Subject: [PATCH 03/12] miscdevice error code rectification Signed-off-by: cff-gite Change-Id: Id2158657984996816a0d97506dbe693183cdf1a6 --- frameworks/js/napi/vibrator/src/vibrator_js.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/frameworks/js/napi/vibrator/src/vibrator_js.cpp b/frameworks/js/napi/vibrator/src/vibrator_js.cpp index 7aa414b..76e30b8 100644 --- a/frameworks/js/napi/vibrator/src/vibrator_js.cpp +++ b/frameworks/js/napi/vibrator/src/vibrator_js.cpp @@ -263,7 +263,7 @@ static napi_value StartVibrate(napi_env env, napi_callback_info info) } if (!IsMatchType(env, args[0], napi_object) || !IsMatchType(env, args[1], napi_object)) { ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", - "args[0] and args[1] should is napi_object"); + "args[0] and args[1] should be napi_object"); return nullptr; } return VibrateEffect(env, args, argc); @@ -322,7 +322,7 @@ static napi_value Stop(napi_env env, napi_callback_info info) if (argc >= 1 && IsMatchType(env, args[0], napi_string)) { string mode; if (!GetStringValue(env, args[0], mode)) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "args[0] should is napi_object"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "args[0] should be napi_object"); return nullptr; } sptr asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo(env); @@ -390,7 +390,7 @@ static napi_value IsSupportEffect(napi_env env, napi_callback_info info) } string effectId; if (!GetStringValue(env, args[0], effectId)) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "args[0] should is effectId"); + ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "args[0] should be effectId"); return nullptr; } sptr asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo(env); -- Gitee From 57c55b0059f56b8516d87a667f0bbb02be1bb600 Mon Sep 17 00:00:00 2001 From: cff-gite Date: Sat, 25 May 2024 14:46:31 +0800 Subject: [PATCH 04/12] miscdevice error code rectification Signed-off-by: cff-gite Change-Id: I27551b80fad221b69a5297579680b78ccde6c1a0 --- frameworks/js/napi/vibrator/include/vibrator_napi_error.h | 2 +- frameworks/js/napi/vibrator/src/vibrator_napi_error.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/frameworks/js/napi/vibrator/include/vibrator_napi_error.h b/frameworks/js/napi/vibrator/include/vibrator_napi_error.h index 196e3e1..95bf19f 100644 --- a/frameworks/js/napi/vibrator/include/vibrator_napi_error.h +++ b/frameworks/js/napi/vibrator/include/vibrator_napi_error.h @@ -32,7 +32,7 @@ const std::map ERROR_MESSAGES = { }; napi_value CreateBusinessError(const napi_env &env, const int32_t errCode, const std::string &errMessage); -void ThrowErr(const napi_env &env, const int32_t errCode, const std::string &printMsg1, const std::string &printMsg2); +void ThrowErr(const napi_env &env, const int32_t errCode, const std::string &printMsg, const std::string &correctMsg); std::optional GetNapiError(int32_t errorCode, const std::string &codeMsg); } // namespace Sensors } // namespace OHOS diff --git a/frameworks/js/napi/vibrator/src/vibrator_napi_error.cpp b/frameworks/js/napi/vibrator/src/vibrator_napi_error.cpp index f77f10e..3a4928c 100644 --- a/frameworks/js/napi/vibrator/src/vibrator_napi_error.cpp +++ b/frameworks/js/napi/vibrator/src/vibrator_napi_error.cpp @@ -44,9 +44,9 @@ std::optional GetNapiError(int32_t errorCode, const std::string &co return std::nullopt; } -void ThrowErr(const napi_env &env, const int32_t errCode, const std::string &printMsg1, const std::string &printMsg2) +void ThrowErr(const napi_env &env, const int32_t errCode, const std::string &printMsg, const std::string &correctMsg) { - MISC_HILOGE("cff Message1:%{public}s, Message2:%{public}s,code:%{public}d", printMsg1.c_str(), printMsg2.c_str(), errCode); + MISC_HILOGE("correctMsg:%{public}s, correctMsg:%{public}s, code:%{public}d", printMsg.c_str(), correctMsg.c_str(), errCode); std::string codeMsg; auto msg = GetNapiError(errCode, codeMsg); if (!msg) { -- Gitee From 3423af279be313da2e6189109085823687d08f77 Mon Sep 17 00:00:00 2001 From: cff-gite Date: Mon, 27 May 2024 16:50:04 +0800 Subject: [PATCH 05/12] miscdevice error code rectification Signed-off-by: cff-gite Change-Id: I6745f3748b02b1fff76368bd816dbc885191bf0c --- .../vibrator/include/vibrator_napi_error.h | 6 +-- .../js/napi/vibrator/src/vibrator_js.cpp | 43 +++++++++---------- .../napi/vibrator/src/vibrator_napi_error.cpp | 26 +++++------ .../napi/vibrator/src/vibrator_napi_utils.cpp | 2 +- 4 files changed, 38 insertions(+), 39 deletions(-) diff --git a/frameworks/js/napi/vibrator/include/vibrator_napi_error.h b/frameworks/js/napi/vibrator/include/vibrator_napi_error.h index 95bf19f..ecdd149 100644 --- a/frameworks/js/napi/vibrator/include/vibrator_napi_error.h +++ b/frameworks/js/napi/vibrator/include/vibrator_napi_error.h @@ -26,14 +26,14 @@ namespace OHOS { namespace Sensors { const std::map ERROR_MESSAGES = { {DEVICE_OPERATION_FAILED, "Device operation failed."}, - {PERMISSION_DENIED, "Permission denied. An attempt was made to %s forbidden by permission:%s."}, - {PARAMETER_ERROR, "Parameter error. The type of %s must be %s."}, + {PERMISSION_DENIED, "Permission denied. An attempt was made to %s forbidden by permission:%s."}, + {PARAMETER_ERROR, "Parameter error. The type of %s must be %s."}, {IS_NOT_SUPPORTED, "Capability not supported."}, }; napi_value CreateBusinessError(const napi_env &env, const int32_t errCode, const std::string &errMessage); void ThrowErr(const napi_env &env, const int32_t errCode, const std::string &printMsg, const std::string &correctMsg); -std::optional GetNapiError(int32_t errorCode, const std::string &codeMsg); +bool GetNapiError(int32_t errorCode, std::string &codeMsg); } // namespace Sensors } // namespace OHOS #endif // VIBRATOR_NAPI_ERROR_H \ No newline at end of file diff --git a/frameworks/js/napi/vibrator/src/vibrator_js.cpp b/frameworks/js/napi/vibrator/src/vibrator_js.cpp index 76e30b8..e6507a9 100644 --- a/frameworks/js/napi/vibrator/src/vibrator_js.cpp +++ b/frameworks/js/napi/vibrator/src/vibrator_js.cpp @@ -232,14 +232,14 @@ static napi_value VibrateEffect(napi_env env, napi_value args[], size_t argc) { VibrateInfo info; if (!ParseParameter(env, args, argc, info)) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "parameter fail"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "enter the correct parameters"); return nullptr; } sptr asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo(env); CHKPP(asyncCallbackInfo); asyncCallbackInfo->error.code = StartVibrate(info); if ((asyncCallbackInfo->error.code != SUCCESS) && (asyncCallbackInfo->error.code == PARAMETER_ERROR)) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "parameters invalid"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "correct range"); return nullptr; } if (argc >= PARAMETER_THREE && IsMatchType(env, args[2], napi_function)) { @@ -257,13 +257,12 @@ static napi_value StartVibrate(napi_env env, napi_callback_info info) napi_value thisArg = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisArg, nullptr); if (status != napi_ok || argc < PARAMETER_TWO) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", - "parameter must be greater than 3"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", + "parameter number greater than 3"); return nullptr; } if (!IsMatchType(env, args[0], napi_object) || !IsMatchType(env, args[1], napi_object)) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", - "args[0] and args[1] should be napi_object"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "napi_object"); return nullptr; } return VibrateEffect(env, args, argc); @@ -278,7 +277,7 @@ static napi_value Vibrate(napi_env env, napi_callback_info info) napi_value thisArg = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisArg, nullptr); if (status != napi_ok) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "napi_get_cb_info fail"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "enter the correct parameters"); return nullptr; } if (argc >= 1 && IsMatchType(env, args[0], napi_number)) { @@ -297,7 +296,7 @@ static napi_value Cancel(napi_env env, napi_callback_info info) napi_value thisArg = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisArg, nullptr); if (status != napi_ok) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "napi_get_cb_info fail"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "enter the correct parameters"); return nullptr; } sptr asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo(env); @@ -316,20 +315,20 @@ static napi_value Stop(napi_env env, napi_callback_info info) napi_value thisArg = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisArg, nullptr); if (status != napi_ok) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "napi_get_cb_info fail"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "enter the correct parameters"); return nullptr; } if (argc >= 1 && IsMatchType(env, args[0], napi_string)) { string mode; if (!GetStringValue(env, args[0], mode)) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "args[0] should be napi_object"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "mode"); return nullptr; } sptr asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo(env); CHKPP(asyncCallbackInfo); asyncCallbackInfo->error.code = StopVibrator(mode.c_str()); if ((asyncCallbackInfo->error.code != SUCCESS) && (asyncCallbackInfo->error.code == PARAMETER_ERROR)) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Parameters invalid"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "correct range"); return nullptr; } if (argc >= PARAMETER_TWO && IsMatchType(env, args[1], napi_function)) { @@ -349,7 +348,7 @@ static napi_value StopVibrationSync(napi_env env, napi_callback_info info) size_t argc = 0; napi_status status = napi_get_cb_info(env, info, &argc, nullptr, &thisArg, nullptr); if (status != napi_ok) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Get the parameter info fail"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "enter the correct parameters"); return result; } int32_t ret = Cancel(); @@ -367,12 +366,12 @@ static napi_value IsHdHapticSupported(napi_env env, napi_callback_info info) size_t argc = 0; napi_status status = napi_get_cb_info(env, info, &argc, nullptr, &thisArg, nullptr); if (status != napi_ok) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Get the parameter info fail"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "enter the correct parameters"); return result; } status= napi_get_boolean(env, IsHdHapticSupported(), &result); if (status != napi_ok) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Must get the value of boolean"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "boolean"); } return result; } @@ -384,13 +383,13 @@ static napi_value IsSupportEffect(napi_env env, napi_callback_info info) napi_value thisArg = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisArg, nullptr); if ((status != napi_ok) || (argc == 0)) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", - "parameter must be greater than 2"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", + "parameter number greater than 2"); return nullptr; } string effectId; if (!GetStringValue(env, args[0], effectId)) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "args[0] should be effectId"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "effectId"); return nullptr; } sptr asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo(env); @@ -412,24 +411,24 @@ static napi_value IsSupportEffectSync(napi_env env, napi_callback_info info) napi_value thisArg = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, args, &thisArg, nullptr); if ((status != napi_ok) || (argc == 0)) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", - "parameter must be greater than 1"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", + "parameter number greater than 1"); return result; } string effectId; if (!GetStringValue(env, args[0], effectId)) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Must get the value of string"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "string"); return result; } bool isSupportEffect = false; int32_t ret = IsSupportEffect(effectId.c_str(), &isSupportEffect); if (ret != SUCCESS) { - ThrowErr(env, ret, "Parameter verification failed", "must be positive"); + ThrowErr(env, ret, "parameter verification failed", "must be positive"); return result; } status = napi_get_boolean(env, isSupportEffect, &result); if (status != napi_ok) { - ThrowErr(env, PARAMETER_ERROR, "Parameter verification failed", "Must get the value of boolean"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "boolean"); } return result; } diff --git a/frameworks/js/napi/vibrator/src/vibrator_napi_error.cpp b/frameworks/js/napi/vibrator/src/vibrator_napi_error.cpp index 3a4928c..7874884 100644 --- a/frameworks/js/napi/vibrator/src/vibrator_napi_error.cpp +++ b/frameworks/js/napi/vibrator/src/vibrator_napi_error.cpp @@ -35,29 +35,29 @@ napi_value CreateBusinessError(const napi_env &env, const int32_t errCode, const return businessError; } -std::optional GetNapiError(int32_t errorCode, const std::string &codeMsg) +bool GetNapiError(int32_t errorCode, std::string &codeMsg) { auto iter = ERROR_MESSAGES.find(errorCode); - if (iter != ERROR_MESSAGES.end()) { - return iter->second; + if (iter == ERROR_MESSAGES.end()) { + return false; } - return std::nullopt; + codeMsg = iter->second; + return true; } void ThrowErr(const napi_env &env, const int32_t errCode, const std::string &printMsg, const std::string &correctMsg) { MISC_HILOGE("correctMsg:%{public}s, correctMsg:%{public}s, code:%{public}d", printMsg.c_str(), correctMsg.c_str(), errCode); std::string codeMsg; - auto msg = GetNapiError(errCode, codeMsg); - if (!msg) { - MISC_HILOGE("ErrCode:%{public}d is invalid", errCode); - return; + if (GetNapiError(errCode, codeMsg)) { + MISC_HILOGE("Message:%{public}s", codeMsg.c_str()); + char buf[300]; + if (sprintf_s(buf, sizeof(buf), codeMsg.c_str(), printMsg.c_str(), correctMsg.c_str()) > 0 ) { + CreateBusinessError(env, errCode, buf); + } else { + MISC_HILOGE("Failed to convert string type to char type"); + } } - napi_handle_scope scope = nullptr; - napi_open_handle_scope(env, &scope); - napi_value error = CreateBusinessError(env, errCode, msg.value()); - napi_throw(env, error); - napi_close_handle_scope(env, scope); } } // namespace Sensors } // namespace OHOS \ No newline at end of file diff --git a/frameworks/js/napi/vibrator/src/vibrator_napi_utils.cpp b/frameworks/js/napi/vibrator/src/vibrator_napi_utils.cpp index a32abb4..aff5879 100644 --- a/frameworks/js/napi/vibrator/src/vibrator_napi_utils.cpp +++ b/frameworks/js/napi/vibrator/src/vibrator_napi_utils.cpp @@ -193,7 +193,7 @@ bool ConvertErrorToResult(const napi_env &env, sptr asyncCall MISC_HILOGE("ErrCode:%{public}d is invalid", code); return false; } - result = CreateBusinessError(env, code, msg.value()); + result = CreateBusinessError(env, code, codeMsg); return (result != nullptr); } -- Gitee From 1f4762cbf3c1d5a5f62a1cc53178b37f00a0395c Mon Sep 17 00:00:00 2001 From: cff-gite Date: Tue, 28 May 2024 15:50:49 +0800 Subject: [PATCH 06/12] miscdevice error code rectification Signed-off-by: cff-gite Change-Id: I20b11fd53582756b18da7cbc97faa7aae447c513 --- frameworks/js/napi/vibrator/src/vibrator_napi_error.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/frameworks/js/napi/vibrator/src/vibrator_napi_error.cpp b/frameworks/js/napi/vibrator/src/vibrator_napi_error.cpp index 7874884..6e3cb99 100644 --- a/frameworks/js/napi/vibrator/src/vibrator_napi_error.cpp +++ b/frameworks/js/napi/vibrator/src/vibrator_napi_error.cpp @@ -39,6 +39,7 @@ bool GetNapiError(int32_t errorCode, std::string &codeMsg) { auto iter = ERROR_MESSAGES.find(errorCode); if (iter == ERROR_MESSAGES.end()) { + MISC_HILOGE("errorCode %{public}d not found", errorCode); return false; } codeMsg = iter->second; @@ -50,9 +51,9 @@ void ThrowErr(const napi_env &env, const int32_t errCode, const std::string &pri MISC_HILOGE("correctMsg:%{public}s, correctMsg:%{public}s, code:%{public}d", printMsg.c_str(), correctMsg.c_str(), errCode); std::string codeMsg; if (GetNapiError(errCode, codeMsg)) { - MISC_HILOGE("Message:%{public}s", codeMsg.c_str()); char buf[300]; if (sprintf_s(buf, sizeof(buf), codeMsg.c_str(), printMsg.c_str(), correctMsg.c_str()) > 0 ) { + MISC_HILOGE("Message buf:%{public}s", buf); CreateBusinessError(env, errCode, buf); } else { MISC_HILOGE("Failed to convert string type to char type"); -- Gitee From c5f7a5e60fbaa19b545891960bd3890e978a6220 Mon Sep 17 00:00:00 2001 From: cff-gite Date: Thu, 30 May 2024 16:06:06 +0800 Subject: [PATCH 07/12] miscdevice error code rectification Signed-off-by: cff-gite Change-Id: I7a9b5bc64a60050ba6ccfe42aeeff6e2bc2b014b --- frameworks/js/napi/vibrator/src/vibrator_js.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/frameworks/js/napi/vibrator/src/vibrator_js.cpp b/frameworks/js/napi/vibrator/src/vibrator_js.cpp index e6507a9..32072cf 100644 --- a/frameworks/js/napi/vibrator/src/vibrator_js.cpp +++ b/frameworks/js/napi/vibrator/src/vibrator_js.cpp @@ -422,8 +422,11 @@ static napi_value IsSupportEffectSync(napi_env env, napi_callback_info info) } bool isSupportEffect = false; int32_t ret = IsSupportEffect(effectId.c_str(), &isSupportEffect); - if (ret != SUCCESS) { - ThrowErr(env, ret, "parameter verification failed", "must be positive"); + if (ret == PARAMETER_ERROR) { + ThrowErr(env, ret, "Parameter verification failed", "positive"); + return result; + } else if (ret != SUCCESS) { + ThrowErr(env, ret, "IsSupportEffect execution failed", "must be positive"); return result; } status = napi_get_boolean(env, isSupportEffect, &result); -- Gitee From 43ab4ed326ba9b0a3d61e98746a8290947a76177 Mon Sep 17 00:00:00 2001 From: cff-gite Date: Thu, 30 May 2024 16:40:42 +0800 Subject: [PATCH 08/12] miscdevice error code rectification Signed-off-by: cff-gite Change-Id: I7a119df4a072be1b50d1c8e8d88b8883aa0fd1fb --- frameworks/js/napi/vibrator/src/vibrator_napi_error.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/frameworks/js/napi/vibrator/src/vibrator_napi_error.cpp b/frameworks/js/napi/vibrator/src/vibrator_napi_error.cpp index 6e3cb99..51b223f 100644 --- a/frameworks/js/napi/vibrator/src/vibrator_napi_error.cpp +++ b/frameworks/js/napi/vibrator/src/vibrator_napi_error.cpp @@ -18,11 +18,16 @@ #include #include "securec.h" + #undef LOG_TAG #define LOG_TAG "VibratorNapiError" namespace OHOS { namespace Sensors { +namespace { +constexpr int32_t VIBRATE_BUFF = 300; +} // namespace + napi_value CreateBusinessError(const napi_env &env, const int32_t errCode, const std::string &errMessage) { napi_value businessError = nullptr; @@ -51,7 +56,7 @@ void ThrowErr(const napi_env &env, const int32_t errCode, const std::string &pri MISC_HILOGE("correctMsg:%{public}s, correctMsg:%{public}s, code:%{public}d", printMsg.c_str(), correctMsg.c_str(), errCode); std::string codeMsg; if (GetNapiError(errCode, codeMsg)) { - char buf[300]; + char buf[VIBRATE_BUFF]; if (sprintf_s(buf, sizeof(buf), codeMsg.c_str(), printMsg.c_str(), correctMsg.c_str()) > 0 ) { MISC_HILOGE("Message buf:%{public}s", buf); CreateBusinessError(env, errCode, buf); -- Gitee From e62022bceea7afeeaefe4ae1a32deac915926ab4 Mon Sep 17 00:00:00 2001 From: cff-gite Date: Fri, 31 May 2024 11:20:09 +0800 Subject: [PATCH 09/12] miscdevice error code rectification Signed-off-by: cff-gite Change-Id: I4a12bcf6d4143d681ce7a0698cdd94e32e6d16f8 --- .../vibrator/include/vibrator_napi_error.h | 10 ++++++- .../napi/vibrator/src/vibrator_napi_error.cpp | 28 +++++++++++++++++-- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/frameworks/js/napi/vibrator/include/vibrator_napi_error.h b/frameworks/js/napi/vibrator/include/vibrator_napi_error.h index ecdd149..f16776f 100644 --- a/frameworks/js/napi/vibrator/include/vibrator_napi_error.h +++ b/frameworks/js/napi/vibrator/include/vibrator_napi_error.h @@ -24,15 +24,23 @@ namespace OHOS { namespace Sensors { -const std::map ERROR_MESSAGES = { +const std::map ERROR_CODE_MESSAGES = { {DEVICE_OPERATION_FAILED, "Device operation failed."}, {PERMISSION_DENIED, "Permission denied. An attempt was made to %s forbidden by permission:%s."}, {PARAMETER_ERROR, "Parameter error. The type of %s must be %s."}, {IS_NOT_SUPPORTED, "Capability not supported."}, }; +const std::map ERROR_MESSAGES = { + {DEVICE_OPERATION_FAILED, "Device operation failed."}, + {PERMISSION_DENIED, "Permission denied."}, + {PARAMETER_ERROR, "The parameter invalid."}, + {IS_NOT_SUPPORTED, "Capability not supported."}, +}; + napi_value CreateBusinessError(const napi_env &env, const int32_t errCode, const std::string &errMessage); void ThrowErr(const napi_env &env, const int32_t errCode, const std::string &printMsg, const std::string &correctMsg); +std::optional GetNapiError(int32_t errorCode); bool GetNapiError(int32_t errorCode, std::string &codeMsg); } // namespace Sensors } // namespace OHOS diff --git a/frameworks/js/napi/vibrator/src/vibrator_napi_error.cpp b/frameworks/js/napi/vibrator/src/vibrator_napi_error.cpp index 51b223f..61bfcf8 100644 --- a/frameworks/js/napi/vibrator/src/vibrator_napi_error.cpp +++ b/frameworks/js/napi/vibrator/src/vibrator_napi_error.cpp @@ -42,8 +42,8 @@ napi_value CreateBusinessError(const napi_env &env, const int32_t errCode, const bool GetNapiError(int32_t errorCode, std::string &codeMsg) { - auto iter = ERROR_MESSAGES.find(errorCode); - if (iter == ERROR_MESSAGES.end()) { + auto iter = ERROR_CODE_MESSAGES.find(errorCode); + if (iter == ERROR_CODE_MESSAGES.end()) { MISC_HILOGE("errorCode %{public}d not found", errorCode); return false; } @@ -51,6 +51,15 @@ bool GetNapiError(int32_t errorCode, std::string &codeMsg) return true; } +std::optional GetNapiError(int32_t errorCode) +{ + auto iter = ERROR_MESSAGES.find(errorCode); + if (iter != ERROR_MESSAGES.end()) { + return iter->second; + } + return std::nullopt; +} + void ThrowErr(const napi_env &env, const int32_t errCode, const std::string &printMsg, const std::string &correctMsg) { MISC_HILOGE("correctMsg:%{public}s, correctMsg:%{public}s, code:%{public}d", printMsg.c_str(), correctMsg.c_str(), errCode); @@ -65,5 +74,20 @@ void ThrowErr(const napi_env &env, const int32_t errCode, const std::string &pri } } } + +void ThrowErr(const napi_env &env, const int32_t errCode, const std::string &printMsg) +{ + MISC_HILOGE("Message:%{public}s, code:%{public}d", printMsg.c_str(), errCode); + auto msg = GetNapiError(errCode); + if (!msg) { + MISC_HILOGE("ErrCode:%{public}d is invalid", errCode); + return; + } + napi_handle_scope scope = nullptr; + napi_open_handle_scope(env, &scope); + napi_value error = CreateBusinessError(env, errCode, msg.value()); + napi_throw(env, error); + napi_close_handle_scope(env, scope); +} } // namespace Sensors } // namespace OHOS \ No newline at end of file -- Gitee From 06ac01b88899f3cdf7eac7a9a62f8ac85cd47fc4 Mon Sep 17 00:00:00 2001 From: cff-gite Date: Fri, 31 May 2024 11:25:04 +0800 Subject: [PATCH 10/12] miscdevice error code rectification Signed-off-by: cff-gite Change-Id: I31f27cf1639bb3a3e2e4c3ab9ed318ddb0d5fe3e --- frameworks/js/napi/vibrator/src/vibrator_js.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/frameworks/js/napi/vibrator/src/vibrator_js.cpp b/frameworks/js/napi/vibrator/src/vibrator_js.cpp index 32072cf..687fe50 100644 --- a/frameworks/js/napi/vibrator/src/vibrator_js.cpp +++ b/frameworks/js/napi/vibrator/src/vibrator_js.cpp @@ -353,7 +353,7 @@ static napi_value StopVibrationSync(napi_env env, napi_callback_info info) } int32_t ret = Cancel(); if (ret != SUCCESS) { - ThrowErr(env, ret, "Cancel execution fail", "must be positive"); + ThrowErr(env, ret, "Cancel execution fail"); } return result; } @@ -422,11 +422,8 @@ static napi_value IsSupportEffectSync(napi_env env, napi_callback_info info) } bool isSupportEffect = false; int32_t ret = IsSupportEffect(effectId.c_str(), &isSupportEffect); - if (ret == PARAMETER_ERROR) { - ThrowErr(env, ret, "Parameter verification failed", "positive"); - return result; - } else if (ret != SUCCESS) { - ThrowErr(env, ret, "IsSupportEffect execution failed", "must be positive"); + if (ret != SUCCESS) { + ThrowErr(env, ret, "IsSupportEffect execution failed"); return result; } status = napi_get_boolean(env, isSupportEffect, &result); -- Gitee From af136968485e840c640f0254db72c8a9ecf5fb59 Mon Sep 17 00:00:00 2001 From: cff-gite Date: Thu, 6 Jun 2024 18:15:12 +0800 Subject: [PATCH 11/12] miscdevice error code rectification Signed-off-by: cff-gite Change-Id: I229a291540ed73e32a34b4114156d4f66e87a525 --- frameworks/js/napi/vibrator/src/vibrator_napi_error.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/frameworks/js/napi/vibrator/src/vibrator_napi_error.cpp b/frameworks/js/napi/vibrator/src/vibrator_napi_error.cpp index 61bfcf8..182797a 100644 --- a/frameworks/js/napi/vibrator/src/vibrator_napi_error.cpp +++ b/frameworks/js/napi/vibrator/src/vibrator_napi_error.cpp @@ -68,7 +68,11 @@ void ThrowErr(const napi_env &env, const int32_t errCode, const std::string &pri char buf[VIBRATE_BUFF]; if (sprintf_s(buf, sizeof(buf), codeMsg.c_str(), printMsg.c_str(), correctMsg.c_str()) > 0 ) { MISC_HILOGE("Message buf:%{public}s", buf); - CreateBusinessError(env, errCode, buf); + napi_handle_scope scope = nullptr; + napi_open_handle_scope(env, &scope); + napi_value error = CreateBusinessError(env, errCode, buf); + napi_throw(env, error); + napi_close_handle_scope(env, scope); } else { MISC_HILOGE("Failed to convert string type to char type"); } -- Gitee From c60f0755b4dae2dd8f596ec0716679bc9507202f Mon Sep 17 00:00:00 2001 From: cff-gite Date: Tue, 11 Jun 2024 16:12:23 +0800 Subject: [PATCH 12/12] miscdevice error code rectification Signed-off-by: cff-gite Change-Id: I205f9122465f68b62cb495e8c005757280fb2137 --- frameworks/js/napi/vibrator/include/vibrator_napi_error.h | 2 +- frameworks/js/napi/vibrator/src/vibrator_napi_error.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/frameworks/js/napi/vibrator/include/vibrator_napi_error.h b/frameworks/js/napi/vibrator/include/vibrator_napi_error.h index f16776f..70660eb 100644 --- a/frameworks/js/napi/vibrator/include/vibrator_napi_error.h +++ b/frameworks/js/napi/vibrator/include/vibrator_napi_error.h @@ -24,7 +24,7 @@ namespace OHOS { namespace Sensors { -const std::map ERROR_CODE_MESSAGES = { +const std::map ACCURATE_MESSAGES = { {DEVICE_OPERATION_FAILED, "Device operation failed."}, {PERMISSION_DENIED, "Permission denied. An attempt was made to %s forbidden by permission:%s."}, {PARAMETER_ERROR, "Parameter error. The type of %s must be %s."}, diff --git a/frameworks/js/napi/vibrator/src/vibrator_napi_error.cpp b/frameworks/js/napi/vibrator/src/vibrator_napi_error.cpp index 182797a..758b598 100644 --- a/frameworks/js/napi/vibrator/src/vibrator_napi_error.cpp +++ b/frameworks/js/napi/vibrator/src/vibrator_napi_error.cpp @@ -42,8 +42,8 @@ napi_value CreateBusinessError(const napi_env &env, const int32_t errCode, const bool GetNapiError(int32_t errorCode, std::string &codeMsg) { - auto iter = ERROR_CODE_MESSAGES.find(errorCode); - if (iter == ERROR_CODE_MESSAGES.end()) { + auto iter = ACCURATE_MESSAGES.find(errorCode); + if (iter == ACCURATE_MESSAGES.end()) { MISC_HILOGE("errorCode %{public}d not found", errorCode); return false; } -- Gitee