From 8d25581880551bc2f06a689a34ba78989cdbe71d Mon Sep 17 00:00:00 2001 From: 18721213663 Date: Thu, 6 Apr 2023 11:28:39 +0800 Subject: [PATCH] fs_libn_add_data Signed-off-by: 18721213663 --- utils/filemgmt_libn/include/n_error.h | 7 +++- utils/filemgmt_libn/src/n_error.cpp | 55 ++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/utils/filemgmt_libn/include/n_error.h b/utils/filemgmt_libn/include/n_error.h index f7d0daa15..87fb2cce5 100644 --- a/utils/filemgmt_libn/include/n_error.h +++ b/utils/filemgmt_libn/include/n_error.h @@ -34,6 +34,7 @@ constexpr int FILEIO_SYS_CAP_TAG = 13900000; constexpr int USER_FILE_MANAGER_SYS_CAP_TAG = 14000000; constexpr int USER_FILE_SERVICE_SYS_CAP_TAG = 14300000; const std::string FILEIO_TAG_ERR_CODE = "code"; +const std::string FILEIO_TAG_ERR_DATA = "data"; enum ErrCodeSuffixOfFileIO { E_PERM = 1, @@ -291,10 +292,12 @@ public: ~NError() = default; explicit operator bool() const; napi_value GetNapiErr(napi_env env); - napi_value GetNapiErr(napi_env env, int code); + napi_value GetNapiErr(napi_env env, int errCode); + napi_value GetNapiErrAddData(napi_env env, int errCode, napi_value data); void ThrowErr(napi_env env); - void ThrowErr(napi_env env, int code); + void ThrowErr(napi_env env, int errCode); void ThrowErr(napi_env env, std::string errMsg); + void ThrowErrAddData(napi_env env, int errCode, napi_value data); private: int errno_ = ERRNO_NOERR; diff --git a/utils/filemgmt_libn/src/n_error.cpp b/utils/filemgmt_libn/src/n_error.cpp index 23b87cd91..8e009481c 100644 --- a/utils/filemgmt_libn/src/n_error.cpp +++ b/utils/filemgmt_libn/src/n_error.cpp @@ -82,7 +82,7 @@ napi_value NError::GetNapiErr(napi_env env, int errCode) if (errCode == ERRNO_NOERR) { return nullptr; } - int32_t code; + int32_t code = 0; string msg; if (errCodeTable.find(errCode) != errCodeTable.end()) { code = errCodeTable.at(errCode).first; @@ -96,9 +96,34 @@ napi_value NError::GetNapiErr(napi_env env, int errCode) return GenerateBusinessError(env, code, msg); } +napi_value NError::GetNapiErrAddData(napi_env env, int errCode, napi_value data) +{ + if (errCode == ERRNO_NOERR) { + return nullptr; + } + int32_t code = 0; + string msg; + if (errCodeTable.find(errCode) != errCodeTable.end()) { + code = errCodeTable.at(errCode).first; + msg = errCodeTable.at(errCode).second; + } else { + code = errCodeTable.at(UNKROWN_ERR).first; + msg = errCodeTable.at(UNKROWN_ERR).second; + } + errno_ = code; + errMsg_ = msg; + napi_value businessError = GenerateBusinessError(env, code, msg); + napi_status status = napi_set_named_property(env, businessError, FILEIO_TAG_ERR_DATA.c_str(), data); + if (status != napi_ok) { + HILOGE("Failed to set data property on Error, error message is %{public}s", msg.c_str()); + return nullptr; + } + return businessError; +} + void NError::ThrowErr(napi_env env, int errCode) { - int32_t code; + int32_t code = 0; string msg; if (errCodeTable.find(errCode) != errCodeTable.end()) { code = errCodeTable.at(errCode).first; @@ -126,6 +151,32 @@ void NError::ThrowErr(napi_env env, string errMsg) } } +void NError::ThrowErrAddData(napi_env env, int errCode, napi_value data) +{ + int32_t code = 0; + string msg; + if (errCodeTable.find(errCode) != errCodeTable.end()) { + code = errCodeTable.at(errCode).first; + msg = errCodeTable.at(errCode).second; + } else { + code = errCodeTable.at(UNKROWN_ERR).first; + msg = errCodeTable.at(UNKROWN_ERR).second; + } + errno_ = code; + errMsg_ = msg; + napi_value businessError = GenerateBusinessError(env, code, msg); + napi_status status = napi_set_named_property(env, businessError, FILEIO_TAG_ERR_DATA.c_str(), data); + if (status != napi_ok) { + HILOGE("Failed to set data property on Error, error message is %{public}s", msg.c_str()); + return; + } + status = napi_throw(env, businessError); + if (status != napi_ok) { + HILOGE("Failed to throw a BusinessError, error message is %{public}s", msg.c_str()); + return; + } +} + void NError::ThrowErr(napi_env env) { napi_value tmp = nullptr; -- Gitee