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;
|
2020-11-19 14:37:22 -05:00
|
|
|
use deno_core::error::{AnyError, JsError, JsStackFrame};
|
2021-09-18 09:40:04 -04:00
|
|
|
use deno_core::url::Url;
|
2019-07-10 18:53:48 -04:00
|
|
|
use std::error::Error;
|
2019-06-19 22:07:01 -04:00
|
|
|
use std::fmt;
|
2020-03-02 17:20:16 -05:00
|
|
|
use std::ops::Deref;
|
2019-06-19 22:07:01 -04:00
|
|
|
|
2020-03-24 23:53:48 -04:00
|
|
|
const SOURCE_ABBREV_THRESHOLD: usize = 150;
|
2021-09-18 09:40:04 -04:00
|
|
|
const DATA_URL_ABBREV_THRESHOLD: usize = 150;
|
2020-03-24 23:53:48 -04:00
|
|
|
|
2021-09-18 09:40:04 -04:00
|
|
|
pub fn format_file_name(file_name: &str) -> String {
|
|
|
|
if file_name.len() > DATA_URL_ABBREV_THRESHOLD {
|
|
|
|
if let Ok(url) = Url::parse(file_name) {
|
|
|
|
if url.scheme() == "data" {
|
|
|
|
let data_path = url.path();
|
|
|
|
if let Some(data_pieces) = data_path.split_once(',') {
|
|
|
|
let data_length = data_pieces.1.len();
|
|
|
|
if let Some(data_start) = data_pieces.1.get(0..20) {
|
|
|
|
if let Some(data_end) = data_pieces.1.get(data_length - 20..) {
|
|
|
|
return format!(
|
|
|
|
"{}:{},{}......{}",
|
|
|
|
url.scheme(),
|
|
|
|
data_pieces.0,
|
|
|
|
data_start,
|
|
|
|
data_end
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
file_name.to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2021-04-22 08:30:03 -04:00
|
|
|
result += &format!("{}{}", ":", yellow(&line_number.to_string()));
|
2020-09-22 13:01:30 -04:00
|
|
|
if let Some(column_number) = frame.column_number {
|
2021-04-22 08:30:03 -04:00
|
|
|
result += &format!("{}{}", ":", yellow(&column_number.to_string()));
|
2020-09-22 13:01:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2020-12-17 11:37:57 -05:00
|
|
|
// Keep in sync with `runtime/js/40_error_stack.js`.
|
2020-09-22 13:01:30 -04:00
|
|
|
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) {
|
|
|
|
formatted_method += &format!("{}.", type_name);
|
|
|
|
}
|
|
|
|
}
|
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) {
|
|
|
|
formatted_method += &format!(" [as {}]", method_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if let Some(type_name) = &frame.type_name {
|
|
|
|
formatted_method += &format!("{}.", type_name);
|
|
|
|
}
|
|
|
|
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 {
|
2021-04-22 08:30:03 -04:00
|
|
|
result += &italic_bold(&function_name).to_string();
|
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;
|
|
|
|
}
|
2021-04-22 08:30:03 -04:00
|
|
|
result += &format!(" ({})", format_location(frame));
|
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.
|
|
|
|
// Also short-circuit on error line too long.
|
|
|
|
if source_line.is_empty() || source_line.len() > SOURCE_ABBREV_THRESHOLD {
|
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-04-15 10:08:09 -04:00
|
|
|
crate::colors::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-04-16 10:12:26 -04:00
|
|
|
fn format_js_error(js_error: &JsError, is_child: bool) -> String {
|
|
|
|
let mut s = String::new();
|
|
|
|
s.push_str(&js_error.exception_message);
|
|
|
|
if let Some(aggregated) = &js_error.aggregated {
|
|
|
|
for aggregated_error in aggregated {
|
|
|
|
let error_string = format_js_error(aggregated_error, true);
|
|
|
|
for line in error_string.trim_start_matches("Uncaught ").lines() {
|
|
|
|
s.push_str(&format!("\n {}", line));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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(
|
|
|
|
if is_child {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
js_error.source_line.as_deref()
|
|
|
|
},
|
|
|
|
column_number,
|
|
|
|
true,
|
|
|
|
0,
|
|
|
|
));
|
|
|
|
for frame in &js_error.frames {
|
|
|
|
s.push_str(&format!("\n at {}", format_frame(frame)));
|
|
|
|
}
|
|
|
|
if let Some(cause) = &js_error.cause {
|
|
|
|
let error_string = format_js_error(cause, true);
|
|
|
|
s.push_str(&format!(
|
|
|
|
"\nCaused by: {}",
|
|
|
|
error_string.trim_start_matches("Uncaught ")
|
|
|
|
));
|
|
|
|
}
|
|
|
|
s
|
|
|
|
}
|
|
|
|
|
2020-11-19 14:37:22 -05:00
|
|
|
/// Wrapper around deno_core::JsError which provides colorful
|
|
|
|
/// string representation.
|
2019-07-10 18:53:48 -04:00
|
|
|
#[derive(Debug)]
|
2020-11-19 14:37:22 -05:00
|
|
|
pub struct PrettyJsError(JsError);
|
2019-07-10 18:53:48 -04:00
|
|
|
|
2020-11-19 14:37:22 -05:00
|
|
|
impl PrettyJsError {
|
|
|
|
pub fn create(js_error: JsError) -> AnyError {
|
|
|
|
let pretty_js_error = Self(js_error);
|
|
|
|
pretty_js_error.into()
|
2019-07-10 18:53:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-19 14:37:22 -05:00
|
|
|
impl Deref for PrettyJsError {
|
|
|
|
type Target = JsError;
|
2020-03-02 17:20:16 -05:00
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
2020-01-17 18:43:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-19 14:37:22 -05:00
|
|
|
impl fmt::Display for PrettyJsError {
|
2019-06-19 22:07:01 -04:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2022-04-16 10:12:26 -04:00
|
|
|
write!(f, "{}", &format_js_error(&self.0, false))
|
2019-06-19 22:07:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-19 14:37:22 -05:00
|
|
|
impl Error for PrettyJsError {}
|
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
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|