2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 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.
|
2019-09-15 14:48:25 -04:00
|
|
|
use crate::colors;
|
2019-07-10 18:53:48 -04:00
|
|
|
use crate::source_maps::apply_source_map;
|
|
|
|
use crate::source_maps::SourceMapGetter;
|
2020-01-05 11:56:18 -05:00
|
|
|
use deno_core::ErrBox;
|
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;
|
|
|
|
|
2020-06-29 08:17:37 -04:00
|
|
|
pub fn format_location(filename: &str, line: i64, col: i64) -> String {
|
2020-06-08 08:06:20 -04:00
|
|
|
format!(
|
|
|
|
"{}:{}:{}",
|
|
|
|
colors::cyan(filename),
|
2020-06-29 08:17:37 -04:00
|
|
|
colors::yellow(&line.to_string()),
|
|
|
|
colors::yellow(&col.to_string())
|
2020-06-08 08:06:20 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-04-20 15:39:02 -04:00
|
|
|
pub fn format_stack(
|
|
|
|
is_error: bool,
|
2020-06-29 08:17:37 -04:00
|
|
|
message_line: &str,
|
|
|
|
source_line: Option<&str>,
|
2020-04-20 15:39:02 -04:00
|
|
|
start_column: Option<i64>,
|
|
|
|
end_column: Option<i64>,
|
|
|
|
formatted_frames: &[String],
|
|
|
|
level: usize,
|
2019-06-19 22:07:01 -04:00
|
|
|
) -> String {
|
2020-04-20 15:39:02 -04:00
|
|
|
let mut s = String::new();
|
|
|
|
s.push_str(&format!("{:indent$}{}", "", message_line, indent = level));
|
|
|
|
s.push_str(&format_maybe_source_line(
|
|
|
|
source_line,
|
|
|
|
start_column,
|
|
|
|
end_column,
|
|
|
|
is_error,
|
|
|
|
level,
|
|
|
|
));
|
|
|
|
for formatted_frame in formatted_frames {
|
|
|
|
s.push_str(&format!(
|
|
|
|
"\n{:indent$} at {}",
|
|
|
|
"",
|
|
|
|
formatted_frame,
|
|
|
|
indent = level
|
|
|
|
));
|
|
|
|
}
|
|
|
|
s
|
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>,
|
2019-06-19 22:07:01 -04:00
|
|
|
start_column: Option<i64>,
|
|
|
|
end_column: Option<i64>,
|
|
|
|
is_error: bool,
|
|
|
|
level: usize,
|
|
|
|
) -> String {
|
2020-04-20 15:39:02 -04:00
|
|
|
if source_line.is_none() || start_column.is_none() || end_column.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();
|
|
|
|
}
|
|
|
|
|
|
|
|
assert!(start_column.is_some());
|
|
|
|
assert!(end_column.is_some());
|
|
|
|
let mut s = String::new();
|
|
|
|
let start_column = start_column.unwrap();
|
|
|
|
let end_column = end_column.unwrap();
|
|
|
|
// TypeScript uses `~` always, but V8 would utilise `^` always, even when
|
|
|
|
// doing ranges, so here, if we only have one marker (very common with V8
|
|
|
|
// errors) we will use `^` instead.
|
|
|
|
let underline_char = if (end_column - start_column) <= 1 {
|
|
|
|
'^'
|
|
|
|
} else {
|
|
|
|
'~'
|
|
|
|
};
|
2020-04-13 10:54:16 -04:00
|
|
|
for _i in 0..start_column {
|
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
|
|
|
}
|
|
|
|
for _i in 0..(end_column - start_column) {
|
|
|
|
s.push(underline_char);
|
2019-06-19 22:07:01 -04:00
|
|
|
}
|
|
|
|
let color_underline = if is_error {
|
2020-06-29 08:17:37 -04:00
|
|
|
colors::red(&s).to_string()
|
2019-06-19 22:07:01 -04:00
|
|
|
} else {
|
2020-06-29 08:17:37 -04:00
|
|
|
colors::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
|
|
|
}
|
|
|
|
|
2020-09-06 15:44:29 -04:00
|
|
|
/// Wrapper around deno_core::JsError which provides color to_string.
|
2019-07-10 18:53:48 -04:00
|
|
|
#[derive(Debug)]
|
2020-09-06 15:44:29 -04:00
|
|
|
pub struct JsError(deno_core::JsError);
|
2019-07-10 18:53:48 -04:00
|
|
|
|
2020-09-06 15:44:29 -04:00
|
|
|
impl JsError {
|
2020-03-02 17:20:16 -05:00
|
|
|
pub fn create(
|
2020-09-06 15:44:29 -04:00
|
|
|
core_js_error: deno_core::JsError,
|
2019-07-10 18:53:48 -04:00
|
|
|
source_map_getter: &impl SourceMapGetter,
|
|
|
|
) -> ErrBox {
|
2020-03-02 17:20:16 -05:00
|
|
|
let core_js_error = apply_source_map(&core_js_error, source_map_getter);
|
|
|
|
let js_error = Self(core_js_error);
|
2020-08-25 18:22:15 -04:00
|
|
|
js_error.into()
|
2019-07-10 18:53:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-06 15:44:29 -04:00
|
|
|
impl Deref for JsError {
|
|
|
|
type Target = deno_core::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-09-06 15:44:29 -04:00
|
|
|
impl fmt::Display for JsError {
|
2019-06-19 22:07:01 -04:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2020-05-02 23:55:16 -04:00
|
|
|
let mut formatted_frames = self.0.formatted_frames.clone();
|
|
|
|
|
|
|
|
// The formatted_frames passed from prepareStackTrace() are colored.
|
|
|
|
if !colors::use_color() {
|
|
|
|
formatted_frames = formatted_frames
|
|
|
|
.iter()
|
|
|
|
.map(|s| colors::strip_ansi_codes(s).to_string())
|
|
|
|
.collect();
|
|
|
|
}
|
|
|
|
|
2020-04-20 15:39:02 -04:00
|
|
|
// When the stack frame array is empty, but the source location given by
|
|
|
|
// (script_resource_name, line_number, start_column + 1) exists, this is
|
|
|
|
// likely a syntax error. For the sake of formatting we treat it like it was
|
|
|
|
// given as a single stack frame.
|
2020-05-02 23:55:16 -04:00
|
|
|
if formatted_frames.is_empty()
|
2020-04-20 15:39:02 -04:00
|
|
|
&& self.0.script_resource_name.is_some()
|
|
|
|
&& self.0.line_number.is_some()
|
|
|
|
&& self.0.start_column.is_some()
|
|
|
|
{
|
2020-06-08 08:06:20 -04:00
|
|
|
formatted_frames = vec![format_location(
|
2020-06-29 08:17:37 -04:00
|
|
|
self.0.script_resource_name.as_ref().unwrap(),
|
2020-06-08 08:06:20 -04:00
|
|
|
self.0.line_number.unwrap(),
|
|
|
|
self.0.start_column.unwrap() + 1,
|
2020-04-20 15:39:02 -04:00
|
|
|
)]
|
|
|
|
};
|
|
|
|
|
2019-06-19 22:07:01 -04:00
|
|
|
write!(
|
|
|
|
f,
|
2020-04-20 15:39:02 -04:00
|
|
|
"{}",
|
|
|
|
&format_stack(
|
|
|
|
true,
|
2020-06-29 08:17:37 -04:00
|
|
|
&self.0.message,
|
|
|
|
self.0.source_line.as_deref(),
|
2020-04-20 15:39:02 -04:00
|
|
|
self.0.start_column,
|
|
|
|
self.0.end_column,
|
|
|
|
&formatted_frames,
|
|
|
|
0
|
|
|
|
)
|
2019-06-19 22:07:01 -04:00
|
|
|
)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-06 15:44:29 -04:00
|
|
|
impl Error for JsError {}
|
2019-06-19 22:07:01 -04:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-09-15 14:48:25 -04:00
|
|
|
use crate::colors::strip_ansi_codes;
|
2019-06-19 22:07:01 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_none_source_line() {
|
2020-04-20 15:39:02 -04:00
|
|
|
let actual = format_maybe_source_line(None, None, None, false, 0);
|
2019-06-19 22:07:01 -04:00
|
|
|
assert_eq!(actual, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_some_source_line() {
|
|
|
|
let actual = format_maybe_source_line(
|
2020-06-29 08:17:37 -04:00
|
|
|
Some("console.log('foo');"),
|
2019-06-19 22:07:01 -04:00
|
|
|
Some(8),
|
|
|
|
Some(11),
|
|
|
|
true,
|
|
|
|
0,
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
strip_ansi_codes(&actual),
|
2020-04-20 15:39:02 -04:00
|
|
|
"\nconsole.log(\'foo\');\n ~~~"
|
2019-06-19 22:07:01 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|