2024-01-01 19:58:21 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2019-06-20 02:07:01 +00:00
|
|
|
//! This mod provides DenoError to unify errors across Deno.
|
2024-07-26 07:08:15 +00:00
|
|
|
use deno_core::error::format_frame;
|
2022-05-03 17:45:57 +00:00
|
|
|
use deno_core::error::JsError;
|
2024-02-07 16:25:14 +00:00
|
|
|
use deno_terminal::colors::cyan;
|
|
|
|
use deno_terminal::colors::italic_bold;
|
|
|
|
use deno_terminal::colors::red;
|
|
|
|
use deno_terminal::colors::yellow;
|
2022-07-01 13:28:06 +00:00
|
|
|
use std::fmt::Write as _;
|
2019-06-20 02:07:01 +00:00
|
|
|
|
2022-10-26 13:37:45 +00:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
struct ErrorReference<'a> {
|
|
|
|
from: &'a JsError,
|
|
|
|
to: &'a JsError,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
struct IndexedErrorReference<'a> {
|
|
|
|
reference: ErrorReference<'a>,
|
|
|
|
index: usize,
|
|
|
|
}
|
|
|
|
|
2024-09-05 12:49:07 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
enum FixSuggestionKind {
|
|
|
|
Info,
|
|
|
|
Hint,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct FixSuggestion<'a> {
|
|
|
|
kind: FixSuggestionKind,
|
|
|
|
message: &'a str,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> FixSuggestion<'a> {
|
|
|
|
pub fn info(message: &'a str) -> Self {
|
|
|
|
Self {
|
|
|
|
kind: FixSuggestionKind::Info,
|
|
|
|
message,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn hint(message: &'a str) -> Self {
|
|
|
|
Self {
|
|
|
|
kind: FixSuggestionKind::Hint,
|
|
|
|
message,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-26 07:08:15 +00:00
|
|
|
struct AnsiColors;
|
|
|
|
|
|
|
|
impl deno_core::error::ErrorFormat for AnsiColors {
|
|
|
|
fn fmt_element(
|
|
|
|
element: deno_core::error::ErrorElement,
|
|
|
|
s: &str,
|
|
|
|
) -> std::borrow::Cow<'_, str> {
|
|
|
|
use deno_core::error::ErrorElement::*;
|
|
|
|
match element {
|
|
|
|
Anonymous | NativeFrame | FileName | EvalOrigin => {
|
|
|
|
cyan(s).to_string().into()
|
2020-09-22 17:01:30 +00:00
|
|
|
}
|
2024-07-26 07:08:15 +00:00
|
|
|
LineNumber | ColumnNumber => yellow(s).to_string().into(),
|
|
|
|
FunctionName | PromiseAll => italic_bold(s).to_string().into(),
|
2020-09-22 17:01:30 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-08 12:06:20 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 02:07:01 +00:00
|
|
|
/// Take an optional source line and associated information to format it into
|
|
|
|
/// a pretty printed version of that line.
|
2020-04-20 19:39:02 +00:00
|
|
|
fn format_maybe_source_line(
|
2020-06-29 12:17:37 +00:00
|
|
|
source_line: Option<&str>,
|
2022-04-15 14:08:09 +00:00
|
|
|
column_number: Option<i64>,
|
2019-06-20 02:07:01 +00:00
|
|
|
is_error: bool,
|
|
|
|
level: usize,
|
|
|
|
) -> String {
|
2022-04-15 14:08:09 +00:00
|
|
|
if source_line.is_none() || column_number.is_none() {
|
2019-06-20 02:07:01 +00:00
|
|
|
return "".to_string();
|
|
|
|
}
|
|
|
|
|
2020-04-20 19:39:02 +00:00
|
|
|
let source_line = source_line.unwrap();
|
2019-06-20 02:07:01 +00:00
|
|
|
// sometimes source_line gets set with an empty string, which then outputs
|
2020-03-25 03:53:48 +00:00
|
|
|
// an empty source line when displayed, so need just short circuit here.
|
2022-06-20 12:42:20 +00:00
|
|
|
if source_line.is_empty() {
|
2019-06-20 02:07:01 +00:00
|
|
|
return "".to_string();
|
|
|
|
}
|
2021-10-18 16:05:36 +00:00
|
|
|
if source_line.contains("Couldn't format source line: ") {
|
2023-01-27 15:43:16 +00:00
|
|
|
return format!("\n{source_line}");
|
2021-10-18 16:05:36 +00:00
|
|
|
}
|
2019-06-20 02:07:01 +00:00
|
|
|
|
|
|
|
let mut s = String::new();
|
2022-04-15 14:08:09 +00:00
|
|
|
let column_number = column_number.unwrap();
|
2021-10-18 16:05:36 +00:00
|
|
|
|
2022-04-15 14:08:09 +00:00
|
|
|
if column_number as usize > source_line.len() {
|
2021-10-18 16:05:36 +00:00
|
|
|
return format!(
|
|
|
|
"\n{} Couldn't format source line: Column {} is out of bounds (source may have changed at runtime)",
|
2022-07-14 21:52:44 +00:00
|
|
|
yellow("Warning"), column_number,
|
2021-10-18 16:05:36 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-04-15 14:08:09 +00:00
|
|
|
for _i in 0..(column_number - 1) {
|
2020-05-01 17:03:54 +00:00
|
|
|
if source_line.chars().nth(_i as usize).unwrap() == '\t' {
|
|
|
|
s.push('\t');
|
|
|
|
} else {
|
|
|
|
s.push(' ');
|
|
|
|
}
|
2020-04-13 14:54:16 +00:00
|
|
|
}
|
2022-04-15 14:08:09 +00:00
|
|
|
s.push('^');
|
2019-06-20 02:07:01 +00:00
|
|
|
let color_underline = if is_error {
|
2021-04-22 12:30:03 +00:00
|
|
|
red(&s).to_string()
|
2019-06-20 02:07:01 +00:00
|
|
|
} else {
|
2021-04-22 12:30:03 +00:00
|
|
|
cyan(&s).to_string()
|
2019-06-20 02:07:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let indent = format!("{:indent$}", "", indent = level);
|
|
|
|
|
2023-01-27 15:43:16 +00:00
|
|
|
format!("\n{indent}{source_line}\n{indent}{color_underline}")
|
2019-06-20 02:07:01 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 13:37:45 +00: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);
|
|
|
|
|
2023-12-07 00:02:52 +00:00
|
|
|
if let Some(seen) = history.iter().find(|&el| cause.is_same_error(el)) {
|
2022-10-26 13:37:45 +00:00
|
|
|
return Some(ErrorReference {
|
|
|
|
from: current_error,
|
2022-11-18 01:59:10 +00:00
|
|
|
to: seen,
|
2022-10-26 13:37:45 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
current_error = cause;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn format_aggregated_error(
|
|
|
|
aggregated_errors: &Vec<JsError>,
|
|
|
|
circular_reference_index: usize,
|
|
|
|
) -> String {
|
2022-04-16 14:12:26 +00:00
|
|
|
let mut s = String::new();
|
2022-10-26 13:37:45 +00: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,
|
2024-09-05 12:49:07 +00:00
|
|
|
vec![],
|
2022-10-26 13:37:45 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
for line in error_string.trim_start_matches("Uncaught ").lines() {
|
2023-01-27 15:43:16 +00:00
|
|
|
write!(s, "\n {line}").unwrap();
|
2022-10-26 13:37:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s
|
|
|
|
}
|
|
|
|
|
|
|
|
fn format_js_error_inner(
|
|
|
|
js_error: &JsError,
|
|
|
|
circular: Option<IndexedErrorReference>,
|
|
|
|
include_source_code: bool,
|
2024-09-05 12:49:07 +00:00
|
|
|
suggestions: Vec<FixSuggestion>,
|
2022-10-26 13:37:45 +00:00
|
|
|
) -> String {
|
|
|
|
let mut s = String::new();
|
|
|
|
|
2022-04-16 14:12:26 +00:00
|
|
|
s.push_str(&js_error.exception_message);
|
2022-10-26 13:37:45 +00:00
|
|
|
|
|
|
|
if let Some(circular) = &circular {
|
2023-12-07 00:02:52 +00:00
|
|
|
if js_error.is_same_error(circular.reference.to) {
|
2022-10-26 13:37:45 +00:00
|
|
|
write!(s, " {}", cyan(format!("<ref *{}>", circular.index))).unwrap();
|
2022-04-16 14:12:26 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-26 13:37:45 +00:00
|
|
|
|
|
|
|
if let Some(aggregated) = &js_error.aggregated {
|
|
|
|
let aggregated_message = format_aggregated_error(
|
|
|
|
aggregated,
|
2023-03-15 21:46:36 +00:00
|
|
|
circular
|
|
|
|
.as_ref()
|
|
|
|
.map(|circular| circular.index)
|
|
|
|
.unwrap_or(0),
|
2022-10-26 13:37:45 +00:00
|
|
|
);
|
|
|
|
s.push_str(&aggregated_message);
|
|
|
|
}
|
|
|
|
|
2022-04-16 14:12:26 +00: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 13:37:45 +00:00
|
|
|
if include_source_code {
|
2022-04-16 14:12:26 +00:00
|
|
|
js_error.source_line.as_deref()
|
2022-10-26 13:37:45 +00:00
|
|
|
} else {
|
|
|
|
None
|
2022-04-16 14:12:26 +00:00
|
|
|
},
|
|
|
|
column_number,
|
|
|
|
true,
|
|
|
|
0,
|
|
|
|
));
|
|
|
|
for frame in &js_error.frames {
|
2024-07-26 07:08:15 +00:00
|
|
|
write!(s, "\n at {}", format_frame::<AnsiColors>(frame)).unwrap();
|
2022-04-16 14:12:26 +00:00
|
|
|
}
|
|
|
|
if let Some(cause) = &js_error.cause {
|
2023-03-15 21:46:36 +00:00
|
|
|
let is_caused_by_circular = circular
|
|
|
|
.as_ref()
|
2023-12-07 00:02:52 +00:00
|
|
|
.map(|circular| js_error.is_same_error(circular.reference.from))
|
2023-03-15 21:46:36 +00:00
|
|
|
.unwrap_or(false);
|
2022-10-26 13:37:45 +00:00
|
|
|
|
|
|
|
let error_string = if is_caused_by_circular {
|
|
|
|
cyan(format!("[Circular *{}]", circular.unwrap().index)).to_string()
|
|
|
|
} else {
|
2024-09-05 12:49:07 +00:00
|
|
|
format_js_error_inner(cause, circular, false, vec![])
|
2022-10-26 13:37:45 +00:00
|
|
|
};
|
|
|
|
|
2022-07-01 13:28:06 +00:00
|
|
|
write!(
|
|
|
|
s,
|
2022-04-16 14:12:26 +00:00
|
|
|
"\nCaused by: {}",
|
|
|
|
error_string.trim_start_matches("Uncaught ")
|
2022-07-01 13:28:06 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2022-04-16 14:12:26 +00:00
|
|
|
}
|
2024-09-05 12:49:07 +00:00
|
|
|
if !suggestions.is_empty() {
|
|
|
|
write!(s, "\n\n").unwrap();
|
|
|
|
for (index, suggestion) in suggestions.iter().enumerate() {
|
|
|
|
write!(s, " ").unwrap();
|
|
|
|
match suggestion.kind {
|
|
|
|
FixSuggestionKind::Hint => write!(s, "{} ", cyan("hint:")).unwrap(),
|
|
|
|
FixSuggestionKind::Info => write!(s, "{} ", yellow("info:")).unwrap(),
|
|
|
|
};
|
|
|
|
write!(s, "{}", suggestion.message).unwrap();
|
|
|
|
if index != (suggestions.len() - 1) {
|
|
|
|
writeln!(s).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-16 14:12:26 +00:00
|
|
|
s
|
|
|
|
}
|
|
|
|
|
2022-09-02 20:53:23 +00:00
|
|
|
/// Format a [`JsError`] for terminal output.
|
2022-04-26 23:06:10 +00:00
|
|
|
pub fn format_js_error(js_error: &JsError) -> String {
|
2022-10-26 13:37:45 +00:00
|
|
|
let circular =
|
|
|
|
find_recursive_cause(js_error).map(|reference| IndexedErrorReference {
|
|
|
|
reference,
|
|
|
|
index: 1,
|
|
|
|
});
|
|
|
|
|
2024-09-05 12:49:07 +00:00
|
|
|
format_js_error_inner(js_error, circular, true, vec![])
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Format a [`JsError`] for terminal output, printing additional suggestions.
|
|
|
|
pub fn format_js_error_with_suggestions(
|
|
|
|
js_error: &JsError,
|
|
|
|
suggestions: Vec<FixSuggestion>,
|
|
|
|
) -> String {
|
|
|
|
let circular =
|
|
|
|
find_recursive_cause(js_error).map(|reference| IndexedErrorReference {
|
|
|
|
reference,
|
|
|
|
index: 1,
|
|
|
|
});
|
|
|
|
|
|
|
|
format_js_error_inner(js_error, circular, true, suggestions)
|
2020-01-17 23:43:53 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 02:07:01 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2021-09-12 16:04:17 +00:00
|
|
|
use test_util::strip_ansi_codes;
|
2019-06-20 02:07:01 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_none_source_line() {
|
2022-04-15 14:08:09 +00:00
|
|
|
let actual = format_maybe_source_line(None, None, false, 0);
|
2019-06-20 02:07:01 +00:00
|
|
|
assert_eq!(actual, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_some_source_line() {
|
2022-04-15 14:08:09 +00:00
|
|
|
let actual =
|
|
|
|
format_maybe_source_line(Some("console.log('foo');"), Some(9), true, 0);
|
2019-06-20 02:07:01 +00:00
|
|
|
assert_eq!(
|
|
|
|
strip_ansi_codes(&actual),
|
2022-04-15 14:08:09 +00:00
|
|
|
"\nconsole.log(\'foo\');\n ^"
|
2019-06-20 02:07:01 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|