diff options
author | Nayeem Rahman <nayeemrmn99@gmail.com> | 2020-04-11 07:08:11 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-11 02:08:11 -0400 |
commit | 2b362bef8523f3d8c05ce5d3d4b4a839b669568d (patch) | |
tree | dd5c567e66fc3141fa486c93ce2e899aba802fe4 /cli/fmt_errors.rs | |
parent | 2feb661b858f3c8380ceb0362d137079e12b9248 (diff) |
refactor(cli/fmt_errors): Format stack frames in prepareStackTrace() (#4706)
Diffstat (limited to 'cli/fmt_errors.rs')
-rw-r--r-- | cli/fmt_errors.rs | 115 |
1 files changed, 6 insertions, 109 deletions
diff --git a/cli/fmt_errors.rs b/cli/fmt_errors.rs index b7cfbbce9..784bcc9c9 100644 --- a/cli/fmt_errors.rs +++ b/cli/fmt_errors.rs @@ -4,7 +4,6 @@ use crate::colors; use crate::source_maps::apply_source_map; use crate::source_maps::SourceMapGetter; use deno_core::ErrBox; -use deno_core::JSStackFrame; use std::error::Error; use std::fmt; use std::ops::Deref; @@ -25,18 +24,13 @@ fn format_source_name( script_name: String, line_number: i64, column: i64, - is_internal: bool, ) -> String { let line_number = line_number + 1; let column = column + 1; - if is_internal { - format!("{}:{}:{}", script_name, line_number, column) - } else { - let script_name_c = colors::cyan(script_name); - let line_c = colors::yellow(line_number.to_string()); - let column_c = colors::yellow(column.to_string()); - format!("{}:{}:{}", script_name_c, line_c, column_c) - } + let script_name_c = colors::cyan(script_name); + let line_c = colors::yellow(line_number.to_string()); + let column_c = colors::yellow(column.to_string()); + format!("{}:{}:{}", script_name_c, line_c, column_c) } /// Formats optional source, line number and column into a single string. @@ -55,7 +49,6 @@ pub fn format_maybe_source_name( script_name.unwrap(), line_number.unwrap(), column.unwrap(), - false, ) } @@ -127,54 +120,6 @@ pub fn format_error_message(msg: String) -> String { format!("{} {}", preamble, msg) } -fn format_stack_frame(frame: &JSStackFrame, is_internal_frame: bool) -> String { - // Note when we print to string, we change from 0-indexed to 1-indexed. - 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_number, - 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() { - if frame.is_async { - format!( - "{} {} {} {}", - at_prefix, - colors::gray("async".to_owned()).to_string(), - function_name, - source_loc - ) - } else { - format!("{} {} {}", at_prefix, function_name, source_loc) - } - } else if frame.is_eval { - format!("{} eval {}", at_prefix, source_loc) - } else if frame.is_async { - format!("{} async {}", at_prefix, source_loc) - } else { - format!("{} {}", at_prefix, source_loc) - } -} - /// Wrapper around deno_core::JSError which provides color to_string. #[derive(Debug)] pub struct JSError(deno_core::JSError); @@ -251,10 +196,8 @@ impl fmt::Display for JSError { self.format_source_name(), self.format_source_line(0), )?; - - for frame in &self.0.frames { - let is_internal_frame = frame.script_name.starts_with("$deno$"); - write!(f, "\n{}", format_stack_frame(&frame, is_internal_frame))?; + for formatted_frame in &self.0.formatted_frames { + write!(f, "\n at {}", formatted_frame)?; } Ok(()) } @@ -268,52 +211,6 @@ mod tests { use crate::colors::strip_ansi_codes; #[test] - fn js_error_to_string() { - let core_js_error = deno_core::JSError { - message: "Error: foo bar".to_string(), - source_line: None, - script_resource_name: None, - line_number: None, - start_column: None, - end_column: None, - frames: vec![ - JSStackFrame { - line_number: 4, - column: 16, - script_name: "foo_bar.ts".to_string(), - function_name: "foo".to_string(), - is_eval: false, - is_constructor: false, - is_async: false, - }, - JSStackFrame { - line_number: 5, - column: 20, - script_name: "bar_baz.ts".to_string(), - function_name: "qat".to_string(), - is_eval: false, - is_constructor: false, - is_async: false, - }, - JSStackFrame { - line_number: 1, - column: 1, - script_name: "deno_main.js".to_string(), - function_name: "".to_string(), - is_eval: false, - is_constructor: false, - is_async: false, - }, - ], - already_source_mapped: true, - }; - let formatted_error = JSError(core_js_error).to_string(); - let actual = strip_ansi_codes(&formatted_error); - let expected = "error: Error: foo bar\n at foo (foo_bar.ts:5:17)\n at qat (bar_baz.ts:6:21)\n at deno_main.js:2:2"; - assert_eq!(actual, expected); - } - - #[test] fn test_format_none_source_name() { let actual = format_maybe_source_name(None, None, None); assert_eq!(actual, ""); |