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/worker_threads.rs | |
parent | b063cfecfe0479b1de14b8e9e06f1921ce830127 (diff) |
refactor(ext/node): use concrete error types (#26419)
Diffstat (limited to 'ext/node/ops/worker_threads.rs')
-rw-r--r-- | ext/node/ops/worker_threads.rs | 61 |
1 files changed, 45 insertions, 16 deletions
diff --git a/ext/node/ops/worker_threads.rs b/ext/node/ops/worker_threads.rs index 5f139c5dc..e33cf9157 100644 --- a/ext/node/ops/worker_threads.rs +++ b/ext/node/ops/worker_threads.rs @@ -1,7 +1,5 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. -use deno_core::error::generic_error; -use deno_core::error::AnyError; use deno_core::op2; use deno_core::url::Url; use deno_core::OpState; @@ -19,7 +17,7 @@ use crate::NodeResolverRc; 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, { @@ -28,12 +26,36 @@ where resolver.ensure_read_permission(permissions, file_path) } +#[derive(Debug, thiserror::Error)] +pub enum WorkerThreadsFilenameError { + #[error(transparent)] + Permission(deno_core::error::AnyError), + #[error("{0}")] + UrlParse(#[from] url::ParseError), + #[error("Relative path entries must start with '.' or '..'")] + InvalidRelativeUrl, + #[error("URL from Path-String")] + UrlFromPathString, + #[error("URL to Path-String")] + UrlToPathString, + #[error("URL to Path")] + UrlToPath, + #[error("File not found [{0:?}]")] + FileNotFound(PathBuf), + #[error("Neither ESM nor CJS")] + NeitherEsmNorCjs, + #[error("{0}")] + UrlToNodeResolution(node_resolver::errors::UrlToNodeResolutionError), + #[error(transparent)] + Fs(#[from] deno_io::fs::FsError), +} + #[op2] #[string] pub fn op_worker_threads_filename<P>( state: &mut OpState, #[string] specifier: String, -) -> Result<String, AnyError> +) -> Result<String, WorkerThreadsFilenameError> where P: NodePermissions + 'static, { @@ -45,40 +67,47 @@ where } else { let path = PathBuf::from(&specifier); if path.is_relative() && !specifier.starts_with('.') { - return Err(generic_error( - "Relative path entries must start with '.' or '..'", - )); + return Err(WorkerThreadsFilenameError::InvalidRelativeUrl); } - let path = ensure_read_permission::<P>(state, &path)?; + let path = ensure_read_permission::<P>(state, &path) + .map_err(WorkerThreadsFilenameError::Permission)?; let fs = state.borrow::<FileSystemRc>(); let canonicalized_path = deno_path_util::strip_unc_prefix(fs.realpath_sync(&path)?); Url::from_file_path(canonicalized_path) - .map_err(|e| generic_error(format!("URL from Path-String: {:#?}", e)))? + .map_err(|_| WorkerThreadsFilenameError::UrlFromPathString)? }; let url_path = url .to_file_path() - .map_err(|e| generic_error(format!("URL to Path-String: {:#?}", e)))?; - let url_path = ensure_read_permission::<P>(state, &url_path)?; + .map_err(|_| WorkerThreadsFilenameError::UrlToPathString)?; + let url_path = ensure_read_permission::<P>(state, &url_path) + .map_err(WorkerThreadsFilenameError::Permission)?; let fs = state.borrow::<FileSystemRc>(); if !fs.exists_sync(&url_path) { - return Err(generic_error(format!("File not found [{:?}]", url_path))); + return Err(WorkerThreadsFilenameError::FileNotFound( + url_path.to_path_buf(), + )); } let node_resolver = state.borrow::<NodeResolverRc>(); - match node_resolver.url_to_node_resolution(url)? { + match node_resolver + .url_to_node_resolution(url) + .map_err(WorkerThreadsFilenameError::UrlToNodeResolution)? + { NodeResolution::Esm(u) => Ok(u.to_string()), NodeResolution::CommonJs(u) => wrap_cjs(u), - NodeResolution::BuiltIn(_) => Err(generic_error("Neither ESM nor CJS")), + NodeResolution::BuiltIn(_) => { + Err(WorkerThreadsFilenameError::NeitherEsmNorCjs) + } } } /// /// Wrap a CJS file-URL and the required setup in a stringified `data:`-URL /// -fn wrap_cjs(url: Url) -> Result<String, AnyError> { +fn wrap_cjs(url: Url) -> Result<String, WorkerThreadsFilenameError> { let path = url .to_file_path() - .map_err(|e| generic_error(format!("URL to Path: {:#?}", e)))?; + .map_err(|_| WorkerThreadsFilenameError::UrlToPath)?; let filename = path.file_name().unwrap().to_string_lossy(); Ok(format!( "data:text/javascript,import {{ createRequire }} from \"node:module\";\ |