From a87be28a46b67e53354f8ce69386057ddbb0f46c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Sat, 16 Apr 2022 16:12:26 +0200 Subject: 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 --- core/error.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'core/error.rs') diff --git a/core/error.rs b/core/error.rs index 59fe170c8..8014dceab 100644 --- a/core/error.rs +++ b/core/error.rs @@ -104,6 +104,7 @@ pub struct JsError { pub frames: Vec, pub source_line: Option, pub source_line_frame_index: Option, + pub aggregated: Option>, } #[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> = + aggregated_errors.and_then(|a| a.try_into().ok()); + + let mut aggregated: Option> = 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, } } } -- cgit v1.2.3