diff --git a/interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets b/interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets index 8738e3c4c1b794cd8c76f60f471ad917a3973ee6..3b577f890000f6436d002f2091f1d0ecb64eb9e5 100644 --- a/interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets +++ b/interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets @@ -1431,6 +1431,154 @@ class FileIoImpl { // console.println("errorHandlerTest4 end"); // } +function readTextSyncErr1() { + console.println("readTextSyncErr1 begin"); + try { + let ret = readTextSync("/data/local/tmp/afafes.txt.txt"); + console.println(`readTextSyncErr1 read text a.txt, result = ${ret}`); + + } catch (error) { + console.error("readTextSyncErr1 Error!", error); + } + console.println("readTextSyncErr1 end"); +} +function readTextSyncErr2() { + console.println("readTextSyncErr2 begin"); + let options: ReadTextOptions = { + offset: -1, + } + try { + let ret = readTextSync("/data/local/tmp/a.txt", options); + console.println(`readTextSyncErr2 read text a.txt, result = ${ret}`); + + } catch (error) { + console.error("readTextSyncErr2 Error!", error); + } + console.println("readTextSyncErr2 end"); +} +function readTextErr1() { + console.println("readTextErr1 begin"); + let options: ReadTextOptions = { + offset: -1, + } + try { + let ret = await readText("/data/local/tmp/a.txt", options); + console.println(`readTextErr1 read text a.txt, result = ${ret}`); + } catch (error) { + console.error("readTextErr1 Error!", error); + } + console.println("readTextErr1 end"); +} +function readTextErr2() { + console.println("readTextErr2 begin"); + let options: ReadTextOptions = { + offset: -1, + } + readText("/data/local/tmp/a.txt", options, (err: BusinessError, data?: string) => { + if (err.code == 0 && data !== undefined) { + console.println(`readTextErr2 read text a.txt, result = ${data}`); + } else { + console.error("readTextErr2 Error!", err); + } + }); + console.println("readTextErr2 end"); +} + + +function listFileSyncErr1() { + console.println("listFileSyncErr1 begin"); + try { + let ret = listFileSync("/data/local/tmp/zx/"); + console.println(`listFileSyncErr1 /data/local/tmp/zx/, result = ${ret}`); + } catch (error) { + console.error("listFileSyncErr1 Error!", error); + } + console.println("listFileSyncErr1 end"); +} +function listFileSyncErr2() { + console.println("listFileSyncErr2 begin"); + let options: ListFileOptions = { + listNum: -1, + } + try { + let ret = listFileSync("/data/local/tmp", options); + console.println(`listFileSyncErr2 /data/local/tmp, result = ${ret}`); + + } catch (error) { + console.error("listFileSyncErr2 Error!", error); + } + console.println("listFileSyncErr2 end"); +} +function listFileErr1() { + console.println("listFileErr1 begin"); + let options: ListFileOptions = { + listNum: -1, + } + try { + let ret = await listFile("/data/local/tmp/", options); + console.println(`listFileErr1 /data/local/tmp, result = ${ret}`); + } catch (error) { + console.error("listFileErr1 Error!", error); + } + console.println("listFileErr1 end"); +} +function listFileErr2() { + console.println("listFileErr2 begin"); + let options: ListFileOptions = { + listNum: -1, + } + listFile("/data/local/tmp", options, (err: BusinessError, data?: string[]) => { + if (err.code == 0 && data !== undefined) { + console.println(`listFileErr2 /data/local/tmp, result = ${data}`); + } else { + console.error("listFileErr2 Error!", err); + } + }); + console.println("listFileErr2 end"); +} + +function closeSyncErr1() { + console.println("closeSyncErr1 begin"); + try { + closeSync(-176); + console.println(`closeSyncErr1 -176`); + } catch (error) { + console.error("closeSyncErr1 Error!", error); + } + console.println("closeSyncErr1 end"); +} +function closeSyncErr2() { + console.println("closeSyncErr2 begin"); + try { + closeSync(65536); + console.println(`closeSyncErr2 65536`); + } catch (error) { + console.error("closeSyncErr2 Error!", error); + } + console.println("closeSyncErr2 end"); +} +function closeErr1() { + console.println("listFileErr1 begin"); + try { + let ret = await close(-1); + console.println(`closeErr1 -1, result = ${ret}`); + } catch (error) { + console.error("closeErr1 Error!", error); + } + console.println("closeErr1 end"); +} +function closeErr2() { + console.println("closeErr2 begin"); + close(-1, (err: BusinessError, data?: undefined) => { + if (err.code == 0) { + console.println(`closeErr2 Succ`); + } else { + console.error("closeErr2 Error!", err); + } + }); + console.println("closeErr2 end"); +} + function main() { console.println("---------- hello ani --------------"); // accessSyncTest1(); @@ -1477,5 +1625,15 @@ function main() { // errorHandlerTest2(); // 参数校验失败 // errorHandlerTest3(); // 异步Promise // errorHandlerTest4(); // 异步callback + readTextErr1(); + readTextErr2(); + listFileSyncErr1() + listFileSyncErr2(); + listFileErr1(); + listFileErr1(); + closeSyncErr1(); + closeSyncErr2() + closeErr1(); + closeErr2(); console.println("---------- hello ani end ---------------"); } \ No newline at end of file diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/close_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/close_ani.cpp index 750ec0cb0a0e35d69d28ad522235995edb8ece5f..f49ccf2f256d3f877b6970f70c7c27e698fcd57e 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/close_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/close_ani.cpp @@ -18,6 +18,7 @@ #include #include "close_core.h" +#include "error_handler.h" #include "filemgmt_libhilog.h" #include "type_converter.h" @@ -68,12 +69,15 @@ void CloseAni::CloseSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_obj auto [succ, fd] = ParseFd(env, obj); if (!succ) { HILOGE("Parse fd argument failed"); + ErrorHandler::Throw(env, EINVAL); return; } auto ret = CloseCore::DoClose(fd); if (!ret.IsSuccess()) { HILOGE("Close %d failed", fd); + const auto &err = ret.GetError(); + ErrorHandler::Throw(env, err); return; } }