diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index f992afa2628c799d5b7596aa3cf51ac0d6b6602f..fc91b25c799f58c75be9da862081815a9095869e 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -853,6 +853,7 @@ ohos_shared_library("ani_securitylabel_class") { "src/common/file_helper/fd_guard.cpp", "src/mod_fs/fs_utils.cpp", "src/mod_securitylabel/ani/bind_function_class.cpp", + "src/mod_securitylabel/ani/error_handler.cpp", "src/mod_securitylabel/ani/securitylabel_ani.cpp", "src/mod_securitylabel/securitylabel_core.cpp", ] 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 286d181561b3770c4043d3e5c8ee966087c10bd2..f9e50b448aeaff4e3c9f54b276645e63a8b335c2 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 @@ -67,4 +67,56 @@ class hashImpl { } static native hashSync(path: string, algorithm: string): string; +} + +function errorHandlerTestSucc() { + console.println("errorHandlerTestSucc begin"); + try { + let ret = await hash("/data/local/tmp/a.txt", "sha256"); + console.println(`errorHandlerTestSucc hash: ${ret}`); + } catch (error) { + console.error("errorHandlerTestSucc Error!", error); + } + + hash("/data/local/tmp/a.txt", "sha256", (err: BusinessError, data?: string) => { + if (err.code == 0 && data !== undefined) { + console.println(`errorHandlerTestSucc hash success: ${data}`); + } else { + console.error("errorHandlerTestSucc hash Error!", err); + } + }); + console.println("errorHandlerTestSucc end"); +} + +function errorHandlerTest3() { + console.println("errorHandlerTest3 begin"); + try { + let ret = await hash("/data/local/tmp/a.txt", "sha128"); + console.println(`errorHandlerTest3 hash: ${ret}`); + } catch (error) { + console.error("errorHandlerTest3 Error!", error); + } + console.println("errorHandlerTest3 end"); +} + +function errorHandlerTest4() { + console.println("errorHandlerTest4 begin"); + hash("/data/local/tmp/a.txt", "sha128", (err: BusinessError, data?: string) => { + if (err.code == 0 && data !== undefined) { + console.println(`errorHandlerTest4 hash success: ${data}`); + } else { + console.error("errorHandlerTest4 hash Error!", err); + } + }); + console.println("errorHandlerTest4 end"); +} + +function main() { + console.println("---------- hello ani --------------"); + + errorHandlerTestSucc(); + errorHandlerTest3(); + errorHandlerTest4(); + + console.println("---------- hello ani end ---------------"); } \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_securitylabel/ani/error_handler.cpp b/interfaces/kits/js/src/mod_securitylabel/ani/error_handler.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c302fc92a2a0ac9202c4446a881fdddc812b49b8 --- /dev/null +++ b/interfaces/kits/js/src/mod_securitylabel/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/securityLabel/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_securitylabel/ani/ets/@ohos.file.securityLabel.ets b/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets index 1458f300135b574026af0180313d70d9b9d8ae6e..1550fb4bdea5d4334d667f7247e60d8a0b5d8b05 100644 --- a/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.ets +++ b/interfaces/kits/js/src/mod_securitylabel/ani/ets/@ohos.file.securityLabel.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; @@ -37,7 +44,7 @@ function setSecurityLabel(path: string, type: DataLevel): Promise { let promise = taskpool.execute((path: string, type: DataLevel): void => securitylabelImpl.setSecurityLabelSync(path, type), path, type); promise.then((ret: NullishType): void => { resolve(undefined); - }).catch((e: BusinessError): void => { + }).catch((e: BusinessError): void => { reject(e); }); }); @@ -46,9 +53,9 @@ function setSecurityLabel(path: string, type: DataLevel): Promise { function setSecurityLabel(path: string, type: DataLevel, callback: AsyncCallback): void { let promise = taskpool.execute((path: string, type: DataLevel): void => securitylabelImpl.setSecurityLabelSync(path, type), path, type); promise.then((ret: NullishType): void => { - let e = new BusinessError(0, ""); + let e = new BusinessError(0, ""); callback(e, undefined); - }).catch((e: BusinessError): void => { + }).catch((e: BusinessError): void => { callback(e, undefined); }); } @@ -64,4 +71,61 @@ class securitylabelImpl { } static native setSecurityLabelSync(path: string, type: DataLevel): void; +} + +function errorHandlerTest1() { + console.println("errorHandlerTest1 begin"); + try { + setSecurityLabelSync("/data/local/tmp/uihuihuisu.txt", "s2"); + console.println(`errorHandlerTest1 setSecurityLabelSync to s1`); + } catch (error) { + console.error("errorHandlerTest1 Error!", error); + } + console.println("errorHandlerTest1 end"); +} + +function errorHandlerTest2() { + console.println("errorHandlerTest2 begin"); + try { + setSecurityLabelSync("/data/local/tmp/a.txt", "s0"); + console.println(`errorHandlerTest2 setSecurityLabelSync to s0`); + } catch (error) { + console.error("errorHandlerTest2 Error!", error); + } + console.println("errorHandlerTest2 end"); +} + +function errorHandlerTest3() { + console.println("errorHandlerTest3 begin"); + try { + let ret = await setSecurityLabel("/data/local/tmp/a.txt", "s0"); + console.println(`errorHandlerTest3 setSecurityLabelSync to s2`); + } catch (error) { + console.error("errorHandlerTest3 Error!", error); + } + console.println("errorHandlerTest3 end"); +} + +function errorHandlerTest4() { + console.println("errorHandlerTest4 begin"); + setSecurityLabel("/data/local/tmp/a.txt", "s0", (err: BusinessError, data?: undefined) => { + if (err.code == 0) { + console.println(`errorHandlerTest4 setSecurityLabel success`); + } else { + console.error("errorHandlerTest4 setSecurityLabel Error!", err); + } + }); + console.println("errorHandlerTest4 end"); +} + +function main() { + console.println("---------- hello ani --------------"); + + errorHandlerTest1(); // 打开文件失败,文件不存在 + setSecurityLabelSync("/data/local/tmp/a.txt", "s4"); + errorHandlerTest2(); // 参数校验失败 + errorHandlerTest3(); // 异步Promise + errorHandlerTest4(); // 异步callback + + console.println("---------- hello ani end ---------------"); } \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_securitylabel/ani/securitylabel_ani.cpp b/interfaces/kits/js/src/mod_securitylabel/ani/securitylabel_ani.cpp index 7c78437d23a7816d74552f03e374d15b71076f62..6c44deb69a515dcf5bf600643ea84b7886101809 100644 --- a/interfaces/kits/js/src/mod_securitylabel/ani/securitylabel_ani.cpp +++ b/interfaces/kits/js/src/mod_securitylabel/ani/securitylabel_ani.cpp @@ -15,6 +15,7 @@ #include "securitylabel_ani.h" +#include "error_handler.h" #include "filemgmt_libhilog.h" #include "securitylabel_core.h" #include "type_converter.h" @@ -34,18 +35,22 @@ void SecurityLabelAni::SetSecurityLabelSync( auto [succPath, srcPath] = TypeConverter::ToUTF8String(env, path); if (!succPath) { HILOGE("Invalid path"); + ErrorHandler::Throw(env, EINVAL); return; } auto [succLevel, dataLevel] = TypeConverter::ToUTF8String(env, level); if (!succLevel) { HILOGE("Invalid dataLevel"); + ErrorHandler::Throw(env, EINVAL); return; } auto ret = DoSetSecurityLabel(srcPath, dataLevel); if (!ret.IsSuccess()) { HILOGE("Set securitylabel failed"); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); return; } }