1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 15:24:46 -05:00

refactor(corr): accept get_error_class_fn in RuntimeOptions (#8444)

This commit adds "get_error_class_fn" field to "RuntimeOptions"
struct in order to unify configuration of "JsRuntime".
This commit is contained in:
Bartek Iwańczuk 2020-11-21 15:56:14 +01:00 committed by GitHub
parent cf7949db2f
commit 04f4201f30
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 11 deletions

View file

@ -118,21 +118,19 @@ impl Worker {
) -> Self { ) -> Self {
let global_state_ = program_state.clone(); let global_state_ = program_state.clone();
let js_error_create_fn = Box::new(move |core_js_error| {
let source_mapped_error =
apply_source_map(&core_js_error, global_state_.clone());
PrettyJsError::create(source_mapped_error)
});
let mut js_runtime = JsRuntime::new(RuntimeOptions { let mut js_runtime = JsRuntime::new(RuntimeOptions {
module_loader: Some(module_loader), module_loader: Some(module_loader),
startup_snapshot: Some(startup_snapshot), startup_snapshot: Some(startup_snapshot),
js_error_create_fn: Some(Box::new(move |core_js_error| { js_error_create_fn: Some(js_error_create_fn),
let source_mapped_error = get_error_class_fn: Some(&crate::errors::get_error_class_name),
apply_source_map(&core_js_error, global_state_.clone());
PrettyJsError::create(source_mapped_error)
})),
..Default::default() ..Default::default()
}); });
{
let op_state = js_runtime.op_state();
let mut op_state = op_state.borrow_mut();
op_state.get_error_class_fn = &crate::errors::get_error_class_name;
}
let inspector = let inspector =
if let Some(inspector_server) = &program_state.maybe_inspector_server { if let Some(inspector_server) = &program_state.maybe_inspector_server {

View file

@ -170,6 +170,10 @@ pub struct RuntimeOptions {
/// is set to `JsError::create()`. /// is set to `JsError::create()`.
pub js_error_create_fn: Option<Box<JsErrorCreateFn>>, pub js_error_create_fn: Option<Box<JsErrorCreateFn>>,
/// Allows to map error type to a string "class" used to represent
/// error in JavaScript.
pub get_error_class_fn: Option<GetErrorClassFn>,
/// Implementation of `ModuleLoader` which will be /// Implementation of `ModuleLoader` which will be
/// called when V8 requests to load ES modules. /// called when V8 requests to load ES modules.
/// ///
@ -254,7 +258,11 @@ impl JsRuntime {
let js_error_create_fn = options let js_error_create_fn = options
.js_error_create_fn .js_error_create_fn
.unwrap_or_else(|| Box::new(JsError::create)); .unwrap_or_else(|| Box::new(JsError::create));
let op_state = OpState::default(); let mut op_state = OpState::default();
if let Some(get_error_class_fn) = options.get_error_class_fn {
op_state.get_error_class_fn = get_error_class_fn;
}
isolate.set_slot(Rc::new(RefCell::new(JsRuntimeState { isolate.set_slot(Rc::new(RefCell::new(JsRuntimeState {
global_context: Some(global_context), global_context: Some(global_context),