diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2022-12-13 22:59:21 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-13 22:59:21 +0100 |
commit | 3551955b6355a8ef677962b6c15adfac349ac01d (patch) | |
tree | 5f8714391ba8910def403b0fd9f44995e078181e /core/error.rs | |
parent | 878590b77369cf3390b8107b08285bafb29ef2ec (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
Diffstat (limited to 'core/error.rs')
-rw-r--r-- | core/error.rs | 32 |
1 files changed, 22 insertions, 10 deletions
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 |