diff options
author | Leo Kettmeir <crowlkats@toaxl.com> | 2024-10-24 10:45:17 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-10-24 10:45:17 -0700 |
commit | c71e020668b40666aecfdffb1dbf979abcb41958 (patch) | |
tree | 6b963905f50c17c21d9a89a5f5f7eee2fa83e808 /ext/node/ops/fs.rs | |
parent | b063cfecfe0479b1de14b8e9e06f1921ce830127 (diff) |
refactor(ext/node): use concrete error types (#26419)
Diffstat (limited to 'ext/node/ops/fs.rs')
-rw-r--r-- | ext/node/ops/fs.rs | 76 |
1 files changed, 49 insertions, 27 deletions
diff --git a/ext/node/ops/fs.rs b/ext/node/ops/fs.rs index 6253f32d0..6645792e7 100644 --- a/ext/node/ops/fs.rs +++ b/ext/node/ops/fs.rs @@ -3,7 +3,6 @@ use std::cell::RefCell; use std::rc::Rc; -use deno_core::error::AnyError; use deno_core::op2; use deno_core::OpState; use deno_fs::FileSystemRc; @@ -11,11 +10,27 @@ use serde::Serialize; use crate::NodePermissions; +#[derive(Debug, thiserror::Error)] +pub enum FsError { + #[error(transparent)] + Permission(deno_core::error::AnyError), + #[error("{0}")] + Io(#[from] std::io::Error), + #[cfg(windows)] + #[error("Path has no root.")] + PathHasNoRoot, + #[cfg(not(any(unix, windows)))] + #[error("Unsupported platform.")] + UnsupportedPlatform, + #[error(transparent)] + Fs(#[from] deno_io::fs::FsError), +} + #[op2(fast)] pub fn op_node_fs_exists_sync<P>( state: &mut OpState, #[string] path: String, -) -> Result<bool, AnyError> +) -> Result<bool, deno_core::error::AnyError> where P: NodePermissions + 'static, { @@ -30,7 +45,7 @@ where pub async fn op_node_fs_exists<P>( state: Rc<RefCell<OpState>>, #[string] path: String, -) -> Result<bool, AnyError> +) -> Result<bool, FsError> where P: NodePermissions + 'static, { @@ -38,7 +53,8 @@ where let mut state = state.borrow_mut(); let path = state .borrow_mut::<P>() - .check_read_with_api_name(&path, Some("node:fs.exists()"))?; + .check_read_with_api_name(&path, Some("node:fs.exists()")) + .map_err(FsError::Permission)?; (state.borrow::<FileSystemRc>().clone(), path) }; @@ -50,16 +66,18 @@ pub fn op_node_cp_sync<P>( state: &mut OpState, #[string] path: &str, #[string] new_path: &str, -) -> Result<(), AnyError> +) -> Result<(), FsError> where P: NodePermissions + 'static, { let path = state .borrow_mut::<P>() - .check_read_with_api_name(path, Some("node:fs.cpSync"))?; + .check_read_with_api_name(path, Some("node:fs.cpSync")) + .map_err(FsError::Permission)?; let new_path = state .borrow_mut::<P>() - .check_write_with_api_name(new_path, Some("node:fs.cpSync"))?; + .check_write_with_api_name(new_path, Some("node:fs.cpSync")) + .map_err(FsError::Permission)?; let fs = state.borrow::<FileSystemRc>(); fs.cp_sync(&path, &new_path)?; @@ -71,7 +89,7 @@ pub async fn op_node_cp<P>( state: Rc<RefCell<OpState>>, #[string] path: String, #[string] new_path: String, -) -> Result<(), AnyError> +) -> Result<(), FsError> where P: NodePermissions + 'static, { @@ -79,10 +97,12 @@ where let mut state = state.borrow_mut(); let path = state .borrow_mut::<P>() - .check_read_with_api_name(&path, Some("node:fs.cpSync"))?; + .check_read_with_api_name(&path, Some("node:fs.cpSync")) + .map_err(FsError::Permission)?; let new_path = state .borrow_mut::<P>() - .check_write_with_api_name(&new_path, Some("node:fs.cpSync"))?; + .check_write_with_api_name(&new_path, Some("node:fs.cpSync")) + .map_err(FsError::Permission)?; (state.borrow::<FileSystemRc>().clone(), path, new_path) }; @@ -108,7 +128,7 @@ pub fn op_node_statfs<P>( state: Rc<RefCell<OpState>>, #[string] path: String, bigint: bool, -) -> Result<StatFs, AnyError> +) -> Result<StatFs, FsError> where P: NodePermissions + 'static, { @@ -116,10 +136,12 @@ where let mut state = state.borrow_mut(); let path = state .borrow_mut::<P>() - .check_read_with_api_name(&path, Some("node:fs.statfs"))?; + .check_read_with_api_name(&path, Some("node:fs.statfs")) + .map_err(FsError::Permission)?; state .borrow_mut::<P>() - .check_sys("statfs", "node:fs.statfs")?; + .check_sys("statfs", "node:fs.statfs") + .map_err(FsError::Permission)?; path }; #[cfg(unix)] @@ -176,7 +198,6 @@ where } #[cfg(windows)] { - use deno_core::anyhow::anyhow; use std::ffi::OsStr; use std::os::windows::ffi::OsStrExt; use windows_sys::Win32::Storage::FileSystem::GetDiskFreeSpaceW; @@ -186,10 +207,7 @@ where // call below. #[allow(clippy::disallowed_methods)] let path = path.canonicalize()?; - let root = path - .ancestors() - .last() - .ok_or(anyhow!("Path has no root."))?; + let root = path.ancestors().last().ok_or(FsError::PathHasNoRoot)?; let mut root = OsStr::new(root).encode_wide().collect::<Vec<_>>(); root.push(0); let mut sectors_per_cluster = 0; @@ -229,7 +247,7 @@ where { let _ = path; let _ = bigint; - Err(anyhow!("Unsupported platform.")) + Err(FsError::UnsupportedPlatform) } } @@ -241,13 +259,14 @@ pub fn op_node_lutimes_sync<P>( #[smi] atime_nanos: u32, #[number] mtime_secs: i64, #[smi] mtime_nanos: u32, -) -> Result<(), AnyError> +) -> Result<(), FsError> where P: NodePermissions + 'static, { let path = state .borrow_mut::<P>() - .check_write_with_api_name(path, Some("node:fs.lutimes"))?; + .check_write_with_api_name(path, Some("node:fs.lutimes")) + .map_err(FsError::Permission)?; let fs = state.borrow::<FileSystemRc>(); fs.lutime_sync(&path, atime_secs, atime_nanos, mtime_secs, mtime_nanos)?; @@ -262,7 +281,7 @@ pub async fn op_node_lutimes<P>( #[smi] atime_nanos: u32, #[number] mtime_secs: i64, #[smi] mtime_nanos: u32, -) -> Result<(), AnyError> +) -> Result<(), FsError> where P: NodePermissions + 'static, { @@ -270,7 +289,8 @@ where let mut state = state.borrow_mut(); let path = state .borrow_mut::<P>() - .check_write_with_api_name(&path, Some("node:fs.lutimesSync"))?; + .check_write_with_api_name(&path, Some("node:fs.lutimesSync")) + .map_err(FsError::Permission)?; (state.borrow::<FileSystemRc>().clone(), path) }; @@ -286,13 +306,14 @@ pub fn op_node_lchown_sync<P>( #[string] path: String, uid: Option<u32>, gid: Option<u32>, -) -> Result<(), AnyError> +) -> Result<(), FsError> where P: NodePermissions + 'static, { let path = state .borrow_mut::<P>() - .check_write_with_api_name(&path, Some("node:fs.lchownSync"))?; + .check_write_with_api_name(&path, Some("node:fs.lchownSync")) + .map_err(FsError::Permission)?; let fs = state.borrow::<FileSystemRc>(); fs.lchown_sync(&path, uid, gid)?; Ok(()) @@ -304,7 +325,7 @@ pub async fn op_node_lchown<P>( #[string] path: String, uid: Option<u32>, gid: Option<u32>, -) -> Result<(), AnyError> +) -> Result<(), FsError> where P: NodePermissions + 'static, { @@ -312,7 +333,8 @@ where let mut state = state.borrow_mut(); let path = state .borrow_mut::<P>() - .check_write_with_api_name(&path, Some("node:fs.lchown"))?; + .check_write_with_api_name(&path, Some("node:fs.lchown")) + .map_err(FsError::Permission)?; (state.borrow::<FileSystemRc>().clone(), path) }; fs.lchown_async(path, uid, gid).await?; |