1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

fix(runtime): Extract error code for all OS error variants (#17958)

This commit is contained in:
Kamil Ogórek 2023-03-19 01:01:50 +01:00 committed by GitHub
parent b64ec79268
commit dd1e650063
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 5 deletions

View file

@ -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");
}
}
},
);

View file

@ -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| {