diff --git a/interfaces/plugin/vibrator/include/vibrator_napi_utils.h b/interfaces/plugin/vibrator/include/vibrator_napi_utils.h index de2a5e106b72fa57662203a245cfc5090bc36cf8..487ed6d91128e15e86d2da935193abfcd0c8d307 100644 --- a/interfaces/plugin/vibrator/include/vibrator_napi_utils.h +++ b/interfaces/plugin/vibrator/include/vibrator_napi_utils.h @@ -15,11 +15,15 @@ #ifndef VIBRATOR_NAPI_UTILS_H #define VIBRATOR_NAPI_UTILS_H -#include "napi/native_api.h" -#include "napi/native_node_api.h" #include #include +#include +#include + +#include "napi/native_api.h" +#include "napi/native_node_api.h" #include "refbase.h" +#include "sensors_errors.h" namespace OHOS { namespace Sensors { @@ -61,12 +65,39 @@ void EmitPromiseWork(sptr asyncCallbackInfo); napi_value GreateCallbackError(const napi_env &env, const int32_t errCode, const string errMessage, const string errName, const string errStack); +struct NapiError { + std::string errorCode; + std::string message; +}; + +const std::map ERROR_MESSAGES = { + {DEVICE_OPERATION_FAILED, {"14600101", "Device operation failed."}}, + {PERMISSION_DENIED, {"201", "Permission denied."}}, + {PARAMETER_ERROR, {"401", "The parameter invalid."}}, +}; + +inline const std::optional GetNapiError(int32_t errorCode) { + auto iter = ERROR_MESSAGES.find(errorCode); + if (iter != ERROR_MESSAGES.end()) { + return iter->second; + } + return std::nullopt; +} + +#define THROWERR(env, code, message) \ + do { \ + MISC_HILOGE("message: %{public}s, code: %{public}s", #message, (#code)); \ + auto error = GetNapiError(code); \ + if (error.has_value()) { \ + auto napiError = error.value(); \ + napi_throw_error(env, napiError.codes.c_str(), napiError.message.c_str()); \ + }\ + } while (0) + #define CHKNRR(env, state, message, retVal) \ do { \ if ((state) != napi_ok) { \ MISC_HILOGE("(%{public}s) fail", #message); \ - auto errDesc = std::string(__FUNCTION__) + ": " + #message; \ - napi_throw_error(env, nullptr, errDesc.c_str()); \ return retVal; \ } \ } while (0) @@ -75,8 +106,6 @@ napi_value GreateCallbackError(const napi_env &env, const int32_t errCode, do { \ if (!(cond)) { \ MISC_HILOGE("(%{public}s)", #message); \ - auto errDesc = std::string(__FUNCTION__) + ": " + #message; \ - napi_throw_error(env, nullptr, errDesc.c_str()); \ return nullptr; \ } \ } while (0) @@ -85,8 +114,6 @@ napi_value GreateCallbackError(const napi_env &env, const int32_t errCode, do { \ if ((state) != (ret)) { \ MISC_HILOGE("(%{public}s)", #message); \ - auto errDesc = std::string(__FUNCTION__) + ": " + #message; \ - napi_throw_error(env, nullptr, errDesc.c_str()); \ return false; \ } \ } while (0) diff --git a/utils/include/sensors_errors.h b/utils/include/sensors_errors.h index a14ec925ac463bf0afdac9748affde0b4bd016f6..d6ef9aa788d14d91261d04115e79b2c505e71a7e 100644 --- a/utils/include/sensors_errors.h +++ b/utils/include/sensors_errors.h @@ -23,6 +23,13 @@ namespace OHOS { namespace Sensors { +// Error code for user +enum ErrorCode : int32_t { + PERMISSION_DENIED = 201, // Use this error code when permission is denied. + PARAMETER_ERROR = 401, // Use this error code when the input parameter type or range does not match. + DEVICE_OPERATION_FAILED = 14600101 // Use this error code when operating the device fail. +}; + enum { MODULE_COMMON = 0x00, MODULE_MISCDEVICE_DEVICE = 0x01,