diff --git a/frameworks/js/napi/file_access_module/napi_fileaccess_helper.cpp b/frameworks/js/napi/file_access_module/napi_fileaccess_helper.cpp index 67a2d7543db24ae7728f76cca316c673fd70c7c5..d50181b9c1f7ab1630aaa4e635395a673e4f109b 100644 --- a/frameworks/js/napi/file_access_module/napi_fileaccess_helper.cpp +++ b/frameworks/js/napi/file_access_module/napi_fileaccess_helper.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include "file_access_extension_info.h" #include "file_access_framework_errno.h" @@ -52,6 +53,11 @@ namespace { std::list> g_fileAccessHelperList = {}; +napi_value ThrowNapiError(napi_env env, int errorCode, const std::string& errorMessage = "") { + HILOG_ERROR("%{public}s", errorMessage.c_str()); + return NapiFileInfoExporter::ThrowError(env, errorCode); +} + static napi_value FileAccessHelperConstructor(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -63,13 +69,11 @@ static napi_value FileAccessHelperConstructor(napi_env env, napi_callback_info i bool isStageMode = false; napi_status status = AbilityRuntime::IsStageContext(env, funcArg.GetArg(PARAM0), isStageMode); if (status != napi_ok || !isStageMode) { - HILOG_INFO("No support FA Model"); - return NapiFileInfoExporter::ThrowError(env, EINVAL); + return ThrowNapiError(env, EINVAL, "No support FA Model"); } auto context = OHOS::AbilityRuntime::GetStageModeContext(env, funcArg.GetArg(PARAM0)); if (context == nullptr) { - HILOG_ERROR("FileAccessHelperConstructor: failed to get native context"); - return NapiFileInfoExporter::ThrowError(env, E_GETRESULT); + return ThrowNapiError(env, E_GETRESULT, "FileAccessHelperConstructor: failed to get native context"); } if (funcArg.GetArgc() == NARG_CNT::ONE) { createResult = FileAccessHelper::Creator(context); @@ -77,8 +81,7 @@ static napi_value FileAccessHelperConstructor(napi_env env, napi_callback_info i std::vector wants; bool suss = UnwrapArrayWantFromJS(env, funcArg.GetArg(PARAM1), wants); if (!suss) { - HILOG_ERROR("UnwrapArrayWantFromJS failed to get native wants"); - return NapiFileInfoExporter::ThrowError(env, E_GETRESULT); + return ThrowNapiError(env, E_GETRESULT, "UnwrapArrayWantFromJS failed to get native wants"); } createResult = FileAccessHelper::Creator(context, wants); } @@ -105,12 +108,27 @@ static napi_value FileAccessHelperConstructor(napi_env env, napi_callback_info i return thisVar; } +napi_value LogErrAndReturnNull(const std::string &message) { + HILOG_ERROR("%s", message.c_str()); + return nullptr; +} + +napi_value LogErrAndReturnNull(const std::string& baseMessage, size_t value) { + int requiredLength = std::snprintf(nullptr, 0, baseMessage.c_str(), value) + 1; + std::string formattedMessage; + formattedMessage.resize(requiredLength); + + std::snprintf(&formattedMessage[0], requiredLength, baseMessage.c_str(), value); + + HILOG_ERROR("%{public}s", formattedMessage.c_str()); + return nullptr; +} + napi_value AcquireFileAccessHelperWrap(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { - NError(EINVAL).ThrowErr(env); - return nullptr; + return ThrowNapiError(env, EINVAL); } napi_value result = nullptr; napi_value cons = nullptr; @@ -122,8 +140,7 @@ napi_value AcquireFileAccessHelperWrap(napi_env env, napi_callback_info info) return nullptr; } if (argc > requireArgc || napi_get_reference_value(env, g_constructorRef, &cons) != napi_ok) { - HILOG_ERROR("Wrong argument count%{public}zu. or g_constructorRef reference is fail", argc); - return nullptr; + return LogErrAndReturnNull("Wrong argument count%{public}zu. or g_constructorRef reference is fail", argc); } if (napi_new_instance(env, cons, ARGS_ONE, args, &result) != napi_ok) { return nullptr; @@ -136,25 +153,21 @@ napi_value AcquireFileAccessHelperWrap(napi_env env, napi_callback_info info) return nullptr; } if (argc > requireArgc || napi_get_reference_value(env, g_constructorRef, &cons) != napi_ok) { - HILOG_ERROR("Wrong argument count%{public}zu. or g_constructorRef reference is fail", argc); - return nullptr; + return LogErrAndReturnNull("Wrong argument count%{public}zu. or g_constructorRef reference is fail", argc); } if (napi_new_instance(env, cons, ARGS_TWO, args, &result) != napi_ok) { return nullptr; } } if (!IsTypeForNapiValue(env, result, napi_object)) { - HILOG_ERROR("IsTypeForNapiValue isn`t object"); - return nullptr; + return LogErrAndReturnNull("IsTypeForNapiValue isn`t object"); } FileAccessHelper *fileAccessHelper = nullptr; if (napi_unwrap(env, result, (void **)&fileAccessHelper) != napi_ok) { - HILOG_ERROR("Faild to get fileAccessHelper"); - return nullptr; + return LogErrAndReturnNull("Faild to get fileAccessHelper"); } if (fileAccessHelper == nullptr) { - HILOG_ERROR("fileAccessHelper is nullptr"); - return nullptr; + return LogErrAndReturnNull("fileAccessHelper is nullptr"); } HILOG_INFO("g_fileAccessHelperList size %{public}zu", g_fileAccessHelperList.size()); return result;