summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2022-12-13 22:59:21 +0100
committerGitHub <noreply@github.com>2022-12-13 22:59:21 +0100
commit3551955b6355a8ef677962b6c15adfac349ac01d (patch)
tree5f8714391ba8910def403b0fd9f44995e078181e
parent878590b77369cf3390b8107b08285bafb29ef2ec (diff)
refactor(core): add more information when unable to build error (#17027)
This should help debug problem in https://github.com/denoland/deno/issues/16963
-rw-r--r--core/01_core.js9
-rw-r--r--core/error.rs32
2 files changed, 30 insertions, 11 deletions
diff --git a/core/01_core.js b/core/01_core.js
index 02150674e..2560f979d 100644
--- a/core/01_core.js
+++ b/core/01_core.js
@@ -129,7 +129,14 @@
}
function buildCustomError(className, message, code) {
- const error = errorMap[className]?.(message);
+ let error;
+ try {
+ error = errorMap[className]?.(message);
+ } catch (e) {
+ throw new Error(
+ `Unsable to build custom error for "${className}"\n ${e.message}`,
+ );
+ }
// Strip buildCustomError() calls from stack trace
if (typeof error == "object") {
ErrorCaptureStackTrace(error, buildCustomError);
diff --git a/core/error.rs b/core/error.rs
index fb9bdf8e4..2ee97c0ac 100644
--- a/core/error.rs
+++ b/core/error.rs
@@ -97,23 +97,35 @@ pub fn to_v8_error<'a>(
get_class: GetErrorClassFn,
error: &Error,
) -> v8::Local<'a, v8::Value> {
- let cb = JsRuntime::state(scope)
+ let tc_scope = &mut v8::TryCatch::new(scope);
+ let cb = JsRuntime::state(tc_scope)
.borrow()
.js_build_custom_error_cb
.clone()
.expect("Custom error builder must be set");
- let cb = cb.open(scope);
- let this = v8::undefined(scope).into();
- let class = v8::String::new(scope, get_class(error)).unwrap();
- let message = v8::String::new(scope, &format!("{:#}", error)).unwrap();
+ let cb = cb.open(tc_scope);
+ let this = v8::undefined(tc_scope).into();
+ let class = v8::String::new(tc_scope, get_class(error)).unwrap();
+ let message = v8::String::new(tc_scope, &format!("{:#}", error)).unwrap();
let mut args = vec![class.into(), message.into()];
if let Some(code) = crate::error_codes::get_error_code(error) {
- args.push(v8::String::new(scope, code).unwrap().into());
+ args.push(v8::String::new(tc_scope, code).unwrap().into());
+ }
+ let maybe_exception = cb.call(tc_scope, this, &args);
+
+ match maybe_exception {
+ Some(exception) => exception,
+ None => {
+ let mut msg =
+ "Custom error class must have a builder registered".to_string();
+ if tc_scope.has_caught() {
+ let e = tc_scope.exception().unwrap();
+ let js_error = JsError::from_v8_exception(tc_scope, e);
+ msg = format!("{}: {}", msg, js_error.exception_message);
+ }
+ panic!("{}", msg);
+ }
}
- let exception = cb
- .call(scope, this, &args)
- .expect("Custom error class must have a builder registered");
- exception
}
/// A `JsError` represents an exception coming from V8, with stack frames and