Skip to content

Commit

Permalink
Better error message for bad filename CLI argument.
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Feb 2, 2019
1 parent 98d20cd commit 4b61170
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 25 deletions.
10 changes: 10 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ pub fn permission_denied() -> DenoError {
)
}

#[derive(Debug)]
pub enum RustOrJsError {
Rust(DenoError),
Js(JSError),
Expand All @@ -196,3 +197,12 @@ impl From<JSError> 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),
}
}
}
16 changes: 9 additions & 7 deletions src/isolate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down
26 changes: 11 additions & 15 deletions src/js_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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(())
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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.
Expand All @@ -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);
});
}
2 changes: 1 addition & 1 deletion tests/error_009_missing_js_module.js.out
Original file line number Diff line number Diff line change
@@ -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"
1 change: 1 addition & 0 deletions tests/error_010_nonexistent_arg.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[WILDCARD]Cannot resolve module "not-a-valid-filename.ts" from "."
4 changes: 4 additions & 0 deletions tests/error_010_nonexistent_arg.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
args: not-a-valid-filename.ts
output: tests/error_010_nonexistent_arg.out
exit_code: 1
check_stderr: true

0 comments on commit 4b61170

Please sign in to comment.