feat: Better formatting for AggregateError (#14285)

This commit adds "aggregated" field to "deno_core::JsError" that stores
instances of "JsError" recursively to properly handle "AggregateError"
formatting. Appropriate logics was added to "PrettyJsError" and
"console" API to format AggregateErrors.

Co-authored-by: Nayeem Rahman <nayeemrmn99@gmail.com>
This commit is contained in:
Bartek Iwańczuk 2022-04-16 16:12:26 +02:00 committed by GitHub
parent 0bb96cde72
commit a87be28a46
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 210 additions and 78 deletions

View file

@ -104,6 +104,7 @@ pub struct JsError {
pub frames: Vec<JsStackFrame>,
pub source_line: Option<String>,
pub source_line_frame_index: Option<usize>,
pub aggregated: Option<Vec<JsError>>,
}
#[derive(Debug, PartialEq, Clone, serde::Deserialize, serde::Serialize)]
@ -305,6 +306,25 @@ impl JsError {
}
}
// Read an array of stored errors, this is only defined for `AggregateError`
let aggregated_errors = get_property(scope, exception, "errors");
let aggregated_errors: Option<v8::Local<v8::Array>> =
aggregated_errors.and_then(|a| a.try_into().ok());
let mut aggregated: Option<Vec<JsError>> = None;
if let Some(errors) = aggregated_errors {
if errors.length() > 0 {
let mut agg = vec![];
for i in 0..errors.length() {
let error = errors.get_index(scope, i).unwrap();
let js_error = Self::from_v8_exception(scope, error);
agg.push(js_error);
}
aggregated = Some(agg);
}
}
Self {
name: e.name,
message: e.message,
@ -314,6 +334,7 @@ impl JsError {
source_line_frame_index,
frames,
stack,
aggregated,
}
} else {
// The exception is not a JS Error object.
@ -328,6 +349,7 @@ impl JsError {
source_line_frame_index: None,
frames: vec![],
stack: None,
aggregated: None,
}
}
}