1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 07:14:47 -05:00

fix(cli): Improve error message in watch mode (#15184)

This commit is contained in:
2shiori17 2022-07-14 05:01:09 +09:00 committed by GitHub
parent 5273259eef
commit 0aca3f0690
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 2 deletions

View file

@ -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::<JsError>() {
Some(e) => format_js_error(e),
None => format!("{:?}", err),
};
eprintln!(
"{}: {}",
colors::red_bold("error"),
error_string.trim_start_matches("error: ")
);
}
}

View file

@ -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();