From 4b61170e224344a94152ef6f618d6c71834e5cea Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Sat, 2 Feb 2019 01:58:53 -0500 Subject: [PATCH] Better error message for bad filename CLI argument. --- src/errors.rs | 10 +++++++++ src/isolate.rs | 16 ++++++++------- src/js_errors.rs | 26 ++++++++++-------------- src/main.rs | 8 ++++++-- tests/error_009_missing_js_module.js.out | 2 +- tests/error_010_nonexistent_arg.out | 1 + tests/error_010_nonexistent_arg.test | 4 ++++ 7 files changed, 42 insertions(+), 25 deletions(-) create mode 100644 tests/error_010_nonexistent_arg.out create mode 100644 tests/error_010_nonexistent_arg.test diff --git a/src/errors.rs b/src/errors.rs index 74502acc50..3a6937495a 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -180,6 +180,7 @@ pub fn permission_denied() -> DenoError { ) } +#[derive(Debug)] pub enum RustOrJsError { Rust(DenoError), Js(JSError), @@ -196,3 +197,12 @@ impl From for RustOrJsError { RustOrJsError::Js(e) } } + +impl fmt::Display for RustOrJsError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + RustOrJsError::Rust(e) => e.fmt(f), + RustOrJsError::Js(e) => e.fmt(f), + } + } +} diff --git a/src/isolate.rs b/src/isolate.rs index e0a657ff2d..a9868b8484 100644 --- a/src/isolate.rs +++ b/src/isolate.rs @@ -404,17 +404,19 @@ impl Isolate { &mut self, js_filename: &str, is_prefetch: bool, - ) -> Result<(), JSError> { - let out = - code_fetch_and_maybe_compile(&self.state, js_filename, ".").unwrap(); + ) -> Result<(), RustOrJsError> { + let out = code_fetch_and_maybe_compile(&self.state, js_filename, ".") + .map_err(RustOrJsError::from)?; - let id = self.mod_new(out.filename.clone(), out.js_source())?; + let id = self + .mod_new(out.filename.clone(), out.js_source()) + .map_err(RustOrJsError::from)?; - self.mod_load_deps(id).ok(); + self.mod_load_deps(id)?; - self.mod_instantiate(id)?; + self.mod_instantiate(id).map_err(RustOrJsError::from)?; if !is_prefetch { - self.mod_evaluate(id)?; + self.mod_evaluate(id).map_err(RustOrJsError::from)?; } Ok(()) } diff --git a/src/js_errors.rs b/src/js_errors.rs index 73116d6793..851d64be46 100644 --- a/src/js_errors.rs +++ b/src/js_errors.rs @@ -14,6 +14,7 @@ use source_map_mappings::parse_mappings; use source_map_mappings::Bias; use source_map_mappings::Mappings; use std::collections::HashMap; +use std::fmt; pub trait SourceMapGetter { /// Returns the raw source map file. @@ -73,39 +74,34 @@ impl ToString for StackFrame { } } -impl ToString for JSError { - fn to_string(&self) -> String { - // TODO Improve the formatting of these error messages. - let mut s = String::new(); - +impl fmt::Display for JSError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { if self.script_resource_name.is_some() { let script_resource_name = self.script_resource_name.as_ref().unwrap(); // Avoid showing internal code from gen/bundle/main.js if script_resource_name != "gen/bundle/main.js" { - s.push_str(script_resource_name); + write!(f, "{}", script_resource_name)?; if self.line_number.is_some() { - s.push_str(&format!( + write!( + f, ":{}:{}", self.line_number.unwrap(), self.start_column.unwrap() - )); + )?; assert!(self.start_column.is_some()); } if self.source_line.is_some() { - s.push_str("\n"); - s.push_str(self.source_line.as_ref().unwrap()); - s.push_str("\n\n"); + write!(f, "\n{}\n\n", self.source_line.as_ref().unwrap())?; } } } - s.push_str(&self.message); + write!(f, "{}", &self.message)?; for frame in &self.frames { - s.push_str("\n"); - s.push_str(&frame.to_string()); + write!(f, "\n{}", &frame.to_string())?; } - s + Ok(()) } } diff --git a/src/main.rs b/src/main.rs index d50a68ad59..705b9db3dc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -55,7 +55,7 @@ impl log::Log for Logger { fn flush(&self) {} } -fn print_err_and_exit(err: js_errors::JSError) { +fn print_err_and_exit(err: errors::RustOrJsError) { eprintln!("{}", err.to_string()); std::process::exit(1); } @@ -100,6 +100,7 @@ fn main() { // Setup runtime. isolate .execute("denoMain();") + .map_err(errors::RustOrJsError::from) .unwrap_or_else(print_err_and_exit); // Execute input file. @@ -110,6 +111,9 @@ fn main() { .unwrap_or_else(print_err_and_exit); } - isolate.event_loop().unwrap_or_else(print_err_and_exit); + isolate + .event_loop() + .map_err(errors::RustOrJsError::from) + .unwrap_or_else(print_err_and_exit); }); } diff --git a/tests/error_009_missing_js_module.js.out b/tests/error_009_missing_js_module.js.out index ca41435faa..edb08da1c3 100644 --- a/tests/error_009_missing_js_module.js.out +++ b/tests/error_009_missing_js_module.js.out @@ -1 +1 @@ -Uncaught Cannot resolve module "./bad-module.js" from "[WILDCARD]error_009_missing_js_module.js" +Cannot resolve module "./bad-module.js" from "[WILDCARD]error_009_missing_js_module.js" diff --git a/tests/error_010_nonexistent_arg.out b/tests/error_010_nonexistent_arg.out new file mode 100644 index 0000000000..248cbc3296 --- /dev/null +++ b/tests/error_010_nonexistent_arg.out @@ -0,0 +1 @@ +[WILDCARD]Cannot resolve module "not-a-valid-filename.ts" from "." diff --git a/tests/error_010_nonexistent_arg.test b/tests/error_010_nonexistent_arg.test new file mode 100644 index 0000000000..9d183107c3 --- /dev/null +++ b/tests/error_010_nonexistent_arg.test @@ -0,0 +1,4 @@ +args: not-a-valid-filename.ts +output: tests/error_010_nonexistent_arg.out +exit_code: 1 +check_stderr: true