1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-21 21:50:00 -05:00

refactor(core/error): Clarify JsError message fields (#14269)

This commit is contained in:
Nayeem Rahman 2022-04-13 15:41:39 +01:00 committed by GitHub
parent c03fbb3c1f
commit 7d50a5fd43
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 27 additions and 17 deletions

View file

@ -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,

View file

@ -85,7 +85,9 @@ pub fn apply_source_map<G: SourceMapGetter>(
.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()),

View file

@ -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(

View file

@ -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);

View file

@ -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;

View file

@ -92,7 +92,7 @@ impl Serialize for WorkerControlEvent {
| WorkerControlEvent::Error(error) => {
let value = match error.downcast_ref::<JsError>() {
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,