Skip to content

Commit f075094

Browse files
authored
fix(engine): Prefer js_error! over JsError::from_opaque (#5116)
In a few places, native code was throwing `JsError::from_opaque(js_string!("some literal"))`. This is allowed, but it is not exactly standard: most users will expect thrown values to be of type `Error`. So, where possible, let's convert code using that to instead use the provided `js_error!` macro, which does generate proper `Error` objects. This doesn't touch `AbortError`, because (1) that's a larger code change, and (2) it might actually be relied on as a sentinel value.
1 parent b6e0fb4 commit f075094

File tree

2 files changed

+10
-20
lines changed

2 files changed

+10
-20
lines changed

core/engine/src/interop/into_js_function_impls.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use super::private::IntoJsFunctionSealed;
44
use super::{IntoJsFunctionCopied, UnsafeIntoJsFunction};
55
use crate::interop::{JsRest, TryFromJsArgument};
6-
use crate::{Context, JsError, NativeFunction, TryIntoJsResult, js_string};
6+
use crate::{Context, NativeFunction, TryIntoJsResult};
77
use std::cell::RefCell;
88

99
/// A token to represent the context argument in the function signature.
@@ -59,7 +59,7 @@ macro_rules! impl_into_js_function {
5959
match s.try_borrow_mut() {
6060
Ok(mut r) => r( $($id,)* ).try_into_js_result(ctx),
6161
Err(_) => {
62-
Err(JsError::from_opaque(js_string!("recursive calls to this function not supported").into()))
62+
Err($crate::js_error!("recursive calls to this function not supported"))
6363
}
6464
}
6565
})
@@ -85,7 +85,7 @@ macro_rules! impl_into_js_function {
8585
match s.try_borrow_mut() {
8686
Ok(mut r) => r( $($id,)* rest.into() ).try_into_js_result(ctx),
8787
Err(_) => {
88-
Err(JsError::from_opaque(js_string!("recursive calls to this function not supported").into()))
88+
Err($crate::js_error!("recursive calls to this function not supported"))
8989
}
9090
}
9191
})

core/engine/src/module/loader/mod.rs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,14 @@ pub fn resolve_module_specifier(
7474
if let Some(r_path) = referrer_dir {
7575
base_path.join(r_path).join(short_path)
7676
} else {
77-
return Err(JsError::from_opaque(
78-
js_string!("relative path without referrer").into(),
79-
));
77+
return Err(js_error!(TypeError: "relative path without referrer"));
8078
}
8179
} else {
8280
base_path.join(&*specifier)
8381
};
8482

8583
if long_path.is_relative() && base.is_some() {
86-
return Err(JsError::from_opaque(
87-
js_string!("resolved path is relative").into(),
88-
));
84+
return Err(js_error!(TypeError: "resolved path is relative"));
8985
}
9086

9187
// Normalize the path. We cannot use `canonicalize` here because it will fail
@@ -96,9 +92,7 @@ pub fn resolve_module_specifier(
9692
.try_fold(PathBuf::new(), |mut acc, c| {
9793
if c == Component::ParentDir {
9894
if acc.as_os_str().is_empty() {
99-
return Err(JsError::from_opaque(
100-
js_string!("path is outside the module root").into(),
101-
));
95+
return Err(js_error!(TypeError: "path is outside the module root"));
10296
}
10397
acc.pop();
10498
} else {
@@ -110,9 +104,7 @@ pub fn resolve_module_specifier(
110104
if path.starts_with(&base_path) {
111105
Ok(path)
112106
} else {
113-
Err(JsError::from_opaque(
114-
js_string!("path is outside the module root").into(),
115-
))
107+
Err(js_error!(TypeError: "path is outside the module root"))
116108
}
117109
}
118110

@@ -320,7 +312,7 @@ impl SimpleModuleLoader {
320312
let absolute = root.canonicalize().map_err(|e| {
321313
JsNativeError::typ()
322314
.with_message(format!("could not set module root `{}`", root.display()))
323-
.with_cause(JsError::from_opaque(js_string!(e.to_string()).into()))
315+
.with_cause(JsError::from_rust(e))
324316
})?;
325317
Ok(Self {
326318
root: absolute,
@@ -416,9 +408,7 @@ impl ModuleLoader for SimpleModuleLoader {
416408
let json_content = std::fs::read_to_string(&path).map_err(|err| {
417409
JsNativeError::typ()
418410
.with_message(format!("could not open file `{short_path}`"))
419-
.with_cause(JsError::from_opaque(
420-
js_string!(err.to_string()).into(),
421-
))
411+
.with_cause(JsError::from_rust(err))
422412
})?;
423413
let json_string = js_string!(json_content.as_str());
424414
Module::parse_json(json_string, &mut context.borrow_mut()).map_err(
@@ -445,7 +435,7 @@ impl ModuleLoader for SimpleModuleLoader {
445435
let source = Source::from_filepath(&path).map_err(|err| {
446436
JsNativeError::typ()
447437
.with_message(format!("could not open file `{short_path}`"))
448-
.with_cause(JsError::from_opaque(js_string!(err.to_string()).into()))
438+
.with_cause(JsError::from_rust(err))
449439
})?;
450440
Module::parse(source, None, &mut context.borrow_mut()).map_err(|err| {
451441
JsNativeError::syntax()

0 commit comments

Comments
 (0)