Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(compat): add .code to dyn import error #12633

Merged
merged 7 commits into from
Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions cli/proc_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::version;

use deno_core::error::anyhow;
use deno_core::error::custom_error;
use deno_core::error::custom_error_with_js_constructor;
use deno_core::error::get_custom_error_class;
use deno_core::error::AnyError;
use deno_core::error::Context;
Expand Down Expand Up @@ -517,18 +518,20 @@ impl ProcState {
return Ok(module_source.clone());
}
} else {
let mut message = format!("Cannot load module \"{}\".", specifier);
if maybe_referrer.is_some() && !is_dynamic {
if let Some(span) = graph_data.resolved_map.get(&specifier) {
return Err(custom_error(
"NotFound",
format!("Cannot load module \"{}\".\n at {}", specifier, span),
));
message =
format!("Cannot load module \"{}\".\n at {}", specifier, span);
}
}
return Err(custom_error(
"NotFound",
format!("Cannot load module \"{}\".", specifier),
));
if self.flags.compat {
return Err(custom_error_with_js_constructor(
"Deno.compat.errors.ERR_MODULE_NOT_FOUND",
message,
));
}
return Err(custom_error("NotFound", message));
}
}

Expand Down
5 changes: 5 additions & 0 deletions cli/tests/integration/compat_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ itest!(compat_with_import_map_and_https_imports {
output: "compat/import_map_https_imports.out",
});

itest!(compat_dyn_import_rejects_with_node_compatible_error {
args: "run --quiet --compat --unstable -A compat/dyn_import_reject.js",
output: "compat/dyn_import_reject.out",
});

#[test]
fn globals_in_repl() {
let (out, _err) = util::run_and_collect_output_with_args(
Expand Down
4 changes: 4 additions & 0 deletions cli/tests/testdata/compat/dyn_import_reject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import("./foobar.js").catch((e) => {
console.log(e);
console.log(e.code);
});
2 changes: 2 additions & 0 deletions cli/tests/testdata/compat/dyn_import_reject.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Error: Cannot load module "file:///[WILDCARD]/testdata/compat/foobar.js".
ERR_MODULE_NOT_FOUND
43 changes: 42 additions & 1 deletion core/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,51 @@ impl Display for CustomError {

impl Error for CustomError {}

pub fn custom_error_with_js_constructor(
js_constructor: &'static str,
message: impl Into<Cow<'static, str>>,
) -> AnyError {
CustomErrorWithJsConstructor {
js_constructor,
message: message.into(),
}
.into()
}

#[derive(Debug)]
struct CustomErrorWithJsConstructor {
js_constructor: &'static str,
message: Cow<'static, str>,
}

impl Display for CustomErrorWithJsConstructor {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.write_str(&self.message)
}
}

impl Error for CustomErrorWithJsConstructor {}

/// If this error was crated with `custom_error()`, return the specified error
/// class name. In all other cases this function returns `None`.
pub fn get_custom_error_class(error: &AnyError) -> Option<&'static str> {
error.downcast_ref::<CustomError>().map(|e| e.class)
error
.downcast_ref::<CustomError>()
.map(|e| e.class)
.or_else(|| {
error
.downcast_ref::<CustomErrorWithJsConstructor>()
.map(|e| e.js_constructor)
})
}

/// If this error was crated with `custom_error_with_js_constructor()`, return
/// the specified js constructor class name. In all other cases this function
/// returns `None`.
pub fn get_custom_js_constructor(error: &AnyError) -> Option<&'static str> {
error
.downcast_ref::<CustomErrorWithJsConstructor>()
.map(|e| e.js_constructor)
}

/// A `JsError` represents an exception coming from V8, with stack frames and
Expand Down
29 changes: 29 additions & 0 deletions core/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use crate::bindings;
use crate::error::attach_handle_to_error;
use crate::error::generic_error;
use crate::error::get_custom_js_constructor;
use crate::error::AnyError;
use crate::error::ErrWithV8Handle;
use crate::error::JsError;
Expand Down Expand Up @@ -561,7 +562,14 @@ impl JsRuntime {
source_code: &str,
) -> Result<v8::Global<v8::Value>, AnyError> {
let scope = &mut self.handle_scope();
Self::execute_script_with_scope(scope, name, source_code)
}

fn execute_script_with_scope(
scope: &mut v8::HandleScope,
name: &str,
source_code: &str,
) -> Result<v8::Global<v8::Value>, AnyError> {
let source = v8::String::new(scope, source_code).unwrap();
let name = v8::String::new(scope, name).unwrap();
let origin = bindings::script_origin(scope, name);
Expand Down Expand Up @@ -1144,6 +1152,27 @@ impl JsRuntime {
let exception = err
.downcast_ref::<ErrWithV8Handle>()
.map(|err| err.get_handle(scope))
.or_else(|| {
if let Some(js_constructor) = get_custom_js_constructor(&err) {
if let Ok(err_class) = Self::execute_script_with_scope(
scope,
"<anonymous>",
js_constructor,
) {
let value = v8::Local::<v8::Value>::new(scope, err_class);
if let Ok(err_class) = v8::Local::<v8::Function>::try_from(value) {
let message = v8::String::new(scope, &err.to_string()).unwrap();
return Some(
err_class
.new_instance(scope, &[message.into()])
.unwrap()
.into(),
);
}
}
}
None
})
.unwrap_or_else(|| {
let message = err.to_string();
let message = v8::String::new(scope, &message).unwrap();
Expand Down
23 changes: 23 additions & 0 deletions runtime/js/07_compat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
"use strict";

((window) => {
const {
Error,
} = window.__bootstrap.primordials;

class ERR_MODULE_NOT_FOUND extends Error {
constructor(msg) {
super(msg);
this.code = "ERR_MODULE_NOT_FOUND";
}
}

const errors = {
ERR_MODULE_NOT_FOUND,
};

window.__bootstrap.compat = {
errors,
};
})(this);
1 change: 1 addition & 0 deletions runtime/js/90_deno_ns.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,5 +139,6 @@
flockSync: __bootstrap.fs.flockSync,
funlock: __bootstrap.fs.funlock,
funlockSync: __bootstrap.fs.funlockSync,
compat: __bootstrap.compat,
};
})(this);