summaryrefslogtreecommitdiff
path: root/cli
diff options
context:
space:
mode:
Diffstat (limited to 'cli')
-rw-r--r--cli/file_watcher.rs2
-rw-r--r--cli/fmt_errors.rs216
-rw-r--r--cli/main.rs3
-rw-r--r--cli/standalone.rs2
-rw-r--r--cli/tools/lint.rs4
-rw-r--r--cli/tools/test.rs2
-rw-r--r--cli/worker.rs2
7 files changed, 7 insertions, 224 deletions
diff --git a/cli/file_watcher.rs b/cli/file_watcher.rs
index c5a92e5bf..6c8a142a6 100644
--- a/cli/file_watcher.rs
+++ b/cli/file_watcher.rs
@@ -1,12 +1,12 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
use crate::colors;
-use crate::fmt_errors::format_js_error;
use crate::fs_util::canonicalize_path;
use deno_core::error::AnyError;
use deno_core::error::JsError;
use deno_core::futures::Future;
+use deno_runtime::fmt_errors::format_js_error;
use log::info;
use notify::event::Event as NotifyEvent;
use notify::event::EventKind;
diff --git a/cli/fmt_errors.rs b/cli/fmt_errors.rs
deleted file mode 100644
index 53cf975bc..000000000
--- a/cli/fmt_errors.rs
+++ /dev/null
@@ -1,216 +0,0 @@
-// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
-//! This mod provides DenoError to unify errors across Deno.
-use crate::colors::cyan;
-use crate::colors::italic_bold;
-use crate::colors::red;
-use crate::colors::yellow;
-use deno_core::error::format_file_name;
-use deno_core::error::JsError;
-use deno_core::error::JsStackFrame;
-use std::fmt::Write as _;
-
-// Keep in sync with `/core/error.js`.
-pub fn format_location(frame: &JsStackFrame) -> String {
- let _internal = frame
- .file_name
- .as_ref()
- .map_or(false, |f| f.starts_with("deno:"));
- if frame.is_native {
- return cyan("native").to_string();
- }
- let mut result = String::new();
- let file_name = frame.file_name.clone().unwrap_or_default();
- if !file_name.is_empty() {
- result += &cyan(&format_file_name(&file_name)).to_string();
- } else {
- if frame.is_eval {
- result +=
- &(cyan(&frame.eval_origin.as_ref().unwrap()).to_string() + ", ");
- }
- result += &cyan("<anonymous>").to_string();
- }
- if let Some(line_number) = frame.line_number {
- write!(result, ":{}", yellow(&line_number.to_string())).unwrap();
- if let Some(column_number) = frame.column_number {
- write!(result, ":{}", yellow(&column_number.to_string())).unwrap();
- }
- }
- result
-}
-
-fn format_frame(frame: &JsStackFrame) -> String {
- let _internal = frame
- .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 {
- result += "async ";
- }
- if frame.is_promise_all {
- result += &italic_bold(&format!(
- "Promise.all (index {})",
- frame.promise_index.unwrap_or_default()
- ))
- .to_string();
- 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) {
- write!(formatted_method, "{}.", type_name).unwrap();
- }
- }
- formatted_method += function_name;
- if let Some(method_name) = &frame.method_name {
- if !function_name.ends_with(method_name) {
- write!(formatted_method, " [as {}]", method_name).unwrap();
- }
- }
- } else {
- if let Some(type_name) = &frame.type_name {
- write!(formatted_method, "{}.", type_name).unwrap();
- }
- if let Some(method_name) = &frame.method_name {
- formatted_method += method_name
- } else {
- formatted_method += "<anonymous>";
- }
- }
- result += &italic_bold(&formatted_method).to_string();
- } else if frame.is_constructor {
- result += "new ";
- if let Some(function_name) = &frame.function_name {
- write!(result, "{}", italic_bold(&function_name)).unwrap();
- } else {
- result += &cyan("<anonymous>").to_string();
- }
- } else if let Some(function_name) = &frame.function_name {
- result += &italic_bold(&function_name).to_string();
- } else {
- result += &format_location(frame);
- return result;
- }
- write!(result, " ({})", format_location(frame)).unwrap();
- result
-}
-
-/// Take an optional source line and associated information to format it into
-/// a pretty printed version of that line.
-fn format_maybe_source_line(
- source_line: Option<&str>,
- column_number: Option<i64>,
- is_error: bool,
- level: usize,
-) -> String {
- if source_line.is_none() || column_number.is_none() {
- return "".to_string();
- }
-
- let source_line = source_line.unwrap();
- // sometimes source_line gets set with an empty string, which then outputs
- // an empty source line when displayed, so need just short circuit here.
- if source_line.is_empty() {
- return "".to_string();
- }
- if source_line.contains("Couldn't format source line: ") {
- return format!("\n{}", source_line);
- }
-
- let mut s = String::new();
- let column_number = column_number.unwrap();
-
- if column_number as usize > source_line.len() {
- return format!(
- "\n{} Couldn't format source line: Column {} is out of bounds (source may have changed at runtime)",
- yellow("Warning"), column_number,
- );
- }
-
- for _i in 0..(column_number - 1) {
- if source_line.chars().nth(_i as usize).unwrap() == '\t' {
- s.push('\t');
- } else {
- s.push(' ');
- }
- }
- s.push('^');
- let color_underline = if is_error {
- red(&s).to_string()
- } else {
- cyan(&s).to_string()
- };
-
- let indent = format!("{:indent$}", "", indent = level);
-
- format!("\n{}{}\n{}{}", indent, source_line, indent, color_underline)
-}
-
-fn format_js_error_inner(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_inner(aggregated_error, true);
- for line in error_string.trim_start_matches("Uncaught ").lines() {
- write!(s, "\n {}", line).unwrap();
- }
- }
- }
- 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 {
- write!(s, "\n at {}", format_frame(frame)).unwrap();
- }
- if let Some(cause) = &js_error.cause {
- let error_string = format_js_error_inner(cause, true);
- write!(
- s,
- "\nCaused by: {}",
- error_string.trim_start_matches("Uncaught ")
- )
- .unwrap();
- }
- s
-}
-
-pub fn format_js_error(js_error: &JsError) -> String {
- format_js_error_inner(js_error, false)
-}
-
-#[cfg(test)]
-mod tests {
- use super::*;
- use test_util::strip_ansi_codes;
-
- #[test]
- fn test_format_none_source_line() {
- let actual = format_maybe_source_line(None, None, false, 0);
- assert_eq!(actual, "");
- }
-
- #[test]
- fn test_format_some_source_line() {
- let actual =
- format_maybe_source_line(Some("console.log('foo');"), Some(9), true, 0);
- assert_eq!(
- strip_ansi_codes(&actual),
- "\nconsole.log(\'foo\');\n ^"
- );
- }
-}
diff --git a/cli/main.rs b/cli/main.rs
index 0c4b5c893..bffe9c470 100644
--- a/cli/main.rs
+++ b/cli/main.rs
@@ -15,7 +15,6 @@ mod emit;
mod errors;
mod file_fetcher;
mod file_watcher;
-mod fmt_errors;
mod fs_util;
mod graph_util;
mod http_cache;
@@ -67,7 +66,6 @@ use crate::cache::TypeCheckCache;
use crate::emit::TsConfigType;
use crate::file_fetcher::File;
use crate::file_watcher::ResolutionResult;
-use crate::fmt_errors::format_js_error;
use crate::graph_util::graph_lock_or_exit;
use crate::graph_util::graph_valid;
use crate::proc_state::ProcState;
@@ -89,6 +87,7 @@ use deno_core::serde_json::json;
use deno_core::v8_set_flags;
use deno_core::ModuleSpecifier;
use deno_runtime::colors;
+use deno_runtime::fmt_errors::format_js_error;
use deno_runtime::permissions::Permissions;
use deno_runtime::tokio_util::run_local;
use log::debug;
diff --git a/cli/standalone.rs b/cli/standalone.rs
index 147a1a57e..fa5fffd13 100644
--- a/cli/standalone.rs
+++ b/cli/standalone.rs
@@ -3,7 +3,6 @@
use crate::args::Flags;
use crate::colors;
use crate::file_fetcher::get_source_from_data_url;
-use crate::fmt_errors::format_js_error;
use crate::ops;
use crate::proc_state::ProcState;
use crate::version;
@@ -25,6 +24,7 @@ use deno_graph::source::Resolver;
use deno_runtime::deno_broadcast_channel::InMemoryBroadcastChannel;
use deno_runtime::deno_tls::rustls_pemfile;
use deno_runtime::deno_web::BlobStore;
+use deno_runtime::fmt_errors::format_js_error;
use deno_runtime::permissions::Permissions;
use deno_runtime::permissions::PermissionsOptions;
use deno_runtime::worker::MainWorker;
diff --git a/cli/tools/lint.rs b/cli/tools/lint.rs
index cfc663387..0bee44d4c 100644
--- a/cli/tools/lint.rs
+++ b/cli/tools/lint.rs
@@ -12,7 +12,6 @@ use crate::args::LintFlags;
use crate::colors;
use crate::file_watcher;
use crate::file_watcher::ResolutionResult;
-use crate::fmt_errors;
use crate::fs_util::collect_files;
use crate::fs_util::is_supported_ext;
use crate::fs_util::specifier_to_file_path;
@@ -29,6 +28,7 @@ use deno_lint::linter::Linter;
use deno_lint::linter::LinterBuilder;
use deno_lint::rules;
use deno_lint::rules::LintRule;
+use deno_runtime::fmt_errors::format_location;
use log::debug;
use log::info;
use serde::Serialize;
@@ -382,7 +382,7 @@ impl LintReporter for PrettyLintReporter {
&source_lines,
d.range.clone(),
d.hint.as_ref(),
- &fmt_errors::format_location(&JsStackFrame::from_location(
+ &format_location(&JsStackFrame::from_location(
Some(d.filename.clone()),
Some(d.range.start.line_index as i64 + 1), // 1-indexed
// todo(#11111): make 1-indexed as well
diff --git a/cli/tools/test.rs b/cli/tools/test.rs
index 265b39b57..cb5b41042 100644
--- a/cli/tools/test.rs
+++ b/cli/tools/test.rs
@@ -10,7 +10,6 @@ use crate::display;
use crate::file_fetcher::File;
use crate::file_watcher;
use crate::file_watcher::ResolutionResult;
-use crate::fmt_errors::format_js_error;
use crate::fs_util::collect_specifiers;
use crate::fs_util::is_supported_test_ext;
use crate::fs_util::is_supported_test_path;
@@ -34,6 +33,7 @@ use deno_core::parking_lot::Mutex;
use deno_core::url::Url;
use deno_core::ModuleSpecifier;
use deno_graph::ModuleKind;
+use deno_runtime::fmt_errors::format_js_error;
use deno_runtime::ops::io::Stdio;
use deno_runtime::ops::io::StdioPipe;
use deno_runtime::permissions::Permissions;
diff --git a/cli/worker.rs b/cli/worker.rs
index 1c4c6475a..d36639821 100644
--- a/cli/worker.rs
+++ b/cli/worker.rs
@@ -11,6 +11,7 @@ use deno_core::Extension;
use deno_core::ModuleId;
use deno_graph::source::ResolveResponse;
use deno_runtime::colors;
+use deno_runtime::fmt_errors::format_js_error;
use deno_runtime::ops::worker_host::CreateWebWorkerCb;
use deno_runtime::ops::worker_host::WorkerEventCb;
use deno_runtime::permissions::Permissions;
@@ -24,7 +25,6 @@ use crate::args::DenoSubcommand;
use crate::checksum;
use crate::compat;
use crate::errors;
-use crate::fmt_errors::format_js_error;
use crate::module_loader::CliModuleLoader;
use crate::node;
use crate::npm::NpmPackageReference;