From e5166b070dc0affb54e9b1b79aebf4aba0eac2ac Mon Sep 17 00:00:00 2001 From: Anonymous Date: Mon, 25 Dec 2023 11:48:01 +0800 Subject: [PATCH] 123 Signed-off-by: Anonymous --- .../src/js_file_access_ext_ability.cpp | 144 +++++++----------- 1 file changed, 52 insertions(+), 92 deletions(-) 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..594ef661 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,55 @@ int JsFileAccessExtAbility::Query(const Uri &uri, std::vector &colu return ERR_OK; } +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 +1504,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 +1522,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 +1540,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