From 7d50a5fd4342969d261017b7a6dd0d0528e99a77 Mon Sep 17 00:00:00 2001 From: Nayeem Rahman Date: Wed, 13 Apr 2022 15:41:39 +0100 Subject: [PATCH] refactor(core/error): Clarify JsError message fields (#14269) --- cli/fmt_errors.rs | 2 +- cli/source_maps.rs | 6 +++++- core/error.rs | 20 +++++++++++++------- core/runtime.rs | 12 ++++++------ ext/web/02_event.js | 2 +- runtime/web_worker.rs | 2 +- 6 files changed, 27 insertions(+), 17 deletions(-) diff --git a/cli/fmt_errors.rs b/cli/fmt_errors.rs index b6e44ea075..106ecaaf21 100644 --- a/cli/fmt_errors.rs +++ b/cli/fmt_errors.rs @@ -283,7 +283,7 @@ impl fmt::Display for PrettyJsError { "{}", &format_stack( true, - &self.0.message, + &self.0.exception_message, cause.as_deref(), self.0.source_line.as_deref(), self.0.start_column, diff --git a/cli/source_maps.rs b/cli/source_maps.rs index 80d97f1b93..d92caf7f4a 100644 --- a/cli/source_maps.rs +++ b/cli/source_maps.rs @@ -85,7 +85,9 @@ pub fn apply_source_map( .map(|cause| Box::new(apply_source_map(&*cause, getter))); JsError { + name: js_error.name.clone(), message: js_error.message.clone(), + exception_message: js_error.exception_message.clone(), cause, source_line, script_resource_name, @@ -243,7 +245,9 @@ mod tests { #[test] fn apply_source_map_line() { let e = JsError { - message: "TypeError: baz".to_string(), + name: Some("TypeError".to_string()), + message: Some("baz".to_string()), + exception_message: "TypeError: baz".to_string(), cause: None, source_line: Some("foo".to_string()), script_resource_name: Some("foo_bar.ts".to_string()), diff --git a/core/error.rs b/core/error.rs index 1fc4a1af77..362bf2fa5b 100644 --- a/core/error.rs +++ b/core/error.rs @@ -93,7 +93,9 @@ pub fn get_custom_error_class(error: &Error) -> Option<&'static str> { #[derive(Debug, PartialEq, Clone, serde::Deserialize, serde::Serialize)] #[serde(rename_all = "camelCase")] pub struct JsError { - pub message: String, + pub name: Option, + pub message: Option, + pub exception_message: String, pub cause: Option>, pub source_line: Option, pub script_resource_name: Option, @@ -198,9 +200,9 @@ impl JsError { let e: NativeJsError = serde_v8::from_v8(scope, exception.into()).unwrap(); // Get the message by formatting error.name and error.message. - let name = e.name.unwrap_or_else(|| "Error".to_string()); - let message_prop = e.message.unwrap_or_else(|| "".to_string()); - let message = if !name.is_empty() && !message_prop.is_empty() { + let name = e.name.clone().unwrap_or_else(|| "Error".to_string()); + let message_prop = e.message.clone().unwrap_or_else(|| "".to_string()); + let exception_message = if !name.is_empty() && !message_prop.is_empty() { format!("Uncaught {}: {}", name, message_prop) } else if !name.is_empty() { format!("Uncaught {}", name) @@ -239,7 +241,9 @@ impl JsError { None => vec![], }; Self { - message, + name: e.name, + message: e.message, + exception_message, cause, script_resource_name: msg .get_script_resource_name(scope) @@ -259,7 +263,9 @@ impl JsError { // Get the message given by V8::Exception::create_message(), and provide // empty frames. Self { - message: msg.get(scope).to_rust_string_lossy(scope), + name: None, + message: None, + exception_message: msg.get(scope).to_rust_string_lossy(scope), cause: None, script_resource_name: None, source_line: None, @@ -294,7 +300,7 @@ impl Display for JsError { } } - write!(f, "{}", self.message)?; + write!(f, "{}", self.exception_message)?; if let Some(script_resource_name) = &self.script_resource_name { if self.line_number.is_some() && self.start_column.is_some() { let source_loc = format_source_loc( diff --git a/core/runtime.rs b/core/runtime.rs index 7f7b6ead1f..a6442ce976 100644 --- a/core/runtime.rs +++ b/core/runtime.rs @@ -1053,9 +1053,9 @@ pub(crate) fn exception_to_err_result<'s, T>( let mut js_error = JsError::from_v8_exception(scope, exception); if in_promise { - js_error.message = format!( + js_error.exception_message = format!( "Uncaught (in promise) {}", - js_error.message.trim_start_matches("Uncaught ") + js_error.exception_message.trim_start_matches("Uncaught ") ); } let js_error = (state.js_error_create_fn)(js_error); @@ -2044,7 +2044,7 @@ pub mod tests { .unwrap(); let v = runtime.poll_value(&value_global, cx); assert!( - matches!(v, Poll::Ready(Err(e)) if e.downcast_ref::().unwrap().message == "Uncaught Error: fail") + matches!(v, Poll::Ready(Err(e)) if e.downcast_ref::().unwrap().exception_message == "Uncaught Error: fail") ); let value_global = runtime @@ -2087,7 +2087,7 @@ pub mod tests { let err = runtime.resolve_value(value_global).await.unwrap_err(); assert_eq!( "Uncaught Error: fail", - err.downcast::().unwrap().message + err.downcast::().unwrap().exception_message ); let value_global = runtime @@ -2377,7 +2377,7 @@ pub mod tests { .expect_err("script should fail"); assert_eq!( "Uncaught Error: execution terminated", - err.downcast::().unwrap().message + err.downcast::().unwrap().exception_message ); assert!(callback_invoke_count.load(Ordering::SeqCst) > 0) } @@ -2430,7 +2430,7 @@ pub mod tests { .expect_err("script should fail"); assert_eq!( "Uncaught Error: execution terminated", - err.downcast::().unwrap().message + err.downcast::().unwrap().exception_message ); assert_eq!(0, callback_invoke_count_first.load(Ordering::SeqCst)); assert!(callback_invoke_count_second.load(Ordering::SeqCst) > 0); diff --git a/ext/web/02_event.js b/ext/web/02_event.js index f1078b4ac2..d8b595c547 100644 --- a/ext/web/02_event.js +++ b/ext/web/02_event.js @@ -1329,7 +1329,7 @@ function reportException(error) { reportExceptionStackedCalls++; const jsError = core.destructureError(error); - const message = jsError.message; + const message = jsError.exceptionMessage; let filename = ""; let lineno = 0; let colno = 0; diff --git a/runtime/web_worker.rs b/runtime/web_worker.rs index 014b3e738b..5d416b9353 100644 --- a/runtime/web_worker.rs +++ b/runtime/web_worker.rs @@ -92,7 +92,7 @@ impl Serialize for WorkerControlEvent { | WorkerControlEvent::Error(error) => { let value = match error.downcast_ref::() { Some(js_error) => json!({ - "message": js_error.message, + "message": js_error.exception_message, "fileName": js_error.script_resource_name, "lineNumber": js_error.line_number, "columnNumber": js_error.start_column,