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/require.rs | |
parent | b063cfecfe0479b1de14b8e9e06f1921ce830127 (diff) |
refactor(ext/node): use concrete error types (#26419)
Diffstat (limited to 'ext/node/ops/require.rs')
-rw-r--r-- | ext/node/ops/require.rs | 93 |
1 files changed, 59 insertions, 34 deletions
diff --git a/ext/node/ops/require.rs b/ext/node/ops/require.rs index 7d85ee853..f4607f4e8 100644 --- a/ext/node/ops/require.rs +++ b/ext/node/ops/require.rs @@ -1,8 +1,5 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. -use deno_core::anyhow::Context; -use deno_core::error::generic_error; -use deno_core::error::AnyError; use deno_core::op2; use deno_core::url::Url; use deno_core::v8; @@ -30,7 +27,7 @@ use crate::NpmResolverRc; fn ensure_read_permission<'a, P>( state: &mut OpState, file_path: &'a Path, -) -> Result<Cow<'a, Path>, AnyError> +) -> Result<Cow<'a, Path>, deno_core::error::AnyError> where P: NodePermissions + 'static, { @@ -39,6 +36,32 @@ where resolver.ensure_read_permission(permissions, file_path) } +#[derive(Debug, thiserror::Error)] +pub enum RequireError { + #[error(transparent)] + UrlParse(#[from] url::ParseError), + #[error(transparent)] + Permission(deno_core::error::AnyError), + #[error(transparent)] + PackageExportsResolve( + #[from] node_resolver::errors::PackageExportsResolveError, + ), + #[error(transparent)] + PackageJsonLoad(#[from] node_resolver::errors::PackageJsonLoadError), + #[error(transparent)] + ClosestPkgJson(#[from] node_resolver::errors::ClosestPkgJsonError), + #[error(transparent)] + PackageImportsResolve( + #[from] node_resolver::errors::PackageImportsResolveError, + ), + #[error("failed to convert '{0}' to file path")] + FilePathConversion(Url), + #[error(transparent)] + Fs(#[from] deno_io::fs::FsError), + #[error("Unable to get CWD: {0}")] + UnableToGetCwd(deno_io::fs::FsError), +} + #[op2] #[serde] pub fn op_require_init_paths() -> Vec<String> { @@ -95,7 +118,7 @@ pub fn op_require_init_paths() -> Vec<String> { pub fn op_require_node_module_paths<P>( state: &mut OpState, #[string] from: String, -) -> Result<Vec<String>, AnyError> +) -> Result<Vec<String>, RequireError> where P: NodePermissions + 'static, { @@ -104,12 +127,12 @@ where let from = if from.starts_with("file:///") { url_to_file_path(&Url::parse(&from)?)? } else { - let current_dir = - &(fs.cwd().map_err(AnyError::from)).context("Unable to get CWD")?; - deno_path_util::normalize_path(current_dir.join(from)) + let current_dir = &fs.cwd().map_err(RequireError::UnableToGetCwd)?; + normalize_path(current_dir.join(from)) }; - let from = ensure_read_permission::<P>(state, &from)?; + let from = ensure_read_permission::<P>(state, &from) + .map_err(RequireError::Permission)?; if cfg!(windows) { // return root node_modules when path is 'D:\\'. @@ -264,7 +287,7 @@ pub fn op_require_path_is_absolute(#[string] p: String) -> bool { pub fn op_require_stat<P>( state: &mut OpState, #[string] path: String, -) -> Result<i32, AnyError> +) -> Result<i32, deno_core::error::AnyError> where P: NodePermissions + 'static, { @@ -287,12 +310,13 @@ where pub fn op_require_real_path<P>( state: &mut OpState, #[string] request: String, -) -> Result<String, AnyError> +) -> Result<String, RequireError> where P: NodePermissions + 'static, { let path = PathBuf::from(request); - let path = ensure_read_permission::<P>(state, &path)?; + let path = ensure_read_permission::<P>(state, &path) + .map_err(RequireError::Permission)?; let fs = state.borrow::<FileSystemRc>(); let canonicalized_path = deno_path_util::strip_unc_prefix(fs.realpath_sync(&path)?); @@ -319,12 +343,14 @@ pub fn op_require_path_resolve(#[serde] parts: Vec<String>) -> String { #[string] pub fn op_require_path_dirname( #[string] request: String, -) -> Result<String, AnyError> { +) -> Result<String, deno_core::error::AnyError> { let p = PathBuf::from(request); if let Some(parent) = p.parent() { Ok(parent.to_string_lossy().into_owned()) } else { - Err(generic_error("Path doesn't have a parent")) + Err(deno_core::error::generic_error( + "Path doesn't have a parent", + )) } } @@ -332,12 +358,14 @@ pub fn op_require_path_dirname( #[string] pub fn op_require_path_basename( #[string] request: String, -) -> Result<String, AnyError> { +) -> Result<String, deno_core::error::AnyError> { let p = PathBuf::from(request); if let Some(path) = p.file_name() { Ok(path.to_string_lossy().into_owned()) } else { - Err(generic_error("Path doesn't have a file name")) + Err(deno_core::error::generic_error( + "Path doesn't have a file name", + )) } } @@ -348,7 +376,7 @@ pub fn op_require_try_self_parent_path<P>( has_parent: bool, #[string] maybe_parent_filename: Option<String>, #[string] maybe_parent_id: Option<String>, -) -> Result<Option<String>, AnyError> +) -> Result<Option<String>, deno_core::error::AnyError> where P: NodePermissions + 'static, { @@ -378,7 +406,7 @@ pub fn op_require_try_self<P>( state: &mut OpState, #[string] parent_path: Option<String>, #[string] request: String, -) -> Result<Option<String>, AnyError> +) -> Result<Option<String>, RequireError> where P: NodePermissions + 'static, { @@ -440,12 +468,13 @@ where pub fn op_require_read_file<P>( state: &mut OpState, #[string] file_path: String, -) -> Result<String, AnyError> +) -> Result<String, RequireError> where P: NodePermissions + 'static, { let file_path = PathBuf::from(file_path); - let file_path = ensure_read_permission::<P>(state, &file_path)?; + let file_path = ensure_read_permission::<P>(state, &file_path) + .map_err(RequireError::Permission)?; let fs = state.borrow::<FileSystemRc>(); Ok(fs.read_text_file_lossy_sync(&file_path, None)?) } @@ -472,7 +501,7 @@ pub fn op_require_resolve_exports<P>( #[string] name: String, #[string] expansion: String, #[string] parent_path: String, -) -> Result<Option<String>, AnyError> +) -> Result<Option<String>, RequireError> where P: NodePermissions + 'static, { @@ -525,16 +554,14 @@ where pub fn op_require_read_closest_package_json<P>( state: &mut OpState, #[string] filename: String, -) -> Result<Option<PackageJsonRc>, AnyError> +) -> Result<Option<PackageJsonRc>, node_resolver::errors::ClosestPkgJsonError> where P: NodePermissions + 'static, { let filename = PathBuf::from(filename); // permissions: allow reading the closest package.json files let node_resolver = state.borrow::<NodeResolverRc>().clone(); - node_resolver - .get_closest_package_json_from_path(&filename) - .map_err(AnyError::from) + node_resolver.get_closest_package_json_from_path(&filename) } #[op2] @@ -564,12 +591,13 @@ pub fn op_require_package_imports_resolve<P>( state: &mut OpState, #[string] referrer_filename: String, #[string] request: String, -) -> Result<Option<String>, AnyError> +) -> Result<Option<String>, RequireError> where P: NodePermissions + 'static, { let referrer_path = PathBuf::from(&referrer_filename); - let referrer_path = ensure_read_permission::<P>(state, &referrer_path)?; + let referrer_path = ensure_read_permission::<P>(state, &referrer_path) + .map_err(RequireError::Permission)?; let node_resolver = state.borrow::<NodeResolverRc>(); let Some(pkg) = node_resolver.get_closest_package_json_from_path(&referrer_path)? @@ -578,8 +606,7 @@ where }; if pkg.imports.is_some() { - let referrer_url = - deno_core::url::Url::from_file_path(&referrer_filename).unwrap(); + let referrer_url = Url::from_file_path(&referrer_filename).unwrap(); let url = node_resolver.package_imports_resolve( &request, Some(&referrer_url), @@ -604,17 +631,15 @@ pub fn op_require_break_on_next_statement(state: Rc<RefCell<OpState>>) { inspector.wait_for_session_and_break_on_next_statement() } -fn url_to_file_path_string(url: &Url) -> Result<String, AnyError> { +fn url_to_file_path_string(url: &Url) -> Result<String, RequireError> { let file_path = url_to_file_path(url)?; Ok(file_path.to_string_lossy().into_owned()) } -fn url_to_file_path(url: &Url) -> Result<PathBuf, AnyError> { +fn url_to_file_path(url: &Url) -> Result<PathBuf, RequireError> { match url.to_file_path() { Ok(file_path) => Ok(file_path), - Err(()) => { - deno_core::anyhow::bail!("failed to convert '{}' to file path", url) - } + Err(()) => Err(RequireError::FilePathConversion(url.clone())), } } |