From 06fe59757984bf397fa088d3a1ac6d29fdbc669c Mon Sep 17 00:00:00 2001 From: h00514358 Date: Tue, 27 Sep 2022 14:15:50 +0800 Subject: [PATCH] Add API error code definition Signed-off-by: h00514358 --- .../vibrator/include/vibrator_napi_utils.h | 43 +++++++++++++++---- utils/include/sensors_errors.h | 7 +++ 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/interfaces/plugin/vibrator/include/vibrator_napi_utils.h b/interfaces/plugin/vibrator/include/vibrator_napi_utils.h index de2a5e1..487ed6d 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 a14ec92..d6ef9aa 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, -- Gitee