Skip to content

Commit

Permalink
Make internel error frames dimmer (denoland#4201)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinkassimo committed Mar 1, 2020
1 parent ba0991a commit c3661e9
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 15 deletions.
15 changes: 15 additions & 0 deletions cli/colors.rs
Expand Up @@ -92,3 +92,18 @@ pub fn bold(s: String) -> impl fmt::Display {
style_spec.set_bold(true);
style(&s, style_spec)
}

pub fn gray(s: String) -> impl fmt::Display {
let mut style_spec = ColorSpec::new();
style_spec.set_fg(Some(Ansi256(8)));
style(&s, style_spec)
}

pub fn italic_bold_gray(s: String) -> impl fmt::Display {
let mut style_spec = ColorSpec::new();
style_spec
.set_fg(Some(Ansi256(8)))
.set_bold(true)
.set_italic(true);
style(&s, style_spec)
}
68 changes: 53 additions & 15 deletions cli/fmt_errors.rs
Expand Up @@ -19,11 +19,22 @@ pub trait DisplayFormatter {
fn format_source_name(&self) -> String;
}

fn format_source_name(script_name: String, line: i64, column: i64) -> String {
let script_name_c = colors::cyan(script_name);
let line_c = colors::yellow((1 + line).to_string());
let column_c = colors::yellow((1 + column).to_string());
format!("{}:{}:{}", script_name_c, line_c, column_c,)
fn format_source_name(
script_name: String,
line: i64,
column: i64,
is_internal: bool,
) -> String {
let line = line + 1;
let column = column + 1;
if is_internal {
format!("{}:{}:{}", script_name, line, column)
} else {
let script_name_c = colors::cyan(script_name);
let line_c = colors::yellow(line.to_string());
let column_c = colors::yellow(column.to_string());
format!("{}:{}:{}", script_name_c, line_c, column_c)
}
}

/// Formats optional source, line and column into a single string.
Expand All @@ -38,7 +49,12 @@ pub fn format_maybe_source_name(

assert!(line.is_some());
assert!(column.is_some());
format_source_name(script_name.unwrap(), line.unwrap(), column.unwrap())
format_source_name(
script_name.unwrap(),
line.unwrap(),
column.unwrap(),
false,
)
}

/// Take an optional source line and associated information to format it into
Expand Down Expand Up @@ -108,18 +124,39 @@ pub fn format_error_message(msg: String) -> String {
format!("{} {}", preamble, msg)
}

fn format_stack_frame(frame: &StackFrame) -> String {
fn format_stack_frame(frame: &StackFrame, is_internal_frame: bool) -> String {
// Note when we print to string, we change from 0-indexed to 1-indexed.
let function_name = colors::italic_bold(frame.function_name.clone());
let source_loc =
format_source_name(frame.script_name.clone(), frame.line, frame.column);

let function_name = if is_internal_frame {
colors::italic_bold_gray(frame.function_name.clone()).to_string()
} else {
colors::italic_bold(frame.function_name.clone()).to_string()
};
let mut source_loc = format_source_name(
frame.script_name.clone(),
frame.line,
frame.column,
is_internal_frame,
);

// Each chunk of styled text is auto-resetted on end,
// which make nesting not working.
// Explicitly specify color for each section.
let mut at_prefix = " at".to_owned();
if is_internal_frame {
at_prefix = colors::gray(at_prefix).to_string();
}
if !frame.function_name.is_empty() || frame.is_eval {
source_loc = format!("({})", source_loc); // wrap then style
}
if is_internal_frame {
source_loc = colors::gray(source_loc).to_string();
}
if !frame.function_name.is_empty() {
format!(" at {} ({})", function_name, source_loc)
format!("{} {} {}", at_prefix, function_name, source_loc)
} else if frame.is_eval {
format!(" at eval ({})", source_loc)
format!("{} eval {}", at_prefix, source_loc)
} else {
format!(" at {}", source_loc)
format!("{} {}", at_prefix, source_loc)
}
}

Expand Down Expand Up @@ -213,7 +250,8 @@ impl fmt::Display for JSError {
)?;

for frame in &self.0.frames {
write!(f, "\n{}", format_stack_frame(&frame))?;
let is_internal_frame = frame.script_name.starts_with("$deno$");
write!(f, "\n{}", format_stack_frame(&frame, is_internal_frame))?;
}
Ok(())
}
Expand Down

0 comments on commit c3661e9

Please sign in to comment.