From 15b97799f5e385779576850946427f5453e469f2 Mon Sep 17 00:00:00 2001 From: liuhonglin9 Date: Tue, 12 Dec 2023 12:10:28 +0000 Subject: [PATCH] BigFunctionRefactoring Signed-off-by: liuhonglin9 Change-Id: I8c9c4712ad83e26c6aa072cbd5099b174c4a6eed --- .../napi_fileaccess_helper.cpp | 42 ++--- .../file_access/src/file_access_helper.cpp | 130 +++++++++------- .../src/js_file_access_ext_ability.cpp | 147 +++++++----------- 3 files changed, 153 insertions(+), 166 deletions(-) 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 67a2d754..713e875c 100644 --- a/frameworks/js/napi/file_access_module/napi_fileaccess_helper.cpp +++ b/frameworks/js/napi/file_access_module/napi_fileaccess_helper.cpp @@ -52,6 +52,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 +68,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 +80,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 +107,21 @@ 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 &format, size_t argc) { + HILOG_ERROR(format.c_str(), argc); + 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 +133,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 +146,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; diff --git a/interfaces/inner_api/file_access/src/file_access_helper.cpp b/interfaces/inner_api/file_access/src/file_access_helper.cpp index cfd22d1d..3bb605bc 100644 --- a/interfaces/inner_api/file_access/src/file_access_helper.cpp +++ b/interfaces/inner_api/file_access/src/file_access_helper.cpp @@ -174,19 +174,48 @@ static bool IsSystemApp() return OHOS::Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(accessTokenIDEx); } +std::pair, int> CreateFileAccessExtConnection(const AppExecFwk::ExtensionAbilityInfo& extInfo, const std::shared_ptr& context) { + AAFwk::Want want; + want.SetElementName(extInfo.bundleName, extInfo.name); + sptr fileAccessExtConnection(new(std::nothrow) FileAccessExtConnection()); + if (fileAccessExtConnection == nullptr) { + HILOG_ERROR("new fileAccessExtConnection fail"); + return {nullptr, E_GETRESULT}; + } + if (!fileAccessExtConnection->IsExtAbilityConnected()) { + fileAccessExtConnection->ConnectFileExtAbility(want, context->GetToken()); + } + sptr fileExtProxy = fileAccessExtConnection->GetFileExtProxy(); + if (fileExtProxy == nullptr) { + HILOG_ERROR("get invalid fileExtProxy"); + return {nullptr, E_CONNECT}; + } + std::shared_ptr connectInfo = std::make_shared(); + if (!connectInfo) { + HILOG_ERROR("connectInfo == nullptr"); + return {nullptr, E_GETRESULT}; + } + connectInfo->want = want; + connectInfo->fileAccessExtConnection = fileAccessExtConnection; + return {connectInfo, ERR_OK}; +} + std::pair, int> FileAccessHelper::Creator( const std::shared_ptr &context) { UserAccessTracer trace; trace.Start("Creator"); + if (context == nullptr) { HILOG_ERROR("FileAccessHelper::Creator failed, context == nullptr"); return {nullptr, EINVAL}; } + if (!IsSystemApp()) { HILOG_ERROR("FileAccessHelper::Creator check IsSystemAppByFullTokenID failed"); return {nullptr, E_PERMISSION_SYS}; } + sptr bm = FileAccessHelper::GetBundleMgrProxy(); FileAccessHelper::wants_.clear(); std::unordered_map> cMap; @@ -198,64 +227,58 @@ std::pair, int> FileAccessHelper::Creator( return {nullptr, E_GETINFO}; } for (size_t i = 0; i < extensionInfos.size(); i++) { - AAFwk::Want wantTem; - wantTem.SetElementName(extensionInfos[i].bundleName, extensionInfos[i].name); - sptr fileAccessExtConnection(new(std::nothrow) FileAccessExtConnection()); - if (fileAccessExtConnection == nullptr) { - HILOG_ERROR("new fileAccessExtConnection fail"); - return {nullptr, E_GETRESULT}; - } - if (!fileAccessExtConnection->IsExtAbilityConnected()) { - fileAccessExtConnection->ConnectFileExtAbility(wantTem, context->GetToken()); - } - sptr fileExtProxy = fileAccessExtConnection->GetFileExtProxy(); - if (fileExtProxy == nullptr) { - HILOG_ERROR("Creator get invalid fileExtProxy"); - return {nullptr, E_CONNECT}; - } - std::shared_ptr connectInfo = std::make_shared(); - if (!connectInfo) { - HILOG_ERROR("Creator, connectInfo == nullptr"); - return {nullptr, E_GETRESULT}; + auto [connectInfo, errCode] = CreateFileAccessExtConnection(extensionInfos[i], context); + if (connectInfo == nullptr) { + HILOG_ERROR("CreateFileAccessExtConnection failed"); + return {nullptr, errCode}; } - FileAccessHelper::wants_.push_back(wantTem); - connectInfo->want = wantTem; - connectInfo->fileAccessExtConnection = fileAccessExtConnection; + + FileAccessHelper::wants_.push_back(connectInfo->want); cMap.emplace(extensionInfos[i].bundleName, connectInfo); } + FileAccessHelper *ptrFileAccessHelper = new (std::nothrow) FileAccessHelper(context, cMap); if (ptrFileAccessHelper == nullptr) { HILOG_ERROR("FileAccessHelper::Creator failed, create FileAccessHelper failed"); return {nullptr, E_GETRESULT}; } + return {std::shared_ptr(ptrFileAccessHelper), ERR_OK}; } -std::pair, int> FileAccessHelper::Creator( - const std::shared_ptr &context, const std::vector &wants) +int CheckCreatorConditions( + const std::shared_ptr& context, + const std::vector& wants) { - UserAccessTracer trace; - trace.Start("Creator"); if (context == nullptr) { - HILOG_ERROR("FileAccessHelper::Creator failed, context == nullptr"); - return {nullptr, EINVAL}; + HILOG_ERROR("FileAccessHelper::Creator failed, context is nullptr"); + return EINVAL; } - - if (wants.size() == 0) { + if (wants.empty()) { HILOG_ERROR("FileAccessHelper::Creator failed, wants is empty"); - return {nullptr, EINVAL}; + return EINVAL; } - if (!IsSystemApp()) { HILOG_ERROR("FileAccessHelper::Creator check IsSystemAppByFullTokenID failed"); - return {nullptr, E_PERMISSION_SYS}; + return E_PERMISSION_SYS; } + return ERR_OK; +} + +std::pair, int> FileAccessHelper::Creator( + const std::shared_ptr &context, const std::vector &wants) +{ + UserAccessTracer trace; + trace.Start("Creator"); + int checkResult = CheckCreatorConditions(context, wants); + if (checkResult != ERR_OK) { + return {nullptr, checkResult}; + } if (GetRegisteredFileAccessExtAbilityInfo(FileAccessHelper::wants_) != ERR_OK) { HILOG_ERROR("GetRegisteredFileAccessExtAbilityInfo failed"); return {nullptr, E_GETINFO}; } - std::unordered_map> cMap; for (size_t i = 0; i < wants.size(); i++) { sptr fileAccessExtConnection(new(std::nothrow) FileAccessExtConnection()); @@ -263,23 +286,19 @@ std::pair, int> FileAccessHelper::Creator( HILOG_ERROR("new fileAccessExtConnection fail"); return {nullptr, E_GETRESULT}; } - if (!fileAccessExtConnection->IsExtAbilityConnected()) { fileAccessExtConnection->ConnectFileExtAbility(wants[i], context->GetToken()); } - sptr fileExtProxy = fileAccessExtConnection->GetFileExtProxy(); if (fileExtProxy == nullptr) { HILOG_ERROR("Creator get invalid fileExtProxy"); return {nullptr, E_CONNECT}; } - std::shared_ptr connectInfo = std::make_shared(); if (connectInfo == nullptr) { HILOG_ERROR("Creator, connectInfo == nullptr"); return {nullptr, E_GETRESULT}; } - connectInfo->want = wants[i]; connectInfo->fileAccessExtConnection = fileAccessExtConnection; string bundleName = FileAccessHelper::GetKeyOfWants(wants[i]); @@ -294,35 +313,39 @@ std::pair, int> FileAccessHelper::Creator( HILOG_ERROR("Creator failed, create FileAccessHelper failed"); return {nullptr, E_GETRESULT}; } - return {std::shared_ptr(ptrFileAccessHelper), ERR_OK}; } -std::shared_ptr FileAccessHelper::Creator(const sptr &token, - const std::vector &wants) +int CheckCreatorConditionsForToken(const sptr& token, const std::vector& wants) { - UserAccessTracer trace; - trace.Start("Creator"); if (token == nullptr) { - HILOG_ERROR("FileAccessHelper::Creator failed, token == nullptr"); - return nullptr; + HILOG_ERROR("FileAccessHelper::Creator failed, token is nullptr"); + return EINVAL; } - - if (wants.size() == 0) { + if (wants.empty()) { HILOG_ERROR("FileAccessHelper::Creator failed, wants is empty"); - return nullptr; + return EINVAL; } - if (!IsSystemApp()) { HILOG_ERROR("FileAccessHelper::Creator check IsSystemAppByFullTokenID failed"); - return nullptr; + return E_PERMISSION_SYS; } + return ERR_OK; +} +std::shared_ptr FileAccessHelper::Creator(const sptr &token, + const std::vector &wants) +{ + UserAccessTracer trace; + trace.Start("Creator"); + int checkResult = CheckCreatorConditionsForToken(token, wants); + if (checkResult != ERR_OK) { + return nullptr; + } if (GetRegisteredFileAccessExtAbilityInfo(FileAccessHelper::wants_) != ERR_OK) { HILOG_ERROR("GetRegisteredFileAccessExtAbilityInfo failed"); return nullptr; } - std::unordered_map> cMap; for (size_t i = 0; i < wants.size(); i++) { sptr fileAccessExtConnection(new(std::nothrow) FileAccessExtConnection()); @@ -330,23 +353,19 @@ std::shared_ptr FileAccessHelper::Creator(const sptrIsExtAbilityConnected()) { fileAccessExtConnection->ConnectFileExtAbility(wants[i], token); } - sptr fileExtProxy = fileAccessExtConnection->GetFileExtProxy(); if (fileExtProxy == nullptr) { HILOG_ERROR("Creator get invalid fileExtProxy"); return nullptr; } - std::shared_ptr connectInfo = std::make_shared(); if (!connectInfo) { HILOG_ERROR("Creator, connectInfo == nullptr"); return nullptr; } - connectInfo->want = wants[i]; connectInfo->fileAccessExtConnection = fileAccessExtConnection; string bundleName = FileAccessHelper::GetKeyOfWants(wants[i]); @@ -361,7 +380,6 @@ std::shared_ptr FileAccessHelper::Creator(const sptr(ptrFileAccessHelper); } diff --git a/interfaces/inner_api/file_access/src/js_file_access_ext_ability.cpp b/interfaces/inner_api/file_access/src/js_file_access_ext_ability.cpp index 1250a6f3..22b3d20a 100644 --- a/interfaces/inner_api/file_access/src/js_file_access_ext_ability.cpp +++ b/interfaces/inner_api/file_access/src/js_file_access_ext_ability.cpp @@ -862,99 +862,73 @@ int JsFileAccessExtAbility::MakeStringNativeArray(napi_env &env, std::vector suffixVec = filter.GetSuffix(); int errorCode = MakeStringNativeArray(env, suffixVec, suffixArray); if (errorCode != ERR_OK) { - HILOG_ERROR("Create Suffix native array value fail, code:%{public}d.", errorCode); - return errorCode; + return LogErrorAndReturn("Create Suffix native array value fail", errorCode, true); } - napi_value displayNameArray = nullptr; napi_create_array_with_length(env, filter.GetDisplayName().size(), &displayNameArray); if (displayNameArray == nullptr) { - HILOG_ERROR("Create DisplayName native array value fail."); - return E_GETRESULT; + return LogErrorAndReturn("Create DisplayName native array value fail.", E_GETRESULT, false); } - std::vector displayNameVec = filter.GetDisplayName(); errorCode = MakeStringNativeArray(env, displayNameVec, displayNameArray); if (errorCode != ERR_OK) { - HILOG_ERROR("Create DisplayName native array value fail, code:%{public}d.", errorCode); - return errorCode; + return LogErrorAndReturn("Create DisplayName native array value fail", errorCode, true); } - napi_value mimeTypeArray = nullptr; napi_create_array_with_length(env, filter.GetMimeType().size(), &mimeTypeArray); if (mimeTypeArray == nullptr) { - HILOG_ERROR("Create MimeType native array value fail."); - return E_GETRESULT; + return LogErrorAndReturn("Create MimeType native array value fail.", E_GETRESULT, false); } - std::vector mimeTypeVec = filter.GetMimeType(); errorCode = MakeStringNativeArray(env, mimeTypeVec, mimeTypeArray); if (errorCode != ERR_OK) { - HILOG_ERROR("Create MimeType native array value fail, code:%{public}d.", errorCode); - return errorCode; + return LogErrorAndReturn("Create MimeType native array value fail", errorCode, true); } - napi_value nativeFileSizeOver = nullptr; napi_create_int64(env, filter.GetFileSizeOver(), &nativeFileSizeOver); if (nativeFileSizeOver == nullptr) { - HILOG_ERROR("Create NativeFileSizeOver native js value fail."); - return E_GETRESULT; + return LogErrorAndReturn("Create NativeFileSizeOver native js value fail.", E_GETRESULT, false); } - napi_value nativeLastModifiedAfter = nullptr; napi_create_double(env, filter.GetLastModifiedAfter(), &nativeLastModifiedAfter); if (nativeLastModifiedAfter == nullptr) { - HILOG_ERROR("Create NativeLastModifiedAfter native js value fail."); - return E_GETRESULT; + return LogErrorAndReturn("Create NativeLastModifiedAfter native js value fail.", E_GETRESULT, false); } - napi_value nativeExcludeMedia = nullptr; napi_get_boolean(env, filter.GetExcludeMedia(), &nativeExcludeMedia); - if (nativeExcludeMedia == nullptr) { - HILOG_ERROR("Create NativeExcludeMedia native js value fail."); - return E_GETRESULT; - } - - if (napi_set_named_property(env, nativeFilter, "suffix", suffixArray) != napi_ok) { - HILOG_ERROR("Set suffix property to Filter NativeValue fail."); - return EINVAL; - } - - if (napi_set_named_property(env, nativeFilter, "displayName", displayNameArray) != napi_ok) { - HILOG_ERROR("Set displayName property to Filter NativeValue fail."); - return EINVAL; - } - - if (napi_set_named_property(env, nativeFilter, "mimeType", mimeTypeArray) != napi_ok) { - HILOG_ERROR("Set mimeType property to Filter NativeValue fail."); - return EINVAL; - } - - if (napi_set_named_property(env, nativeFilter, "fileSizeOver", nativeFileSizeOver) != napi_ok) { - HILOG_ERROR("Set fileSizeOver property to Filter NativeValue fail."); - return EINVAL; - } - - if (napi_set_named_property(env, nativeFilter, "lastModifiedAfter", nativeLastModifiedAfter) != napi_ok) { - HILOG_ERROR("Set lastModifiedAfter property to Filter NativeValue fail."); - return EINVAL; - } - - if (napi_set_named_property(env, nativeFilter, "excludeMedia", nativeExcludeMedia) != napi_ok) { - HILOG_ERROR("Set excludeMedia property to Filter NativeValue fail."); - return EINVAL; + if (nativeExcludeMedia == nullptr) { + return LogErrorAndReturn("Create NativeExcludeMedia native js value fail.", E_GETRESULT, false); + } else if (napi_set_named_property(env, nativeFilter, "suffix", suffixArray) != napi_ok) { + return LogErrorAndReturn("Set suffix property to Filter NativeValue fail.", EINVAL, false); + } else if (napi_set_named_property(env, nativeFilter, "displayName", displayNameArray) != napi_ok) { + return LogErrorAndReturn("Set displayName property to Filter NativeValue fail.", EINVAL, false); + } else if (napi_set_named_property(env, nativeFilter, "mimeType", mimeTypeArray) != napi_ok) { + return LogErrorAndReturn("Set mimeType property to Filter NativeValue fail.", EINVAL, false); + } else if (napi_set_named_property(env, nativeFilter, "fileSizeOver", nativeFileSizeOver) != napi_ok) { + return LogErrorAndReturn("Set fileSizeOver property to Filter NativeValue fail.", EINVAL, false); + } else if (napi_set_named_property(env, nativeFilter, "lastModifiedAfter", nativeLastModifiedAfter) != napi_ok) { + return LogErrorAndReturn("Set lastModifiedAfter property to Filter NativeValue fail.", EINVAL, false); + } else if (napi_set_named_property(env, nativeFilter, "excludeMedia", nativeExcludeMedia) != napi_ok) { + return LogErrorAndReturn("Set excludeMedia property to Filter NativeValue fail.", EINVAL, false); } return ERR_OK; } @@ -1471,62 +1445,58 @@ int JsFileAccessExtAbility::Query(const Uri &uri, std::vector &colu return ERR_OK; } +return LogErrorAndReturn("Create Suffix native array value fail", errorCode, true); + +bool LogErrorAndReturnBool(const std::string &errorMessage, bool condition) { + if (!condition) { + HILOG_ERROR("%s", errorMessage.c_str()); + } + return condition; +} + + int JsFileAccessExtAbility::GetFileInfoFromUri(const Uri &selectFile, FileInfo &fileInfo) { UserAccessTracer trace; trace.Start("GetFileInfoFromUri"); auto value = std::make_shared>(); if (value == nullptr) { - HILOG_ERROR("GetFileInfoFromUri value is nullptr."); - return E_GETRESULT; + return LogErrorAndReturn("GetFileInfoFromUri value is nullptr.", E_GETRESULT); } - auto argParser = [selectFile](napi_env &env, napi_value *argv, size_t &argc) -> bool { napi_value nativeUri = nullptr; napi_create_string_utf8(env, selectFile.ToString().c_str(), selectFile.ToString().length(), &nativeUri); if (nativeUri == nullptr) { - HILOG_ERROR("create selectFile uri native js value fail."); - return false; + return LogErrorAndReturnBool("create selectFile uri native js value fail.", false); } argv[ARGC_ZERO] = nativeUri; argc = ARGC_ONE; return true; }; - auto retParser = [value](napi_env &env, napi_value result) -> bool { napi_value code = nullptr; napi_get_named_property(env, result, "code", &code); if (napi_get_value_int32(env, code, &value->code) != napi_ok) { - HILOG_ERROR("Convert js value fail."); - return false; + return LogErrorAndReturnBool("Convert js value fail.", false); } - FileInfo fileInfo; napi_value nativeFileInfo = nullptr; if (napi_get_named_property(env, result, "fileInfo", &nativeFileInfo) != napi_ok) { - HILOG_INFO("Convert fileInfo js value failed"); - return false; + return LogErrorAndReturnBool("Convert fileInfo js value failed", false); } - if (GetFileInfoFromJs(env, nativeFileInfo, fileInfo) != napi_ok) { - HILOG_ERROR("Convert fileInfo js value fail."); - return false; + return LogErrorAndReturnBool("Convert fileInfo js value fail.", false); } value->data = std::move(fileInfo); return true; }; - auto errCode = CallJsMethod("getFileInfoFromUri", jsRuntime_, jsObj_.get(), argParser, retParser); if (errCode != ERR_OK) { - HILOG_ERROR("CallJsMethod error, code:%{public}d.", errCode); - return errCode; + return LogErrorAndReturn("CallJsMethod error, code:%{public}d.", errCode, true); } - if (value->code != ERR_OK) { - HILOG_ERROR("fileio fail."); - return value->code; + return LogErrorAndReturn("fileio fail.", value->code); } - fileInfo = std::move(value->data); return ERR_OK; } @@ -1537,16 +1507,14 @@ int JsFileAccessExtAbility::GetFileInfoFromRelativePath(const std::string &selec trace.Start("GetFileInfoFromRelativePath"); auto value = std::make_shared>(); if (value == nullptr) { - HILOG_ERROR("GetFileInfoFromRelativePath value is nullptr."); - return E_GETRESULT; + return LogErrorAndReturn("GetFileInfoFromRelativePath value is nullptr.", E_GETRESULT); } auto argParser = [selectFileRealtivePath](napi_env &env, napi_value *argv, size_t &argc) -> bool { napi_value nativePath = nullptr; napi_create_string_utf8(env, selectFileRealtivePath.c_str(), selectFileRealtivePath.length(), &nativePath); if (nativePath == nullptr) { - HILOG_ERROR("create selectFileRealtivePath native js value fail."); - return false; + return LogErrorAndReturnBool("create selectFileRealtivePath native js value fail", false); } argv[ARGC_ZERO] = nativePath; argc = ARGC_ONE; @@ -1557,20 +1525,17 @@ int JsFileAccessExtAbility::GetFileInfoFromRelativePath(const std::string &selec napi_value code = nullptr; napi_get_named_property(env, result, "code", &code); if (napi_get_value_int32(env, code, &value->code) != napi_ok) { - HILOG_ERROR("Convert js value fail."); - return false; + return LogErrorAndReturnBool("Convert js value fail.", false); } FileInfo fileInfo; napi_value nativeFileInfo = nullptr; if (napi_get_named_property(env, result, "fileInfo", &nativeFileInfo) != napi_ok) { - HILOG_INFO("Convert fileInfo js value failed"); - return false; + return LogErrorAndReturnBool("Convert fileInfo js value failed", false); } if (GetFileInfoFromJs(env, nativeFileInfo, fileInfo) != napi_ok) { - HILOG_ERROR("Convert fileInfo js value fail."); - return false; + return LogErrorAndReturnBool("Convert fileInfo js value fail.", false); } value->data = std::move(fileInfo); return true; @@ -1578,13 +1543,11 @@ int JsFileAccessExtAbility::GetFileInfoFromRelativePath(const std::string &selec auto errCode = CallJsMethod("getFileInfoFromRelativePath", jsRuntime_, jsObj_.get(), argParser, retParser); if (errCode != ERR_OK) { - HILOG_ERROR("CallJsMethod error, code:%{public}d.", errCode); - return errCode; + return LogErrorAndReturn("CallJsMethod error, code:%{public}d.", errCode, true); } if (value->code != ERR_OK) { - HILOG_ERROR("fileio fail."); - return value->code; + return LogErrorAndReturn("fileio fail.", value->code); } fileInfo = std::move(value->data); -- Gitee