diff --git a/utils/filemgmt_libn/include/n_error.h b/utils/filemgmt_libn/include/n_error.h index f7d0daa15b31a10b10a803bfa52c5c406d541eed..a86225a3039aa579e7e4c70225b6a532308837d2 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, @@ -294,6 +295,7 @@ public: napi_value GetNapiErr(napi_env env, int code); void ThrowErr(napi_env env); void ThrowErr(napi_env env, int code); + void ThrowErrAddData(napi_env env, int errCode, napi_value data); void ThrowErr(napi_env env, std::string errMsg); private: diff --git a/utils/filemgmt_libn/src/n_error.cpp b/utils/filemgmt_libn/src/n_error.cpp index 23b87cd91900890187b39158a7427f82cc9ff2ec..adcaac3a9f3304c320555bc2feb313c56e1a3e5d 100644 --- a/utils/filemgmt_libn/src/n_error.cpp +++ b/utils/filemgmt_libn/src/n_error.cpp @@ -126,6 +126,32 @@ void NError::ThrowErr(napi_env env, string errMsg) } } +void NError::ThrowErrAddData(napi_env env, int errCode, napi_value data) +{ + int32_t code; + 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;