diff --git a/src/cargo/lib.rs b/src/cargo/lib.rs index 4952bd7eb..0830956b0 100644 --- a/src/cargo/lib.rs +++ b/src/cargo/lib.rs @@ -32,8 +32,8 @@ macro_rules! some( macro_rules! cargo_try ( ($expr:expr) => ({ - use util::BoxError; - try!($expr.box_error()) + use util::CargoError; + try!($expr.map_err(|err| err.to_error())) }) ) diff --git a/src/cargo/util/config.rs b/src/cargo/util/config.rs index 535b87fe7..aee0bb83c 100644 --- a/src/cargo/util/config.rs +++ b/src/cargo/util/config.rs @@ -2,7 +2,7 @@ use std::{io,fmt,os}; use std::collections::HashMap; use serialize::{Encodable,Encoder}; use toml; -use util::{CargoResult, Require, error, internal_error}; +use util::{CargoResult, ChainError, Require, error, internal_error}; pub struct Config { home_path: Path @@ -11,7 +11,7 @@ pub struct Config { impl Config { pub fn new() -> CargoResult { Ok(Config { - home_path: try!(os::homedir() + home_path: cargo_try!(os::homedir() .require(|| "Couldn't find the home directory")) }) } @@ -102,7 +102,7 @@ pub fn get_config(pwd: Path, key: &str) -> CargoResult { pub fn all_configs(pwd: Path) -> CargoResult> { let mut map = HashMap::new(); - try!(walk_tree(&pwd, |file| { + cargo_try!(walk_tree(&pwd, |file| { extract_all_configs(file, &mut map) })); @@ -115,7 +115,7 @@ fn find_in_tree(pwd: &Path, walk: |io::fs::File| -> CargoResult) -> CargoR loop { let possible = current.join(".cargo").join("config"); if possible.exists() { - let file = try!(io::fs::File::open(&possible).map_err(|_| error("could not open file"))); + let file = cargo_try!(io::fs::File::open(&possible).chain_error(|| error("could not open file"))); match walk(file) { Ok(res) => return Ok(res), _ => () @@ -135,7 +135,7 @@ fn walk_tree(pwd: &Path, walk: |io::fs::File| -> CargoResult<()>) -> CargoResult loop { let possible = current.join(".cargo").join("config"); if possible.exists() { - let file = try!(io::fs::File::open(&possible).map_err(|_| error("could not open file"))); + let file = cargo_try!(io::fs::File::open(&possible).chain_error(|| error("could not open file"))); match walk(file) { Err(_) => err = false, _ => () @@ -152,8 +152,8 @@ fn walk_tree(pwd: &Path, walk: |io::fs::File| -> CargoResult<()>) -> CargoResult fn extract_config(file: io::fs::File, key: &str) -> CargoResult { let path = file.path().clone(); let mut buf = io::BufferedReader::new(file); - let root = try!(toml::parse_from_buffer(&mut buf).map_err(|_| error(""))); - let val = try!(root.lookup(key).require(|| error(""))); + let root = cargo_try!(toml::parse_from_buffer(&mut buf)); + let val = cargo_try!(root.lookup(key).require(|| error(""))); let v = match val { &toml::String(ref val) => String(val.clone()), @@ -167,11 +167,11 @@ fn extract_config(file: io::fs::File, key: &str) -> CargoResult { fn extract_all_configs(file: io::fs::File, map: &mut HashMap) -> CargoResult<()> { let path = file.path().clone(); let mut buf = io::BufferedReader::new(file); - let root = try!(toml::parse_from_buffer(&mut buf).map_err(|err| - internal_error("could not parse Toml manifest", format!("path={}; err={}", path.display(), err.to_str())))); + let root = cargo_try!(toml::parse_from_buffer(&mut buf).chain_error(|| + error(format!("could not parse Toml manifest; path={}", path.display())))); - let table = try!(root.get_table() - .require(|| internal_error("could not parse Toml manifest", format!("path={}", path.display())))); + let table = cargo_try!(root.get_table().require(|| + error(format!("could not parse Toml manifest; path={}", path.display())))); for (key, value) in table.iter() { match value { @@ -181,8 +181,8 @@ fn extract_all_configs(file: io::fs::File, map: &mut HashMap () } diff --git a/src/cargo/util/errors.rs b/src/cargo/util/errors.rs index 707c9aa30..c23fddb72 100644 --- a/src/cargo/util/errors.rs +++ b/src/cargo/util/errors.rs @@ -11,6 +11,10 @@ pub trait CargoError { fn cause<'a>(&'a self) -> Option<&'a CargoError> { None } fn is_human(&self) -> bool { false } + fn to_error>(self) -> E { + FromError::from_error(self) + } + fn box_error(self) -> Box { box self as Box } @@ -25,6 +29,25 @@ pub trait CargoError { } } +pub trait FromError { + fn from_error(error: E) -> Self; +} + +impl FromError for Box { + fn from_error(error: E) -> Box { + error.box_error() + } +} + +macro_rules! from_error ( + ($ty:ty) => { + impl FromError<$ty> for $ty { + fn from_error(error: $ty) -> $ty { + error + } + } + } +) impl Show for Box { fn fmt(&self, f: &mut Formatter) -> fmt::Result { @@ -95,10 +118,14 @@ impl CargoError for IoError { fn description(&self) -> String { self.to_str() } } +from_error!(IoError) + impl CargoError for TomlError { fn description(&self) -> String { self.to_str() } } +from_error!(TomlError) + pub struct ProcessError { pub msg: String, pub command: String, @@ -108,6 +135,8 @@ pub struct ProcessError { pub cause: Option> } +from_error!(ProcessError) + impl Show for ProcessError { fn fmt(&self, f: &mut Formatter) -> fmt::Result { let exit = match self.exit {