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

refactor(ext/napi): use concrete error types (#26186)

This commit is contained in:
Leo Kettmeir 2024-10-17 12:51:33 -07:00 committed by GitHub
parent 9fde5cb5e0
commit 8cfd9968fa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 39 additions and 19 deletions

1
Cargo.lock generated
View file

@ -1772,6 +1772,7 @@ dependencies = [
"deno_core",
"deno_permissions",
"libloading 0.7.4",
"thiserror",
]
[[package]]

View file

@ -17,3 +17,4 @@ path = "lib.rs"
deno_core.workspace = true
deno_permissions.workspace = true
libloading = { version = "0.7" }
thiserror.workspace = true

View file

@ -6,8 +6,6 @@
#![deny(clippy::missing_safety_doc)]
use core::ptr::NonNull;
use deno_core::error::type_error;
use deno_core::error::AnyError;
use deno_core::op2;
use deno_core::parking_lot::RwLock;
use deno_core::url::Url;
@ -20,6 +18,18 @@ use std::path::PathBuf;
use std::rc::Rc;
use std::thread_local;
#[derive(Debug, thiserror::Error)]
pub enum NApiError {
#[error("Invalid path")]
InvalidPath,
#[error(transparent)]
LibLoading(#[from] libloading::Error),
#[error("Unable to find register Node-API module at {}", .0.display())]
ModuleNotFound(PathBuf),
#[error(transparent)]
Permission(deno_core::error::AnyError),
}
#[cfg(unix)]
use libloading::os::unix::*;
@ -482,14 +492,20 @@ deno_core::extension!(deno_napi,
pub trait NapiPermissions {
#[must_use = "the resolved return value to mitigate time-of-check to time-of-use issues"]
fn check(&mut self, path: &str) -> std::result::Result<PathBuf, AnyError>;
fn check(
&mut self,
path: &str,
) -> Result<PathBuf, deno_core::error::AnyError>;
}
// NOTE(bartlomieju): for now, NAPI uses `--allow-ffi` flag, but that might
// change in the future.
impl NapiPermissions for deno_permissions::PermissionsContainer {
#[inline(always)]
fn check(&mut self, path: &str) -> Result<PathBuf, AnyError> {
fn check(
&mut self,
path: &str,
) -> Result<PathBuf, deno_core::error::AnyError> {
deno_permissions::PermissionsContainer::check_ffi(self, path)
}
}
@ -512,7 +528,7 @@ fn op_napi_open<NP, 'scope>(
global: v8::Local<'scope, v8::Object>,
buffer_constructor: v8::Local<'scope, v8::Function>,
report_error: v8::Local<'scope, v8::Function>,
) -> std::result::Result<v8::Local<'scope, v8::Value>, AnyError>
) -> Result<v8::Local<'scope, v8::Value>, NApiError>
where
NP: NapiPermissions + 'static,
{
@ -521,7 +537,7 @@ where
let (async_work_sender, cleanup_hooks, external_ops_tracker, path) = {
let mut op_state = op_state.borrow_mut();
let permissions = op_state.borrow_mut::<NP>();
let path = permissions.check(&path)?;
let path = permissions.check(&path).map_err(NApiError::Permission)?;
let napi_state = op_state.borrow::<NapiState>();
(
op_state.borrow::<V8CrossThreadTaskSpawner>().clone(),
@ -540,7 +556,7 @@ where
let type_tag = v8::Global::new(scope, type_tag);
let url_filename =
Url::from_file_path(&path).map_err(|_| type_error("Invalid path"))?;
Url::from_file_path(&path).map_err(|_| NApiError::InvalidPath)?;
let env_shared =
EnvShared::new(napi_wrap, type_tag, format!("{url_filename}\0"));
@ -565,17 +581,11 @@ where
// SAFETY: opening a DLL calls dlopen
#[cfg(unix)]
let library = match unsafe { Library::open(Some(&path), flags) } {
Ok(lib) => lib,
Err(e) => return Err(type_error(e.to_string())),
};
let library = unsafe { Library::open(Some(&path), flags) }?;
// SAFETY: opening a DLL calls dlopen
#[cfg(not(unix))]
let library = match unsafe { Library::load_with_flags(&path, flags) } {
Ok(lib) => lib,
Err(e) => return Err(type_error(e.to_string())),
};
let library = unsafe { Library::load_with_flags(&path, flags) }?;
let maybe_module = MODULE_TO_REGISTER.with(|cell| {
let mut slot = cell.borrow_mut();
@ -610,10 +620,7 @@ where
// SAFETY: we are going blind, calling the register function on the other side.
unsafe { init(env_ptr, exports.into()) }
} else {
return Err(type_error(format!(
"Unable to find register Node-API module at {}",
path.display()
)));
return Err(NApiError::ModuleNotFound(path));
};
let exports = maybe_exports.unwrap_or(exports.into());

View file

@ -26,6 +26,7 @@ use deno_ffi::StaticError;
use deno_kv::KvCheckError;
use deno_kv::KvError;
use deno_kv::KvMutationError;
use deno_napi::NApiError;
use deno_net::ops::NetError;
use deno_tls::TlsError;
use deno_web::BlobError;
@ -174,6 +175,15 @@ pub fn get_nix_error_class(error: &nix::Error) -> &'static str {
}
}
fn get_napi_error_class(e: &NApiError) -> &'static str {
match e {
NApiError::InvalidPath
| NApiError::LibLoading(_)
| NApiError::ModuleNotFound(_) => "TypeError",
NApiError::Permission(e) => get_error_class_name(e).unwrap_or("Error"),
}
}
fn get_web_error_class(e: &WebError) -> &'static str {
match e {
WebError::Base64Decode => "DOMExceptionInvalidCharacterError",
@ -443,6 +453,7 @@ pub fn get_error_class_name(e: &AnyError) -> Option<&'static str> {
deno_core::error::get_custom_error_class(e)
.or_else(|| deno_webgpu::error::get_error_class_name(e))
.or_else(|| deno_websocket::get_network_error_class_name(e))
.or_else(|| e.downcast_ref::<NApiError>().map(get_napi_error_class))
.or_else(|| e.downcast_ref::<WebError>().map(get_web_error_class))
.or_else(|| {
e.downcast_ref::<CompressionError>()