Improve error messages
authorYehuda Katz <wycats@gmail.com>
Sat, 21 Jun 2014 06:26:19 +0000 (23:26 -0700)
committerYehuda Katz <wycats@gmail.com>
Sat, 21 Jun 2014 06:26:19 +0000 (23:26 -0700)
src/cargo/util/config.rs
src/cargo/util/errors.rs
src/cargo/util/important_paths.rs
src/cargo/util/process_builder.rs

index 411d3cb1febc8a29cc9280d555b956fd6bf1269c..4604ec9c55aeffb153a0f8dea70f24fe5c7210fc 100644 (file)
@@ -12,7 +12,8 @@ impl Config {
     pub fn new() -> CargoResult<Config> {
         Ok(Config {
             home_path: try!(os::homedir().require(|| {
-                human("Couldn't find the home directory")
+                human("Cargo couldn't find your home directory. \
+                      This probably means that $HOME was not set.")
             }))
         })
     }
@@ -99,15 +100,15 @@ impl fmt::Show for ConfigValue {
 
 pub fn get_config(pwd: Path, key: &str) -> CargoResult<ConfigValue> {
     find_in_tree(&pwd, |file| extract_config(file, key)).map_err(|_|
-        internal(format!("config key not found; key={}", key)))
+        human(format!("`{}` not found in your configuration", key)))
 }
 
 pub fn all_configs(pwd: Path) -> CargoResult<HashMap<String, ConfigValue>> {
     let mut map = HashMap::new();
 
-    try!(walk_tree(&pwd, |file| {
-        extract_all_configs(file, &mut map)
-    }));
+    try!(walk_tree(&pwd, |file| extract_all_configs(file, &mut map)).map_err(|_|
+        human("Couldn't load Cargo configuration")));
+
 
     Ok(map)
 }
@@ -119,9 +120,8 @@ fn find_in_tree<T>(pwd: &Path,
     loop {
         let possible = current.join(".cargo").join("config");
         if possible.exists() {
-            let file = try!(io::fs::File::open(&possible).chain_error(|| {
-                internal("could not open file")
-            }));
+            let file = try!(io::fs::File::open(&possible));
+
             match walk(file) {
                 Ok(res) => return Ok(res),
                 _ => ()
@@ -142,9 +142,8 @@ fn walk_tree(pwd: &Path,
     loop {
         let possible = current.join(".cargo").join("config");
         if possible.exists() {
-            let file = try!(io::fs::File::open(&possible).chain_error(|| {
-                internal("could not open file")
-            }));
+            let file = try!(io::fs::File::open(&possible));
+
             match walk(file) {
                 Err(_) => err = false,
                 _ => ()
index e1dc0aa656ad1510c36ebcedf9c874750987f893..c3c1ea4faae917b646a2350b70b198feae914e12 100644 (file)
@@ -1,4 +1,4 @@
-use std::io::process::{Command,ProcessOutput,ProcessExit,ExitStatus,ExitSignal};
+use std::io::process::{ProcessOutput, ProcessExit, ExitStatus, ExitSignal};
 use std::io::IoError;
 use std::fmt;
 use std::fmt::{Show, Formatter, FormatError};
@@ -141,7 +141,6 @@ from_error!(FormatError)
 
 pub struct ProcessError {
     pub msg: String,
-    pub command: String,
     pub exit: Option<ProcessExit>,
     pub output: Option<ProcessOutput>,
     pub detail: Option<String>,
@@ -275,16 +274,15 @@ impl CliError {
 }
 
 pub fn process_error<S: Str>(msg: S,
-                             command: &Command,
+                             cause: Option<IoError>,
                              status: Option<&ProcessExit>,
                              output: Option<&ProcessOutput>) -> ProcessError {
     ProcessError {
         msg: msg.as_slice().to_str(),
-        command: command.to_str(),
         exit: status.map(|o| o.clone()),
         output: output.map(|o| o.clone()),
         detail: None,
-        cause: None
+        cause: cause.map(|c| box c as Box<CargoError>)
     }
 }
 
index f8fd25981625b146f1b356a18fd085734dffe514..7bcc9a551b82b0bf0aafca5527099f2cbf8d69db 100644 (file)
@@ -1,4 +1,4 @@
-use util::{CargoResult, CargoError, internal_error};
+use util::{CargoResult, human};
 
 pub fn find_project(pwd: Path, file: &str) -> CargoResult<Path> {
     let mut current = pwd.clone();
@@ -11,10 +11,5 @@ pub fn find_project(pwd: Path, file: &str) -> CargoResult<Path> {
         if !current.pop() { break; }
     }
 
-    Err(manifest_missing_err(&pwd, file.as_slice()))
-}
-
-fn manifest_missing_err(pwd: &Path, file: &str) -> Box<CargoError> {
-    internal_error("manifest not found",
-                   format!("pwd={}; file={}", pwd.display(), file))
+    Err(human(format!("no manifest found in `{}`", pwd.display())))
 }
index 49e07b9c277c5bda0a6501ba1e23cc077f7a1379..1dc3ca4e6feae1dd0b299995ee2a11169fa32f04 100644 (file)
@@ -80,14 +80,13 @@ impl ProcessBuilder {
         let msg = || format!("Could not execute process `{}`",
                              self.debug_string());
 
-        let exit = try!(command.status().map_err(|_| {
-            process_error(msg(), &command, None, None)
-        }));
+        let exit = try!(command.status().map_err(|e|
+            process_error(msg(), Some(e), None, None)));
 
         if exit.success() {
             Ok(())
         } else {
-            Err(process_error(msg(), &command, Some(&exit), None))
+            Err(process_error(msg(), None, Some(&exit), None))
         }
     }
 
@@ -98,14 +97,14 @@ impl ProcessBuilder {
         let msg = || format!("Could not execute process `{}`",
                              self.debug_string());
 
-        let output = try!(command.output().map_err(|_| {
-            process_error(msg(), &command, None, None)
+        let output = try!(command.output().map_err(|e| {
+            process_error(msg(), Some(e), None, None)
         }));
 
         if output.status.success() {
             Ok(output)
         } else {
-            Err(process_error(msg(), &command, Some(&output.status),
+            Err(process_error(msg(), None, Some(&output.status),
                               Some(&output)))
         }
     }