diff options
author | Nayeem Rahman <nayeemrmn99@gmail.com> | 2022-04-13 15:41:39 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-13 16:41:39 +0200 |
commit | 7d50a5fd4342969d261017b7a6dd0d0528e99a77 (patch) | |
tree | b9aa868213c082e189c6529351e195dee3f8a8aa /core/error.rs | |
parent | c03fbb3c1f58868e079a9bf21f8351ad599ea7b5 (diff) |
refactor(core/error): Clarify JsError message fields (#14269)
Diffstat (limited to 'core/error.rs')
-rw-r--r-- | core/error.rs | 20 |
1 files changed, 13 insertions, 7 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( |