From dd1e6500639c25d73ff63dd8fde6ba093b2b4255 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Og=C3=B3rek?= Date: Sun, 19 Mar 2023 01:01:50 +0100 Subject: [PATCH] fix(runtime): Extract error code for all OS error variants (#17958) --- cli/tests/unit/read_file_test.ts | 26 ++++++++++++++++++++++++++ ext/fs/lib.rs | 10 +++++----- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/cli/tests/unit/read_file_test.ts b/cli/tests/unit/read_file_test.ts index 24cf6dccf5..5761cb1cbb 100644 --- a/cli/tests/unit/read_file_test.ts +++ b/cli/tests/unit/read_file_test.ts @@ -158,3 +158,29 @@ Deno.test( assert(data.byteLength > 0); }, ); + +Deno.test( + { permissions: { read: true } }, + async function readFileNotFoundErrorCode() { + try { + await Deno.readFile("definitely-not-found.json"); + } catch (e) { + assertEquals(e.code, "ENOENT"); + } + }, +); + +Deno.test( + { permissions: { read: true } }, + async function readFileIsDirectoryErrorCode() { + try { + await Deno.readFile("cli/tests/testdata/assets/"); + } catch (e) { + if (Deno.build.os === "windows") { + assertEquals(e.code, "ENOENT"); + } else { + assertEquals(e.code, "EISDIR"); + } + } + }, +); diff --git a/ext/fs/lib.rs b/ext/fs/lib.rs index 386d143d21..3bad653304 100644 --- a/ext/fs/lib.rs +++ b/ext/fs/lib.rs @@ -188,8 +188,8 @@ deno_core::extension!(deno_fs, }, ); -fn default_err_mapper(err: Error, desc: String) -> Error { - Error::new(err.kind(), format!("{err}, {desc}")) +fn default_err_mapper(err: Error, desc: String) -> AnyError { + AnyError::new(Error::new(err.kind(), desc)).context(err) } #[derive(Deserialize, Default, Debug)] @@ -1017,7 +1017,7 @@ where let mut st = std::mem::zeroed(); let ret = stat(from.as_ptr(), &mut st); if ret != 0 { - return Err(err_mapper(Error::last_os_error()).into()); + return Err(err_mapper(Error::last_os_error())); } if st.st_size > 128 * 1024 { @@ -2347,9 +2347,9 @@ where let read_future = tokio::task::spawn_blocking(move || { let path = Path::new(&path); - Ok(std::fs::read(path).map(ZeroCopyBuf::from).map_err(|err| { + std::fs::read(path).map(ZeroCopyBuf::from).map_err(|err| { default_err_mapper(err, format!("readfile '{}'", path.display())) - })?) + }) }); let cancel_handle = cancel_rid.and_then(|rid| {