From 0aca3f06904d2582b4f520e0b03b56bb2255c03e Mon Sep 17 00:00:00 2001 From: 2shiori17 <98276492+2shiori17@users.noreply.github.com> Date: Thu, 14 Jul 2022 05:01:09 +0900 Subject: [PATCH] fix(cli): Improve error message in watch mode (#15184) --- cli/file_watcher.rs | 13 +++++++++-- cli/tests/integration/watcher_tests.rs | 30 ++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/cli/file_watcher.rs b/cli/file_watcher.rs index 36b4276e77..60627548e0 100644 --- a/cli/file_watcher.rs +++ b/cli/file_watcher.rs @@ -1,9 +1,11 @@ // 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 log::info; use notify::event::Event as NotifyEvent; @@ -71,8 +73,15 @@ where { let result = watch_future.await; if let Err(err) = result { - let msg = format!("{}: {}", colors::red_bold("error"), err); - eprintln!("{}", msg); + let error_string = match err.downcast_ref::() { + Some(e) => format_js_error(e), + None => format!("{:?}", err), + }; + eprintln!( + "{}: {}", + colors::red_bold("error"), + error_string.trim_start_matches("error: ") + ); } } diff --git a/cli/tests/integration/watcher_tests.rs b/cli/tests/integration/watcher_tests.rs index 1e79a219dd..dee1987409 100644 --- a/cli/tests/integration/watcher_tests.rs +++ b/cli/tests/integration/watcher_tests.rs @@ -788,6 +788,36 @@ fn run_watch_with_import_map_and_relative_paths() { check_alive_then_kill(child); } +#[test] +fn run_watch_error_messages() { + let t = TempDir::new(); + let file_to_watch = t.path().join("file_to_watch.js"); + write( + &file_to_watch, + "throw SyntaxError(`outer`, {cause: TypeError(`inner`)})", + ) + .unwrap(); + + let mut child = util::deno_cmd() + .current_dir(util::testdata_path()) + .arg("run") + .arg("--watch") + .arg(&file_to_watch) + .env("NO_COLOR", "1") + .stdout(std::process::Stdio::piped()) + .stderr(std::process::Stdio::piped()) + .spawn() + .unwrap(); + let (_, mut stderr_lines) = child_lines(&mut child); + + wait_contains("Process started", &mut stderr_lines); + wait_contains("error: Uncaught SyntaxError: outer", &mut stderr_lines); + wait_contains("Caused by: TypeError: inner", &mut stderr_lines); + wait_contains("Process finished", &mut stderr_lines); + + check_alive_then_kill(child); +} + #[test] fn test_watch() { let t = TempDir::new();