From f51e48cb01e6d4bb6f7be9766f5ab0b2bd4e5316 Mon Sep 17 00:00:00 2001 From: zhuhongtao66 Date: Tue, 10 Jan 2023 11:45:42 +0800 Subject: [PATCH] bugfix_v9_errcode Signed-off-by: zhuhongtao66 --- interfaces/kits/js/src/common/uni_error.cpp | 24 +++++++++------ interfaces/kits/js/src/common/uni_error.h | 4 ++- .../mod_fileio/class_file/file_n_exporter.cpp | 14 ++++----- .../class_stat_v9/stat_n_exporter_v9.cpp | 30 +++++++++---------- .../js/src/mod_fileio/properties/open_v9.cpp | 24 +++++++-------- .../properties/prop_n_exporter_v9.cpp | 30 +++++++++---------- .../js/src/mod_fileio/properties/stat_v9.cpp | 26 ++++++++-------- .../src/mod_fileio/properties/truncate_v9.cpp | 22 +++++++------- 8 files changed, 91 insertions(+), 83 deletions(-) diff --git a/interfaces/kits/js/src/common/uni_error.cpp b/interfaces/kits/js/src/common/uni_error.cpp index 05395ca43..93d5e46ac 100644 --- a/interfaces/kits/js/src/common/uni_error.cpp +++ b/interfaces/kits/js/src/common/uni_error.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2021-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -45,6 +45,9 @@ UniError::UniError(ELegacy eLegacy) : errno_(eLegacy), codingSystem_(ERR_CODE_SY UniError::UniError(int ePosix) : errno_(ePosix), codingSystem_(ERR_CODE_SYSTEM_POSIX) {} +UniError::UniError(int ePosix, bool throwCode) : errno_(ePosix), codingSystem_(ERR_CODE_SYSTEM_POSIX), + throwCode_(throwCode) {} + UniError::operator bool() const { return errno_ != ERRNO_NOERR; @@ -95,6 +98,9 @@ napi_value UniError::GetNapiErr(napi_env env) if (errCode == ERRNO_NOERR) { return nullptr; } + if (!throwCode_) { + return GetNapiErr(env, GetDefaultErrstr()); + } int32_t code; string msg; if (errCodeTable.find(errCode) != errCodeTable.end()) { @@ -109,15 +115,9 @@ napi_value UniError::GetNapiErr(napi_env env) napi_value UniError::GetNapiErr(napi_env env, string errMsg) { - int errCode = GetErrno(codingSystem_); - if (errCode == ERRNO_NOERR) { - return nullptr; - } - napi_value code = NVal::CreateUTF8String(env, to_string(errCode)).val_; napi_value msg = NVal::CreateUTF8String(env, errMsg).val_; - napi_value res = nullptr; - napi_status createRes = napi_create_error(env, code, msg, &res); + napi_status createRes = napi_create_error(env, nullptr, msg, &res); if (createRes) { HILOGE("Failed to create an exception, msg = %{public}s", errMsg.c_str()); } @@ -130,6 +130,7 @@ void UniError::ThrowErr(napi_env env) napi_get_and_clear_last_exception(env, &tmp); int32_t code; string msg; + napi_status throwStatus = napi_ok; if (errCodeTable.find(errno_) != errCodeTable.end()) { code = errCodeTable.at(errno_).first; msg = errCodeTable.at(errno_).second; @@ -137,7 +138,12 @@ void UniError::ThrowErr(napi_env env) code = errCodeTable.at(-1).first; msg = errCodeTable.at(-1).second; } - napi_status throwStatus = napi_throw(env, GenerateBusinessError(env, code, msg)); + if (!throwCode_) { + throwStatus = napi_throw_error(env, nullptr, msg.c_str()); + } else { + throwStatus = napi_throw(env, GenerateBusinessError(env, code, msg)); + } + if (throwStatus != napi_ok) { HILOGE("Failed to throw an exception, %{public}d, code = %{public}s", throwStatus, msg.c_str()); } diff --git a/interfaces/kits/js/src/common/uni_error.h b/interfaces/kits/js/src/common/uni_error.h index b9d2b7a30..c6ea4dbcd 100644 --- a/interfaces/kits/js/src/common/uni_error.h +++ b/interfaces/kits/js/src/common/uni_error.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2021-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -134,6 +134,7 @@ public: UniError(); explicit UniError(ELegacy eLegacy); explicit UniError(int ePosix); + explicit UniError(int ePosix, bool throwCode); UniError(const UniError &) = default; ~UniError() = default; @@ -155,6 +156,7 @@ public: private: int errno_ = ERRNO_NOERR; ErrCodeSystem codingSystem_ = ERR_CODE_SYSTEM_POSIX; + bool throwCode_ = false; }; } // namespace DistributedFS } // namespace OHOS diff --git a/interfaces/kits/js/src/mod_fileio/class_file/file_n_exporter.cpp b/interfaces/kits/js/src/mod_fileio/class_file/file_n_exporter.cpp index 8adda7d37..4eeb63358 100644 --- a/interfaces/kits/js/src/mod_fileio/class_file/file_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fileio/class_file/file_n_exporter.cpp @@ -38,11 +38,11 @@ static FileEntity *GetFileEntity(napi_env env, napi_value raf_entity) { auto rafEntity = NClass::GetEntityOf(env, raf_entity); if (!rafEntity) { - UniError(EINVAL).ThrowErr(env); + UniError(EINVAL, true).ThrowErr(env); return nullptr; } if (!rafEntity->fd_) { - UniError(EINVAL).ThrowErr(env); + UniError(EINVAL, true).ThrowErr(env); return nullptr; } return rafEntity; @@ -52,7 +52,7 @@ napi_value FileNExporter::GetFD(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ZERO)) { - UniError(EINVAL).ThrowErr(env); + UniError(EINVAL, true).ThrowErr(env); return nullptr; } auto rafEntity = GetFileEntity(env, funcArg.GetThisVar()); @@ -66,13 +66,13 @@ napi_value FileNExporter::Constructor(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ZERO)) { - UniError(EINVAL).ThrowErr(env); + UniError(EINVAL, true).ThrowErr(env); return nullptr; } auto rafEntity = make_unique(); if (!NClass::SetEntityFor(env, funcArg.GetThisVar(), move(rafEntity))) { - UniError(EIO).ThrowErr(env); + UniError(EIO, true).ThrowErr(env); return nullptr; } return funcArg.GetThisVar(); @@ -90,12 +90,12 @@ bool FileNExporter::Export() tie(succ, classValue) = NClass::DefineClass(exports_.env_, className, FileNExporter::Constructor, move(props)); if (!succ) { - UniError(EIO).ThrowErr(exports_.env_); + UniError(EIO, true).ThrowErr(exports_.env_); return false; } succ = NClass::SaveClass(exports_.env_, className, classValue); if (!succ) { - UniError(EIO).ThrowErr(exports_.env_); + UniError(EIO, true).ThrowErr(exports_.env_); return false; } diff --git a/interfaces/kits/js/src/mod_fileio/class_stat_v9/stat_n_exporter_v9.cpp b/interfaces/kits/js/src/mod_fileio/class_stat_v9/stat_n_exporter_v9.cpp index ee9f5e40e..fb2768f69 100644 --- a/interfaces/kits/js/src/mod_fileio/class_stat_v9/stat_n_exporter_v9.cpp +++ b/interfaces/kits/js/src/mod_fileio/class_stat_v9/stat_n_exporter_v9.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -42,7 +42,7 @@ static napi_value CheckStatMode(napi_env env, napi_callback_info info, mode_t mo { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ZERO)) { - UniError(EINVAL).ThrowErr(env); + UniError(EINVAL, true).ThrowErr(env); return nullptr; } @@ -94,7 +94,7 @@ napi_value StatNExporterV9::GetIno(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ZERO)) { - UniError(EINVAL).ThrowErr(env); + UniError(EINVAL, true).ThrowErr(env); return nullptr; } @@ -110,7 +110,7 @@ napi_value StatNExporterV9::GetMode(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ZERO)) { - UniError(EINVAL).ThrowErr(env); + UniError(EINVAL, true).ThrowErr(env); return nullptr; } @@ -126,7 +126,7 @@ napi_value StatNExporterV9::GetUid(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ZERO)) { - UniError(EINVAL).ThrowErr(env); + UniError(EINVAL, true).ThrowErr(env); return nullptr; } @@ -142,7 +142,7 @@ napi_value StatNExporterV9::GetGid(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ZERO)) { - UniError(EINVAL).ThrowErr(env); + UniError(EINVAL, true).ThrowErr(env); return nullptr; } @@ -158,7 +158,7 @@ napi_value StatNExporterV9::GetSize(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ZERO)) { - UniError(EINVAL).ThrowErr(env); + UniError(EINVAL, true).ThrowErr(env); return nullptr; } @@ -174,7 +174,7 @@ napi_value StatNExporterV9::GetBlksize(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ZERO)) { - UniError(EINVAL).ThrowErr(env); + UniError(EINVAL, true).ThrowErr(env); return nullptr; } @@ -190,7 +190,7 @@ napi_value StatNExporterV9::GetAtime(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ZERO)) { - UniError(EINVAL).ThrowErr(env); + UniError(EINVAL, true).ThrowErr(env); return nullptr; } @@ -206,7 +206,7 @@ napi_value StatNExporterV9::GetMtime(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ZERO)) { - UniError(EINVAL).ThrowErr(env); + UniError(EINVAL, true).ThrowErr(env); return nullptr; } @@ -222,7 +222,7 @@ napi_value StatNExporterV9::GetCtime(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ZERO)) { - UniError(EINVAL).ThrowErr(env); + UniError(EINVAL, true).ThrowErr(env); return nullptr; } @@ -238,13 +238,13 @@ napi_value StatNExporterV9::Constructor(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ZERO)) { - UniError(EINVAL).ThrowErr(env); + UniError(EINVAL, true).ThrowErr(env); return nullptr; } unique_ptr statEntity = make_unique(); if (!NClass::SetEntityFor(env, funcArg.GetThisVar(), move(statEntity))) { - UniError(EIO).ThrowErr(env); + UniError(EIO, true).ThrowErr(env); return nullptr; } return funcArg.GetThisVar(); @@ -278,12 +278,12 @@ bool StatNExporterV9::Export() tie(succ, classValue) = NClass::DefineClass(exports_.env_, className, StatNExporterV9::Constructor, std::move(props)); if (!succ) { - UniError(EIO).ThrowErr(exports_.env_); + UniError(EIO, true).ThrowErr(exports_.env_); return false; } succ = NClass::SaveClass(exports_.env_, className, classValue); if (!succ) { - UniError(EIO).ThrowErr(exports_.env_); + UniError(EIO, true).ThrowErr(exports_.env_); return false; } diff --git a/interfaces/kits/js/src/mod_fileio/properties/open_v9.cpp b/interfaces/kits/js/src/mod_fileio/properties/open_v9.cpp index 34955cac2..0ccb51d75 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/open_v9.cpp +++ b/interfaces/kits/js/src/mod_fileio/properties/open_v9.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -46,7 +46,7 @@ static tuple GetJsFlags(napi_env env, const NFuncArg &funcArg) if (funcArg.GetArgc() >= NARG_CNT::TWO && NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_number)) { tie(succ, mode) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(); if (!succ) { - UniError(EINVAL).ThrowErr(env); + UniError(EINVAL, true).ThrowErr(env); return { false, mode }; } (void)CommonFunc::ConvertJsFlags(mode); @@ -58,13 +58,13 @@ static NVal InstantiateFile(napi_env env, int fd, string path) { napi_value objRAF = NClass::InstantiateClass(env, FileNExporter::className_, {}); if (!objRAF) { - UniError(EIO).ThrowErr(env); + UniError(EIO, true).ThrowErr(env); return NVal(); } auto rafEntity = NClass::GetEntityOf(env, objRAF); if (!rafEntity) { - UniError(EIO).ThrowErr(env); + UniError(EIO, true).ThrowErr(env); return NVal(); } auto fdg = make_unique(fd, false); @@ -93,12 +93,12 @@ napi_value OpenV9::Sync(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { - UniError(EINVAL).ThrowErr(env); + UniError(EINVAL, true).ThrowErr(env); return nullptr; } auto [succPath, path, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); if (!succPath) { - UniError(EINVAL).ThrowErr(env); + UniError(EINVAL, true).ThrowErr(env); return nullptr; } auto [succMode, mode] = GetJsFlags(env, funcArg); @@ -111,7 +111,7 @@ napi_value OpenV9::Sync(napi_env env, napi_callback_info info) auto File = InstantiateFile(env, fd, path.get()).val_; return File; } - UniError(-1).ThrowErr(env); + UniError(-1, true).ThrowErr(env); return nullptr; } uv_loop_s *loop = nullptr; @@ -120,7 +120,7 @@ napi_value OpenV9::Sync(napi_env env, napi_callback_info info) int ret = uv_fs_open(loop, &open_req, path.get(), mode, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, NULL); if (ret < 0) { - UniError(errno).ThrowErr(env); + UniError(errno, true).ThrowErr(env); return nullptr; } auto File = InstantiateFile(env, open_req.result, path.get()).val_; @@ -138,12 +138,12 @@ napi_value OpenV9::Async(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::THREE)) { - UniError(EINVAL).ThrowErr(env); + UniError(EINVAL, true).ThrowErr(env); return nullptr; } auto [succPath, path, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); if (!succPath) { - UniError(EINVAL).ThrowErr(env); + UniError(EINVAL, true).ThrowErr(env); return nullptr; } auto [succMode, mode] = GetJsFlags(env, funcArg); @@ -161,7 +161,7 @@ napi_value OpenV9::Async(napi_env env, napi_callback_info info) arg->uri = ""; return UniError(ERRNO_NOERR); } - return UniError(-1); + return UniError(-1, true); } uv_loop_s *loop = nullptr; napi_get_uv_event_loop(env, &loop); @@ -169,7 +169,7 @@ napi_value OpenV9::Async(napi_env env, napi_callback_info info) int ret = uv_fs_open(loop, &open_req, path.c_str(), mode, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, NULL); if (ret < 0) { - return UniError(errno); + return UniError(errno, true); } arg->fd = open_req.result; arg->path = path; diff --git a/interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter_v9.cpp b/interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter_v9.cpp index 0c94dcc94..cfa371471 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter_v9.cpp +++ b/interfaces/kits/js/src/mod_fileio/properties/prop_n_exporter_v9.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -37,7 +37,7 @@ napi_value PropNExporterV9::ReadSync(napi_env env, napi_callback_info info) NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) { - UniError(EINVAL).ThrowErr(env); + UniError(EINVAL, true).ThrowErr(env); return nullptr; } @@ -45,7 +45,7 @@ napi_value PropNExporterV9::ReadSync(napi_env env, napi_callback_info info) int fd = 0; tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); if (!succ) { - UniError(EINVAL).ThrowErr(env); + UniError(EINVAL, true).ThrowErr(env); return nullptr; } @@ -66,7 +66,7 @@ napi_value PropNExporterV9::ReadSync(napi_env env, napi_callback_info info) actLen = read(fd, buf, len); } if (actLen == -1) { - UniError(errno).ThrowErr(env); + UniError(errno, true).ThrowErr(env); return nullptr; } @@ -86,7 +86,7 @@ static UniError ReadExec(shared_ptr arg, void *buf, size_t len, } if (arg->lenRead == -1) { - return UniError(errno); + return UniError(errno, true); } else { return UniError(ERRNO_NOERR); } @@ -96,7 +96,7 @@ napi_value PropNExporterV9::Read(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::FOUR)) { - UniError(EINVAL).ThrowErr(env); + UniError(EINVAL, true).ThrowErr(env); return nullptr; } @@ -108,14 +108,14 @@ napi_value PropNExporterV9::Read(napi_env env, napi_callback_info info) int64_t pos = 0; tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); if (!succ) { - UniError(EINVAL).ThrowErr(env); + UniError(EINVAL, true).ThrowErr(env); return nullptr; } tie(succ, buf, len, hasPos, pos) = CommonFunc::GetReadArgV9(env, funcArg[NARG_POS::SECOND], funcArg[NARG_POS::THIRD]); if (!succ) { - UniError(EINVAL).ThrowErr(env); + UniError(EINVAL, true).ThrowErr(env); return nullptr; } @@ -159,7 +159,7 @@ UniError PropNExporterV9::WriteExec(shared_ptr arg, void *buf, } if (arg->actLen == -1) { - return UniError(errno); + return UniError(errno, true); } else { return UniError(ERRNO_NOERR); } @@ -169,7 +169,7 @@ napi_value PropNExporterV9::Write(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::FOUR)) { - UniError(EINVAL).ThrowErr(env); + UniError(EINVAL, true).ThrowErr(env); return nullptr; } @@ -177,7 +177,7 @@ napi_value PropNExporterV9::Write(napi_env env, napi_callback_info info) int fd; tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); if (!succ) { - UniError(EINVAL).ThrowErr(env); + UniError(EINVAL, true).ThrowErr(env); return nullptr; } @@ -189,7 +189,7 @@ napi_value PropNExporterV9::Write(napi_env env, napi_callback_info info) tie(succ, bufGuard, buf, len, hasPos, position) = CommonFunc::GetWriteArgV9(env, funcArg[NARG_POS::SECOND], funcArg[NARG_POS::THIRD]); if (!succ) { - UniError(EINVAL).ThrowErr(env); + UniError(EINVAL, true).ThrowErr(env); return nullptr; } @@ -236,7 +236,7 @@ napi_value PropNExporterV9::WriteSync(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) { - UniError(EINVAL).ThrowErr(env); + UniError(EINVAL, true).ThrowErr(env); return nullptr; } @@ -244,7 +244,7 @@ napi_value PropNExporterV9::WriteSync(napi_env env, napi_callback_info info) int fd; tie(succ, fd) = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); if (!succ) { - UniError(EINVAL).ThrowErr(env); + UniError(EINVAL, true).ThrowErr(env); return nullptr; } @@ -267,7 +267,7 @@ napi_value PropNExporterV9::WriteSync(napi_env env, napi_callback_info info) } if (writeLen == -1) { - UniError(errno).ThrowErr(env); + UniError(errno, true).ThrowErr(env); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fileio/properties/stat_v9.cpp b/interfaces/kits/js/src/mod_fileio/properties/stat_v9.cpp index 86322baa7..e513d5530 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/stat_v9.cpp +++ b/interfaces/kits/js/src/mod_fileio/properties/stat_v9.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -48,12 +48,12 @@ static tuple ParseJsFile(napi_env env, napi_value pathOrFdFromJs auto [isFd, fd] = NVal(env, pathOrFdFromJsArg).ToInt32(); if (isFd) { if (fd < 0) { - UniError(EINVAL).ThrowErr(env); + UniError(EINVAL, true).ThrowErr(env); return { false, FileInfo { false, {}, {} } }; } return { true, FileInfo { false, {}, { fd, false } } }; } - UniError(EINVAL).ThrowErr(env); + UniError(EINVAL, true).ThrowErr(env); return { false, FileInfo { false, {}, {} } }; }; @@ -61,7 +61,7 @@ napi_value StatV9::Sync(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ONE)) { - UniError(EINVAL).ThrowErr(env); + UniError(EINVAL, true).ThrowErr(env); return nullptr; } auto [succ, fileInfo] = ParseJsFile(env, funcArg[NARG_POS::FIRST]); @@ -72,24 +72,24 @@ napi_value StatV9::Sync(napi_env env, napi_callback_info info) struct stat buf; if (fileInfo.isPath) { if (stat(fileInfo.path.get(), &buf) != 0) { - UniError(errno).ThrowErr(env); + UniError(errno, true).ThrowErr(env); return nullptr; } } else { if (fstat(fileInfo.fdg.GetFD(), &buf) != 0) { - UniError(errno).ThrowErr(env); + UniError(errno, true).ThrowErr(env); return nullptr; } } napi_value objStat = NClass::InstantiateClass(env, StatNExporterV9::className_, {}); if (!objStat) { - UniError(EIO).ThrowErr(env); + UniError(EIO, true).ThrowErr(env); return nullptr; } auto statEntity = NClass::GetEntityOf(env, objStat); if (!statEntity) { - UniError(EIO).ThrowErr(env); + UniError(EIO, true).ThrowErr(env); return nullptr; } statEntity->stat_ = buf; @@ -104,7 +104,7 @@ napi_value StatV9::Async(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { - UniError(EINVAL).ThrowErr(env); + UniError(EINVAL, true).ThrowErr(env); return nullptr; } auto [succ, fileInfo] = ParseJsFile(env, funcArg[NARG_POS::FIRST]); @@ -116,11 +116,11 @@ napi_value StatV9::Async(napi_env env, napi_callback_info info) auto cbExec = [arg, fileInfo = make_shared(move(fileInfo))](napi_env env) -> UniError { if (fileInfo->isPath) { if (stat(fileInfo->path.get(), &arg->stat_) != 0) { - return UniError(errno); + return UniError(errno, true); } } else { if (fstat(fileInfo->fdg.GetFD(), &arg->stat_) != 0) { - return UniError(errno); + return UniError(errno, true); } } return UniError(ERRNO_NOERR); @@ -131,11 +131,11 @@ napi_value StatV9::Async(napi_env env, napi_callback_info info) } napi_value objStat = NClass::InstantiateClass(env, StatNExporterV9::className_, {}); if (!objStat) { - return { env, UniError(EIO).GetNapiErr(env) }; + return { env, UniError(EIO, true).GetNapiErr(env) }; } auto statEntity = NClass::GetEntityOf(env, objStat); if (!statEntity) { - return { env, UniError(EIO).GetNapiErr(env) }; + return { env, UniError(EIO, true).GetNapiErr(env) }; } statEntity->stat_ = arg->stat_; return { env, objStat }; diff --git a/interfaces/kits/js/src/mod_fileio/properties/truncate_v9.cpp b/interfaces/kits/js/src/mod_fileio/properties/truncate_v9.cpp index fd7777b63..c79d72d30 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/truncate_v9.cpp +++ b/interfaces/kits/js/src/mod_fileio/properties/truncate_v9.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -41,12 +41,12 @@ static tuple ParseJsFile(napi_env env, napi_value pathOrFdFromJs auto [isFd, fd] = NVal(env, pathOrFdFromJsArg).ToInt32(); if (isFd) { if (fd < 0) { - UniError(EINVAL).ThrowErr(env); + UniError(EINVAL, true).ThrowErr(env); return { false, FileInfo { false, {}, {} } }; } return { true, FileInfo { false, {}, { fd, false } } }; } - UniError(EINVAL).ThrowErr(env); + UniError(EINVAL, true).ThrowErr(env); return { false, FileInfo { false, {}, {} } }; }; @@ -55,7 +55,7 @@ napi_value TruncateV9::Sync(napi_env env, napi_callback_info info) NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { - UniError(EINVAL).ThrowErr(env); + UniError(EINVAL, true).ThrowErr(env); return nullptr; } auto [succ, fileInfo] = ParseJsFile(env, funcArg[NARG_POS::FIRST]); @@ -66,18 +66,18 @@ napi_value TruncateV9::Sync(napi_env env, napi_callback_info info) if (funcArg.GetArgc() == NARG_CNT::TWO) { tie(succ, truncateLen) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(); if (!succ) { - UniError(EINVAL).ThrowErr(env); + UniError(EINVAL, true).ThrowErr(env); return nullptr; } } if (fileInfo.isPath) { if (truncate(fileInfo.path.get(), truncateLen) != 0) { - UniError(errno).ThrowErr(env); + UniError(errno, true).ThrowErr(env); return nullptr; } } else { if (ftruncate(fileInfo.fdg.GetFD(), truncateLen) != 0) { - UniError(errno).ThrowErr(env); + UniError(errno, true).ThrowErr(env); return nullptr; } } @@ -88,7 +88,7 @@ napi_value TruncateV9::Async(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::THREE)) { - UniError(EINVAL).ThrowErr(env); + UniError(EINVAL, true).ThrowErr(env); return nullptr; } auto [succ, fileInfo] = ParseJsFile(env, funcArg[NARG_POS::FIRST]); @@ -99,18 +99,18 @@ napi_value TruncateV9::Async(napi_env env, napi_callback_info info) if (funcArg.GetArgc() > NARG_CNT::ONE && NVal(env, funcArg[NARG_POS::SECOND]).TypeIs(napi_number)) { tie(succ, truncateLen) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(); if (!succ) { - UniError(EINVAL).ThrowErr(env); + UniError(EINVAL, true).ThrowErr(env); return nullptr; } } auto cbExec = [fileInfo = make_shared(move(fileInfo)), truncateLen](napi_env env) -> UniError { if (fileInfo->isPath) { if (truncate(fileInfo->path.get(), truncateLen) != 0) { - return UniError(errno); + return UniError(errno, true); } } else { if (ftruncate(fileInfo->fdg.GetFD(), truncateLen) != 0) { - return UniError(errno); + return UniError(errno, true); } } return UniError(ERRNO_NOERR); -- Gitee