summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2019-02-02 01:58:53 -0500
committerRyan Dahl <ry@tinyclouds.org>2019-02-02 17:33:21 -0500
commit4b61170e224344a94152ef6f618d6c71834e5cea (patch)
treed0a58d0a5a47c14be529621bea886d05863fb67b /src
parent98d20cd17894e02710a20425ba5ae8be52cfe932 (diff)
Better error message for bad filename CLI argument.
Diffstat (limited to 'src')
-rw-r--r--src/errors.rs10
-rw-r--r--src/isolate.rs16
-rw-r--r--src/js_errors.rs26
-rw-r--r--src/main.rs8
4 files changed, 36 insertions, 24 deletions
diff --git a/src/errors.rs b/src/errors.rs
index 74502acc5..3a6937495 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<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),
+ }
+ }
+}
diff --git a/src/isolate.rs b/src/isolate.rs
index e0a657ff2..a9868b848 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 73116d679..851d64be4 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 d50a68ad5..705b9db3d 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);
});
}