diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/error.rs | 20 | ||||
-rw-r--r-- | core/runtime.rs | 12 |
2 files changed, 19 insertions, 13 deletions
diff --git a/core/error.rs b/core/error.rs index 1fc4a1af7..362bf2fa5 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<String>, + pub message: Option<String>, + pub exception_message: String, pub cause: Option<Box<JsError>>, pub source_line: Option<String>, pub script_resource_name: Option<String>, @@ -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 7f7b6ead1..a6442ce97 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::<JsError>().unwrap().message == "Uncaught Error: fail") + matches!(v, Poll::Ready(Err(e)) if e.downcast_ref::<JsError>().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::<JsError>().unwrap().message + err.downcast::<JsError>().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::<JsError>().unwrap().message + err.downcast::<JsError>().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::<JsError>().unwrap().message + err.downcast::<JsError>().unwrap().exception_message ); assert_eq!(0, callback_invoke_count_first.load(Ordering::SeqCst)); assert!(callback_invoke_count_second.load(Ordering::SeqCst) > 0); |