Improve error messages

This commit is contained in:
Yehuda Katz 2014-06-20 23:26:19 -07:00
parent 78d8d61ccc
commit 89b9374814
4 changed files with 21 additions and 30 deletions

View 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,
_ => ()

View 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>)
}
}

View 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())))
}

View 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)))
}
}