diff options
Diffstat (limited to 'core/error.rs')
-rw-r--r-- | core/error.rs | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/core/error.rs b/core/error.rs index 664cb5c0b..bc8a3195b 100644 --- a/core/error.rs +++ b/core/error.rs @@ -1,5 +1,7 @@ // Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. +use crate::runtime::GetErrorClassFn; +use crate::runtime::JsRealm; use crate::runtime::JsRuntime; use crate::source_map::apply_source_map; use crate::source_map::get_source_line; @@ -91,6 +93,30 @@ pub fn get_custom_error_class(error: &Error) -> Option<&'static str> { error.downcast_ref::<CustomError>().map(|e| e.class) } +pub fn to_v8_error<'a>( + scope: &mut v8::HandleScope<'a>, + get_class: GetErrorClassFn, + error: &Error, +) -> v8::Local<'a, v8::Value> { + let cb = JsRealm::state_from_scope(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, &error.to_string()).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()); + } + 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 /// line numbers. The deno_cli crate defines another `JsError` type, which wraps /// the one defined here, that adds source map support and colorful formatting. |