2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2019-06-19 22:07:01 -04:00
|
|
|
//! This mod provides DenoError to unify errors across Deno.
|
2021-04-22 08:30:03 -04:00
|
|
|
use crate::colors::cyan;
|
|
|
|
use crate::colors::italic_bold;
|
|
|
|
use crate::colors::red;
|
|
|
|
use crate::colors::yellow;
|
2022-05-03 13:45:57 -04:00
|
|
|
use deno_core::error::format_file_name;
|
|
|
|
use deno_core::error::JsError;
|
|
|
|
use deno_core::error::JsStackFrame;
|
2022-07-01 09:28:06 -04:00
|
|
|
use std::fmt::Write as _;
|
2019-06-19 22:07:01 -04:00
|
|
|
|
2022-10-26 09:37:45 -04:00
|
|
|
/// Compares all properties of JsError, except for JsError::cause.
|
|
|
|
/// This function is used to detect that 2 JsError objects in a JsError::cause
|
|
|
|
/// chain are identical, ie. there is a recursive cause.
|
|
|
|
/// 02_console.js, which also detects recursive causes, can use JS object
|
|
|
|
/// comparisons to compare errors. We don't have access to JS object identity in
|
|
|
|
/// format_js_error().
|
|
|
|
fn errors_are_equal_without_cause(a: &JsError, b: &JsError) -> bool {
|
|
|
|
a.name == b.name
|
|
|
|
&& a.message == b.message
|
|
|
|
&& a.stack == b.stack
|
|
|
|
// `a.cause == b.cause` omitted, because it is absent in recursive errors,
|
|
|
|
// despite the error being identical to a previously seen one.
|
|
|
|
&& a.exception_message == b.exception_message
|
|
|
|
&& a.frames == b.frames
|
|
|
|
&& a.source_line == b.source_line
|
|
|
|
&& a.source_line_frame_index == b.source_line_frame_index
|
|
|
|
&& a.aggregated == b.aggregated
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
struct ErrorReference<'a> {
|
|
|
|
from: &'a JsError,
|
|
|
|
to: &'a JsError,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
struct IndexedErrorReference<'a> {
|
|
|
|
reference: ErrorReference<'a>,
|
|
|
|
index: usize,
|
|
|
|
}
|
|
|
|
|
2021-09-18 09:40:04 -04:00
|
|
|
// Keep in sync with `/core/error.js`.
|
2020-09-22 13:01:30 -04:00
|
|
|
pub fn format_location(frame: &JsStackFrame) -> String {
|
2021-04-22 08:30:03 -04:00
|
|
|
let _internal = frame
|
2020-09-22 13:01:30 -04:00
|
|
|
.file_name
|
|
|
|
.as_ref()
|
|
|
|
.map_or(false, |f| f.starts_with("deno:"));
|
|
|
|
if frame.is_native {
|
2021-04-22 08:30:03 -04:00
|
|
|
return cyan("native").to_string();
|
2020-09-22 13:01:30 -04:00
|
|
|
}
|
|
|
|
let mut result = String::new();
|
2022-04-19 04:59:51 -04:00
|
|
|
let file_name = frame.file_name.clone().unwrap_or_default();
|
|
|
|
if !file_name.is_empty() {
|
|
|
|
result += &cyan(&format_file_name(&file_name)).to_string();
|
2020-09-22 13:01:30 -04:00
|
|
|
} else {
|
|
|
|
if frame.is_eval {
|
2021-04-22 08:30:03 -04:00
|
|
|
result +=
|
|
|
|
&(cyan(&frame.eval_origin.as_ref().unwrap()).to_string() + ", ");
|
2020-09-22 13:01:30 -04:00
|
|
|
}
|
2021-04-22 08:30:03 -04:00
|
|
|
result += &cyan("<anonymous>").to_string();
|
2020-09-22 13:01:30 -04:00
|
|
|
}
|
|
|
|
if let Some(line_number) = frame.line_number {
|
2022-07-01 09:28:06 -04:00
|
|
|
write!(result, ":{}", yellow(&line_number.to_string())).unwrap();
|
2020-09-22 13:01:30 -04:00
|
|
|
if let Some(column_number) = frame.column_number {
|
2022-07-01 09:28:06 -04:00
|
|
|
write!(result, ":{}", yellow(&column_number.to_string())).unwrap();
|
2020-09-22 13:01:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
fn format_frame(frame: &JsStackFrame) -> String {
|
2021-04-22 08:30:03 -04:00
|
|
|
let _internal = frame
|
2020-09-22 13:01:30 -04:00
|
|
|
.file_name
|
|
|
|
.as_ref()
|
|
|
|
.map_or(false, |f| f.starts_with("deno:"));
|
|
|
|
let is_method_call =
|
|
|
|
!(frame.is_top_level.unwrap_or_default() || frame.is_constructor);
|
|
|
|
let mut result = String::new();
|
|
|
|
if frame.is_async {
|
2021-04-22 08:30:03 -04:00
|
|
|
result += "async ";
|
2020-09-22 13:01:30 -04:00
|
|
|
}
|
|
|
|
if frame.is_promise_all {
|
2021-04-22 08:30:03 -04:00
|
|
|
result += &italic_bold(&format!(
|
|
|
|
"Promise.all (index {})",
|
2022-01-15 01:10:12 -05:00
|
|
|
frame.promise_index.unwrap_or_default()
|
2021-04-22 08:30:03 -04:00
|
|
|
))
|
|
|
|
.to_string();
|
2020-09-22 13:01:30 -04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
if is_method_call {
|
|
|
|
let mut formatted_method = String::new();
|
|
|
|
if let Some(function_name) = &frame.function_name {
|
|
|
|
if let Some(type_name) = &frame.type_name {
|
|
|
|
if !function_name.starts_with(type_name) {
|
2022-07-01 09:28:06 -04:00
|
|
|
write!(formatted_method, "{}.", type_name).unwrap();
|
2020-09-22 13:01:30 -04:00
|
|
|
}
|
|
|
|
}
|
2021-07-30 09:03:41 -04:00
|
|
|
formatted_method += function_name;
|
2020-09-22 13:01:30 -04:00
|
|
|
if let Some(method_name) = &frame.method_name {
|
|
|
|
if !function_name.ends_with(method_name) {
|
2022-07-01 09:28:06 -04:00
|
|
|
write!(formatted_method, " [as {}]", method_name).unwrap();
|
2020-09-22 13:01:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if let Some(type_name) = &frame.type_name {
|
2022-07-01 09:28:06 -04:00
|
|
|
write!(formatted_method, "{}.", type_name).unwrap();
|
2020-09-22 13:01:30 -04:00
|
|
|
}
|
|
|
|
if let Some(method_name) = &frame.method_name {
|
2021-07-30 09:03:41 -04:00
|
|
|
formatted_method += method_name
|
2020-09-22 13:01:30 -04:00
|
|
|
} else {
|
|
|
|
formatted_method += "<anonymous>";
|
|
|
|
}
|
|
|
|
}
|
2021-04-22 08:30:03 -04:00
|
|
|
result += &italic_bold(&formatted_method).to_string();
|
2020-09-22 13:01:30 -04:00
|
|
|
} else if frame.is_constructor {
|
2021-04-22 08:30:03 -04:00
|
|
|
result += "new ";
|
2020-09-22 13:01:30 -04:00
|
|
|
if let Some(function_name) = &frame.function_name {
|
2022-07-01 09:28:06 -04:00
|
|
|
write!(result, "{}", italic_bold(&function_name)).unwrap();
|
2020-09-22 13:01:30 -04:00
|
|
|
} else {
|
2021-04-22 08:30:03 -04:00
|
|
|
result += &cyan("<anonymous>").to_string();
|
2020-09-22 13:01:30 -04:00
|
|
|
}
|
|
|
|
} else if let Some(function_name) = &frame.function_name {
|
2021-04-22 08:30:03 -04:00
|
|
|
result += &italic_bold(&function_name).to_string();
|
2020-09-22 13:01:30 -04:00
|
|
|
} else {
|
|
|
|
result += &format_location(frame);
|
|
|
|
return result;
|
|
|
|
}
|
2022-07-01 09:28:06 -04:00
|
|
|
write!(result, " ({})", format_location(frame)).unwrap();
|
2020-09-22 13:01:30 -04:00
|
|
|
result
|
2020-06-08 08:06:20 -04:00
|
|
|
}
|
|
|
|
|
2019-06-19 22:07:01 -04:00
|
|
|
/// Take an optional source line and associated information to format it into
|
|
|
|
/// a pretty printed version of that line.
|
2020-04-20 15:39:02 -04:00
|
|
|
fn format_maybe_source_line(
|
2020-06-29 08:17:37 -04:00
|
|
|
source_line: Option<&str>,
|
2022-04-15 10:08:09 -04:00
|
|
|
column_number: Option<i64>,
|
2019-06-19 22:07:01 -04:00
|
|
|
is_error: bool,
|
|
|
|
level: usize,
|
|
|
|
) -> String {
|
2022-04-15 10:08:09 -04:00
|
|
|
if source_line.is_none() || column_number.is_none() {
|
2019-06-19 22:07:01 -04:00
|
|
|
return "".to_string();
|
|
|
|
}
|
|
|
|
|
2020-04-20 15:39:02 -04:00
|
|
|
let source_line = source_line.unwrap();
|
2019-06-19 22:07:01 -04:00
|
|
|
// sometimes source_line gets set with an empty string, which then outputs
|
2020-03-24 23:53:48 -04:00
|
|
|
// an empty source line when displayed, so need just short circuit here.
|
2022-06-20 08:42:20 -04:00
|
|
|
if source_line.is_empty() {
|
2019-06-19 22:07:01 -04:00
|
|
|
return "".to_string();
|
|
|
|
}
|
2021-10-18 12:05:36 -04:00
|
|
|
if source_line.contains("Couldn't format source line: ") {
|
|
|
|
return format!("\n{}", source_line);
|
|
|
|
}
|
2019-06-19 22:07:01 -04:00
|
|
|
|
|
|
|
let mut s = String::new();
|
2022-04-15 10:08:09 -04:00
|
|
|
let column_number = column_number.unwrap();
|
2021-10-18 12:05:36 -04:00
|
|
|
|
2022-04-15 10:08:09 -04:00
|
|
|
if column_number as usize > source_line.len() {
|
2021-10-18 12:05:36 -04:00
|
|
|
return format!(
|
|
|
|
"\n{} Couldn't format source line: Column {} is out of bounds (source may have changed at runtime)",
|
2022-07-14 17:52:44 -04:00
|
|
|
yellow("Warning"), column_number,
|
2021-10-18 12:05:36 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-04-15 10:08:09 -04:00
|
|
|
for _i in 0..(column_number - 1) {
|
2020-05-01 13:03:54 -04:00
|
|
|
if source_line.chars().nth(_i as usize).unwrap() == '\t' {
|
|
|
|
s.push('\t');
|
|
|
|
} else {
|
|
|
|
s.push(' ');
|
|
|
|
}
|
2020-04-13 10:54:16 -04:00
|
|
|
}
|
2022-04-15 10:08:09 -04:00
|
|
|
s.push('^');
|
2019-06-19 22:07:01 -04:00
|
|
|
let color_underline = if is_error {
|
2021-04-22 08:30:03 -04:00
|
|
|
red(&s).to_string()
|
2019-06-19 22:07:01 -04:00
|
|
|
} else {
|
2021-04-22 08:30:03 -04:00
|
|
|
cyan(&s).to_string()
|
2019-06-19 22:07:01 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
let indent = format!("{:indent$}", "", indent = level);
|
|
|
|
|
2020-04-20 15:39:02 -04:00
|
|
|
format!("\n{}{}\n{}{}", indent, source_line, indent, color_underline)
|
2019-06-19 22:07:01 -04:00
|
|
|
}
|
|
|
|
|
2022-10-26 09:37:45 -04:00
|
|
|
fn find_recursive_cause(js_error: &JsError) -> Option<ErrorReference> {
|
|
|
|
let mut history = Vec::<&JsError>::new();
|
|
|
|
|
|
|
|
let mut current_error: &JsError = js_error;
|
|
|
|
|
|
|
|
while let Some(cause) = ¤t_error.cause {
|
|
|
|
history.push(current_error);
|
|
|
|
|
|
|
|
if let Some(seen) = history
|
|
|
|
.iter()
|
|
|
|
.find(|&el| errors_are_equal_without_cause(el, cause.as_ref()))
|
|
|
|
{
|
|
|
|
return Some(ErrorReference {
|
|
|
|
from: current_error,
|
2022-11-17 20:59:10 -05:00
|
|
|
to: seen,
|
2022-10-26 09:37:45 -04:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
current_error = cause;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn format_aggregated_error(
|
|
|
|
aggregated_errors: &Vec<JsError>,
|
|
|
|
circular_reference_index: usize,
|
|
|
|
) -> String {
|
2022-04-16 10:12:26 -04:00
|
|
|
let mut s = String::new();
|
2022-10-26 09:37:45 -04:00
|
|
|
let mut nested_circular_reference_index = circular_reference_index;
|
|
|
|
|
|
|
|
for js_error in aggregated_errors {
|
|
|
|
let aggregated_circular = find_recursive_cause(js_error);
|
|
|
|
if aggregated_circular.is_some() {
|
|
|
|
nested_circular_reference_index += 1;
|
|
|
|
}
|
|
|
|
let error_string = format_js_error_inner(
|
|
|
|
js_error,
|
|
|
|
aggregated_circular.map(|reference| IndexedErrorReference {
|
|
|
|
reference,
|
|
|
|
index: nested_circular_reference_index,
|
|
|
|
}),
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
|
|
|
|
for line in error_string.trim_start_matches("Uncaught ").lines() {
|
|
|
|
write!(s, "\n {}", line).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s
|
|
|
|
}
|
|
|
|
|
|
|
|
fn format_js_error_inner(
|
|
|
|
js_error: &JsError,
|
|
|
|
circular: Option<IndexedErrorReference>,
|
|
|
|
include_source_code: bool,
|
|
|
|
) -> String {
|
|
|
|
let mut s = String::new();
|
|
|
|
|
2022-04-16 10:12:26 -04:00
|
|
|
s.push_str(&js_error.exception_message);
|
2022-10-26 09:37:45 -04:00
|
|
|
|
|
|
|
if let Some(circular) = &circular {
|
|
|
|
if errors_are_equal_without_cause(js_error, circular.reference.to) {
|
|
|
|
write!(s, " {}", cyan(format!("<ref *{}>", circular.index))).unwrap();
|
2022-04-16 10:12:26 -04:00
|
|
|
}
|
|
|
|
}
|
2022-10-26 09:37:45 -04:00
|
|
|
|
|
|
|
if let Some(aggregated) = &js_error.aggregated {
|
|
|
|
let aggregated_message = format_aggregated_error(
|
|
|
|
aggregated,
|
|
|
|
circular.as_ref().map_or(0, |circular| circular.index),
|
|
|
|
);
|
|
|
|
s.push_str(&aggregated_message);
|
|
|
|
}
|
|
|
|
|
2022-04-16 10:12:26 -04:00
|
|
|
let column_number = js_error
|
|
|
|
.source_line_frame_index
|
|
|
|
.and_then(|i| js_error.frames.get(i).unwrap().column_number);
|
|
|
|
s.push_str(&format_maybe_source_line(
|
2022-10-26 09:37:45 -04:00
|
|
|
if include_source_code {
|
2022-04-16 10:12:26 -04:00
|
|
|
js_error.source_line.as_deref()
|
2022-10-26 09:37:45 -04:00
|
|
|
} else {
|
|
|
|
None
|
2022-04-16 10:12:26 -04:00
|
|
|
},
|
|
|
|
column_number,
|
|
|
|
true,
|
|
|
|
0,
|
|
|
|
));
|
|
|
|
for frame in &js_error.frames {
|
2022-07-01 09:28:06 -04:00
|
|
|
write!(s, "\n at {}", format_frame(frame)).unwrap();
|
2022-04-16 10:12:26 -04:00
|
|
|
}
|
|
|
|
if let Some(cause) = &js_error.cause {
|
2022-10-26 09:37:45 -04:00
|
|
|
let is_caused_by_circular = circular.as_ref().map_or(false, |circular| {
|
|
|
|
errors_are_equal_without_cause(circular.reference.from, js_error)
|
|
|
|
});
|
|
|
|
|
|
|
|
let error_string = if is_caused_by_circular {
|
|
|
|
cyan(format!("[Circular *{}]", circular.unwrap().index)).to_string()
|
|
|
|
} else {
|
|
|
|
format_js_error_inner(cause, circular, false)
|
|
|
|
};
|
|
|
|
|
2022-07-01 09:28:06 -04:00
|
|
|
write!(
|
|
|
|
s,
|
2022-04-16 10:12:26 -04:00
|
|
|
"\nCaused by: {}",
|
|
|
|
error_string.trim_start_matches("Uncaught ")
|
2022-07-01 09:28:06 -04:00
|
|
|
)
|
|
|
|
.unwrap();
|
2022-04-16 10:12:26 -04:00
|
|
|
}
|
|
|
|
s
|
|
|
|
}
|
|
|
|
|
2022-09-02 16:53:23 -04:00
|
|
|
/// Format a [`JsError`] for terminal output.
|
2022-04-26 19:06:10 -04:00
|
|
|
pub fn format_js_error(js_error: &JsError) -> String {
|
2022-10-26 09:37:45 -04:00
|
|
|
let circular =
|
|
|
|
find_recursive_cause(js_error).map(|reference| IndexedErrorReference {
|
|
|
|
reference,
|
|
|
|
index: 1,
|
|
|
|
});
|
|
|
|
|
|
|
|
format_js_error_inner(js_error, circular, true)
|
2020-01-17 18:43:53 -05:00
|
|
|
}
|
|
|
|
|
2019-06-19 22:07:01 -04:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2021-09-12 12:04:17 -04:00
|
|
|
use test_util::strip_ansi_codes;
|
2019-06-19 22:07:01 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_none_source_line() {
|
2022-04-15 10:08:09 -04:00
|
|
|
let actual = format_maybe_source_line(None, None, false, 0);
|
2019-06-19 22:07:01 -04:00
|
|
|
assert_eq!(actual, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_some_source_line() {
|
2022-04-15 10:08:09 -04:00
|
|
|
let actual =
|
|
|
|
format_maybe_source_line(Some("console.log('foo');"), Some(9), true, 0);
|
2019-06-19 22:07:01 -04:00
|
|
|
assert_eq!(
|
|
|
|
strip_ansi_codes(&actual),
|
2022-04-15 10:08:09 -04:00
|
|
|
"\nconsole.log(\'foo\');\n ^"
|
2019-06-19 22:07:01 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|