From 3805c5b9b918f939ace4004625203c68a065dae4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E9=91=AB?= Date: Wed, 19 Mar 2025 16:40:28 +0800 Subject: [PATCH] hash MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 周鑫 --- interfaces/kits/js/BUILD.gn | 1 + .../js/src/mod_hash/ani/error_handler.cpp | 28 +++++++++++++++ .../src/mod_hash/ani/ets/@ohos.file.hash.ets | 34 +++++++++---------- .../kits/js/src/mod_hash/ani/hash_ani.cpp | 6 ++++ 4 files changed, 52 insertions(+), 17 deletions(-) create mode 100644 interfaces/kits/js/src/mod_hash/ani/error_handler.cpp diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index f992afa2..3231764c 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -785,6 +785,7 @@ ohos_shared_library("ani_hash_class") { "src/common/file_helper/hash_file.cpp", "src/mod_fs/fs_utils.cpp", "src/mod_hash/ani/bind_function_class.cpp", + "src/mod_hash/ani/error_handler.cpp", "src/mod_hash/ani/hash_ani.cpp", "src/mod_hash/hash_core.cpp", ] diff --git a/interfaces/kits/js/src/mod_hash/ani/error_handler.cpp b/interfaces/kits/js/src/mod_hash/ani/error_handler.cpp new file mode 100644 index 00000000..0d14637f --- /dev/null +++ b/interfaces/kits/js/src/mod_hash/ani/error_handler.cpp @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2025 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "error_handler.h" + +namespace OHOS::FileManagement::ModuleFileIO::ANI { + +ani_status ErrorHandler::Throw(ani_env *env, int32_t code, const std::string &errMsg) +{ + const char *className = "L@ohos/file/hash/BusinessError;"; + const char *name = "BusinessError"; + return Throw(env, className, name, code, errMsg); +} + + +} // namespace OHOS::FileManagement::ModuleFileIO::ANI \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_hash/ani/ets/@ohos.file.hash.ets b/interfaces/kits/js/src/mod_hash/ani/ets/@ohos.file.hash.ets index 286d1815..f93f258d 100644 --- a/interfaces/kits/js/src/mod_hash/ani/ets/@ohos.file.hash.ets +++ b/interfaces/kits/js/src/mod_hash/ani/ets/@ohos.file.hash.ets @@ -18,6 +18,13 @@ export class BusinessError { message: string; code: number; data?: T; + + constructor() { + this.name = 'BusinessError'; + this.message = ''; + this.code = 0; + } + constructor(code: number, msg: string, data?: T) { this.name = 'BusinessError'; this.code = code; @@ -33,14 +40,11 @@ export type AsyncCallback = (err: BusinessError, data?: T) => vo function hash(path: string, algorithm: string): Promise { return new Promise((resolve: (result: string) => void, reject: (e: BusinessError) => void) => { let promise = taskpool.execute(hashImpl.hashSync, path, algorithm); - promise.then((ret: NullishType) => { - if (ret === null || ret === undefined) { - let err = new BusinessError(-1, ""); - reject(err); - } else { - let r = ret as string; - resolve(r); - } + promise.then((ret: NullishType): void => { + let res = ret as string; + resolve(res); + }).catch((e: BusinessError): void => { + reject(e); }); }); } @@ -48,15 +52,11 @@ function hash(path: string, algorithm: string): Promise { function hash(path: string, algorithm: string, callback: AsyncCallback): void { let promise = taskpool.execute(hashImpl.hashSync, path, algorithm); promise.then((ret: NullishType) => { - let err = new BusinessError(-1, ""); - if (ret === null || ret === undefined) { - err.code = -1 - callback(err, undefined) - } else { - err.code = 0 - let r = ret as string; - callback(err, r); - } + let e = new BusinessError(0, ""); + let res = ret as string; + callback(e, res); + }).catch((e: BusinessError): void => { + callback(e, ""); }); } diff --git a/interfaces/kits/js/src/mod_hash/ani/hash_ani.cpp b/interfaces/kits/js/src/mod_hash/ani/hash_ani.cpp index c8bffc53..0728869a 100644 --- a/interfaces/kits/js/src/mod_hash/ani/hash_ani.cpp +++ b/interfaces/kits/js/src/mod_hash/ani/hash_ani.cpp @@ -15,6 +15,7 @@ #include "hash_ani.h" +#include "error_handler.h" #include "filemgmt_libhilog.h" #include "hash_core.h" #include "type_converter.h" @@ -32,18 +33,22 @@ ani_string HashAni::HashSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani auto [succPath, srcPath] = TypeConverter::ToUTF8String(env, path); if (!succPath) { HILOGE("Invalid path"); + ErrorHandler::Throw(env, EINVAL); return nullptr; } auto [succAlg, algType] = TypeConverter::ToUTF8String(env, algorithm); if (!succAlg) { HILOGE("Invalid algorithm"); + ErrorHandler::Throw(env, EINVAL); return nullptr; } auto ret = HashCore::DoHash(srcPath, algType); if (!ret.IsSuccess()) { HILOGE("DoHash failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); return nullptr; } @@ -51,6 +56,7 @@ ani_string HashAni::HashSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani auto [succ, result] = TypeConverter::ToAniString(env, res); if (!succ) { HILOGE("Convert hash value to ani_string failed"); + ErrorHandler::Throw(env, UNKNOWN_ERR); return nullptr; } return result; -- Gitee