diff --git a/frameworks/js/napi/vibrator/include/vibrator_napi_error.h b/frameworks/js/napi/vibrator/include/vibrator_napi_error.h index 0e3f83ea6ff62d3c4f41f0c381c2bf531eb47126..70660ebca727b76c65005e1b2c4f344e1f940001 100644 --- a/frameworks/js/napi/vibrator/include/vibrator_napi_error.h +++ b/frameworks/js/napi/vibrator/include/vibrator_napi_error.h @@ -24,6 +24,13 @@ namespace OHOS { namespace Sensors { +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."}, + {IS_NOT_SUPPORTED, "Capability not supported."}, +}; + const std::map ERROR_MESSAGES = { {DEVICE_OPERATION_FAILED, "Device operation failed."}, {PERMISSION_DENIED, "Permission denied."}, @@ -32,8 +39,9 @@ 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 &printMsg); +void ThrowErr(const napi_env &env, const int32_t errCode, const std::string &printMsg, const std::string &correctMsg); std::optional GetNapiError(int32_t errorCode); -} // namespace Sensors -} // namespace OHOS +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 dc0c49a86ba56bf794c94166ad06cbf50f1bbb58..50b1ee5aa15fbc4b7385646acd82cbe4d7242e7f 100644 --- a/frameworks/js/napi/vibrator/src/vibrator_js.cpp +++ b/frameworks/js/napi/vibrator/src/vibrator_js.cpp @@ -239,14 +239,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", "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, "parameters invalid"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "correct range"); return nullptr; } if (argc >= PARAMETER_THREE && IsMatchType(env, args[2], napi_function)) { @@ -264,11 +264,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, "napi_get_cb_info fail or number of parameter invalid"); + 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, "args[0] and args[1] should is napi_object"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "napi_object"); return nullptr; } return VibrateEffect(env, args, argc); @@ -283,7 +284,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", "enter the correct parameters"); return nullptr; } if (argc >= 1 && IsMatchType(env, args[0], napi_number)) { @@ -302,7 +303,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", "enter the correct parameters"); return nullptr; } sptr asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo(env); @@ -321,20 +322,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", "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, "Parameters invalid"); + 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, "Parameters invalid"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "correct range"); return nullptr; } if (argc >= PARAMETER_TWO && IsMatchType(env, args[1], napi_function)) { @@ -354,7 +355,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, "Get the parameter info fail"); + ThrowErr(env, PARAMETER_ERROR, "parameter verification failed", "enter the correct parameters"); return result; } int32_t ret = Cancel(); @@ -372,12 +373,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", "enter the correct parameters"); 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", "boolean"); } return result; } @@ -389,12 +390,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 number 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", "effectId"); return nullptr; } sptr asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo(env); @@ -416,12 +418,13 @@ 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 number 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", "string"); return result; } bool isSupportEffect = false; @@ -430,9 +433,9 @@ static napi_value IsSupportEffectSync(napi_env env, napi_callback_info info) ThrowErr(env, ret, "IsSupportEffect execution failed"); 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", "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 cdc963d6892793194d1644a39b6a2ee1b917e19c..71e8a7b27ec5b8a83e3b23d752513965e9fb4662 100644 --- a/frameworks/js/napi/vibrator/src/vibrator_napi_error.cpp +++ b/frameworks/js/napi/vibrator/src/vibrator_napi_error.cpp @@ -17,11 +17,17 @@ #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; @@ -34,6 +40,17 @@ napi_value CreateBusinessError(const napi_env &env, const int32_t errCode, const return businessError; } +bool GetNapiError(int32_t errorCode, std::string &codeMsg) +{ + auto iter = ACCURATE_MESSAGES.find(errorCode); + if (iter == ACCURATE_MESSAGES.end()) { + MISC_HILOGE("errorCode %{public}d not found", errorCode); + return false; + } + codeMsg = iter->second; + return true; +} + std::optional GetNapiError(int32_t errorCode) { auto iter = ERROR_MESSAGES.find(errorCode); @@ -43,6 +60,25 @@ std::optional GetNapiError(int32_t errorCode) 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); + std::string codeMsg; + if (GetNapiError(errCode, codeMsg)) { + 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); + 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"); + } + } +} + 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); diff --git a/frameworks/js/napi/vibrator/src/vibrator_napi_utils.cpp b/frameworks/js/napi/vibrator/src/vibrator_napi_utils.cpp index 738462b5352ffd611621e917fc334e159d0c5002..cf585e9226748cefbd36fd4230a7b519ddb4a1f6 100644 --- a/frameworks/js/napi/vibrator/src/vibrator_napi_utils.cpp +++ b/frameworks/js/napi/vibrator/src/vibrator_napi_utils.cpp @@ -187,12 +187,13 @@ 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; } - result = CreateBusinessError(env, code, msg.value()); + result = CreateBusinessError(env, code, codeMsg); return (result != nullptr); }