Skip to content

Commit 03c0a41

Browse files
committed
Auto merge of #4090 - jluner:master, r=alexcrichton
Add error-chain errors. Fixes #4209 Convert CargoResult, CargoError into an implementation provided by error-chain. The previous is_human machinery is mostly removed; now errors are displayed unless of the Internal kind, verbose mode will print all errors.
2 parents b30694b + def249f commit 03c0a41

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+862
-1066
lines changed

Cargo.lock

+52
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ crossbeam = "0.2"
2222
curl = "0.4.6"
2323
docopt = "0.7"
2424
env_logger = "0.4"
25+
error-chain = "0.10.0"
2526
filetime = "0.1"
2627
flate2 = "0.2"
2728
fs2 = "0.4"

src/bin/bench.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use cargo::core::Workspace;
22
use cargo::ops::{self, MessageFormat, Packages};
3-
use cargo::util::{CliResult, CliError, Human, Config, human};
3+
use cargo::util::{CliResult, CliError, Config, CargoErrorKind};
44
use cargo::util::important_paths::{find_root_manifest_for_wd};
55

66
#[derive(RustcDecodable)]
@@ -128,8 +128,8 @@ pub fn execute(options: Options, config: &Config) -> CliResult {
128128
None => Ok(()),
129129
Some(err) => {
130130
Err(match err.exit.as_ref().and_then(|e| e.code()) {
131-
Some(i) => CliError::new(human("bench failed"), i),
132-
None => CliError::new(Box::new(Human(err)), 101)
131+
Some(i) => CliError::new("bench failed".into(), i),
132+
None => CliError::new(CargoErrorKind::CargoTestErrorKind(err).into(), 101)
133133
})
134134
}
135135
}

src/bin/cargo.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use std::fs;
1717
use std::path::{Path, PathBuf};
1818

1919
use cargo::core::shell::{Verbosity, ColorConfig};
20-
use cargo::util::{self, CliResult, lev_distance, Config, human, CargoResult};
20+
use cargo::util::{self, CliResult, lev_distance, Config, CargoResult, CargoError, CargoErrorKind};
2121
use cargo::util::CliError;
2222

2323
#[derive(RustcDecodable)]
@@ -84,7 +84,9 @@ fn main() {
8484
let result = (|| {
8585
let args: Vec<_> = try!(env::args_os()
8686
.map(|s| {
87-
s.into_string().map_err(|s| human(format!("invalid unicode in argument: {:?}", s)))
87+
s.into_string().map_err(|s| {
88+
CargoError::from(format!("invalid unicode in argument: {:?}", s))
89+
})
8890
})
8991
.collect());
9092
let rest = &args;
@@ -180,7 +182,7 @@ fn execute(flags: Flags, config: &Config) -> CliResult {
180182

181183
if let Some(ref code) = flags.flag_explain {
182184
let mut procss = config.rustc()?.process();
183-
procss.arg("--explain").arg(code).exec().map_err(human)?;
185+
procss.arg("--explain").arg(code).exec()?;
184186
return Ok(());
185187
}
186188

@@ -309,7 +311,7 @@ fn execute_external_subcommand(config: &Config, cmd: &str, args: &[String]) -> C
309311
let command = match path {
310312
Some(command) => command,
311313
None => {
312-
return Err(human(match find_closest(config, cmd) {
314+
return Err(CargoError::from(match find_closest(config, cmd) {
313315
Some(closest) => {
314316
format!("no such subcommand: `{}`\n\n\tDid you mean `{}`?\n",
315317
cmd,
@@ -330,11 +332,12 @@ fn execute_external_subcommand(config: &Config, cmd: &str, args: &[String]) -> C
330332
Err(e) => e,
331333
};
332334

333-
if let Some(code) = err.exit.as_ref().and_then(|c| c.code()) {
334-
Err(CliError::code(code))
335-
} else {
336-
Err(CliError::new(Box::new(err), 101))
335+
if let &CargoErrorKind::ProcessErrorKind(ref perr) = err.kind() {
336+
if let Some(code) = perr.exit.as_ref().and_then(|c| c.code()) {
337+
return Err(CliError::code(code));
338+
}
337339
}
340+
Err(CliError::new(err, 101))
338341
}
339342

340343
/// List all runnable commands

src/bin/help.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use cargo::util::{CliResult, CliError, Config, human};
1+
use cargo::util::{CliResult, CliError, Config};
22

33
#[derive(RustcDecodable)]
44
pub struct Options;
@@ -18,5 +18,5 @@ pub fn execute(_: Options, _: &Config) -> CliResult {
1818
// This is a dummy command just so that `cargo help help` works.
1919
// The actual delegation of help flag to subcommands is handled by the
2020
// cargo command.
21-
Err(CliError::new(human("help command should not be executed directly"), 101))
21+
Err(CliError::new("help command should not be executed directly".into(), 101))
2222
}

src/bin/locate_project.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use cargo;
2-
use cargo::util::{CliResult, CliError, human, ChainError, Config};
2+
use cargo::util::{CliResult, CliError, Config};
33
use cargo::util::important_paths::{find_root_manifest_for_wd};
44

55
#[derive(RustcDecodable)]
@@ -28,9 +28,9 @@ pub fn execute(flags: LocateProjectFlags,
2828
let root = find_root_manifest_for_wd(flags.flag_manifest_path, config.cwd())?;
2929

3030
let string = root.to_str()
31-
.chain_error(|| human("Your project path contains \
31+
.ok_or_else(|| "Your project path contains \
3232
characters not representable in \
33-
Unicode"))
33+
Unicode".into())
3434
.map_err(|e| CliError::new(e, 1))?;
3535

3636
let location = ProjectLocation { root: string.to_string() };

src/bin/login.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::io;
44
use cargo::ops;
55
use cargo::core::{SourceId, Source};
66
use cargo::sources::RegistrySource;
7-
use cargo::util::{CliResult, Config, human, ChainError};
7+
use cargo::util::{CliResult, CargoResultExt, Config};
88

99
#[derive(RustcDecodable)]
1010
pub struct Options {
@@ -51,8 +51,8 @@ pub fn execute(options: Options, config: &Config) -> CliResult {
5151
println!("please visit {}me and paste the API Token below", host);
5252
let mut line = String::new();
5353
let input = io::stdin();
54-
input.lock().read_line(&mut line).chain_error(|| {
55-
human("failed to read stdin")
54+
input.lock().read_line(&mut line).chain_err(|| {
55+
"failed to read stdin"
5656
})?;
5757
line
5858
}

src/bin/run.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::iter::FromIterator;
22

33
use cargo::core::Workspace;
44
use cargo::ops::{self, MessageFormat, Packages};
5-
use cargo::util::{CliResult, CliError, Config, Human};
5+
use cargo::util::{CliResult, CliError, Config, CargoErrorKind};
66
use cargo::util::important_paths::{find_root_manifest_for_wd};
77

88
#[derive(RustcDecodable)]
@@ -113,7 +113,8 @@ pub fn execute(options: Options, config: &Config) -> CliResult {
113113
// bad and we always want to forward that up.
114114
let exit = match err.exit.clone() {
115115
Some(exit) => exit,
116-
None => return Err(CliError::new(Box::new(Human(err)), 101)),
116+
None => return Err(
117+
CliError::new(CargoErrorKind::ProcessErrorKind(err).into(), 101)),
117118
};
118119

119120
// If `-q` was passed then we suppress extra error information about
@@ -123,7 +124,7 @@ pub fn execute(options: Options, config: &Config) -> CliResult {
123124
Err(if options.flag_quiet == Some(true) {
124125
CliError::code(exit_code)
125126
} else {
126-
CliError::new(Box::new(Human(err)), exit_code)
127+
CliError::new(CargoErrorKind::ProcessErrorKind(err).into(), exit_code)
127128
})
128129
}
129130
}

src/bin/rustc.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::env;
33
use cargo::core::Workspace;
44
use cargo::ops::{self, CompileOptions, CompileMode, MessageFormat, Packages};
55
use cargo::util::important_paths::{find_root_manifest_for_wd};
6-
use cargo::util::{CliResult, CliError, Config, human};
6+
use cargo::util::{CliResult, CliError, Config};
77

88
#[derive(RustcDecodable)]
99
pub struct Options {
@@ -98,8 +98,8 @@ pub fn execute(options: Options, config: &Config) -> CliResult {
9898
Some("bench") => CompileMode::Bench,
9999
Some("check") => CompileMode::Check,
100100
Some(mode) => {
101-
let err = human(format!("unknown profile: `{}`, use dev,
102-
test, or bench", mode));
101+
let err = format!("unknown profile: `{}`, use dev,
102+
test, or bench", mode).into();
103103
return Err(CliError::new(err, 101))
104104
}
105105
};

src/bin/test.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use cargo::core::Workspace;
22
use cargo::ops::{self, MessageFormat, Packages};
3-
use cargo::util::{CliResult, CliError, Human, human, Config};
3+
use cargo::util::{CliResult, CliError, Config, CargoErrorKind};
44
use cargo::util::important_paths::find_root_manifest_for_wd;
55

66
#[derive(RustcDecodable)]
@@ -163,8 +163,8 @@ pub fn execute(options: Options, config: &Config) -> CliResult {
163163
None => Ok(()),
164164
Some(err) => {
165165
Err(match err.exit.as_ref().and_then(|e| e.code()) {
166-
Some(i) => CliError::new(human(err.hint()), i),
167-
None => CliError::new(Box::new(Human(err)), 101),
166+
Some(i) => CliError::new(err.hint().into(), i),
167+
None => CliError::new(CargoErrorKind::CargoTestErrorKind(err).into(), 101),
168168
})
169169
}
170170
}

src/cargo/core/dependency.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ use semver::ReqParseError;
77
use serde::ser;
88

99
use core::{SourceId, Summary, PackageId};
10-
use util::{CargoError, CargoResult, Cfg, CfgExpr, ChainError, human, Config};
10+
use util::{Cfg, CfgExpr, Config};
11+
use util::errors::{CargoResult, CargoResultExt, CargoError};
1112

1213
/// Information about a dependency requested by a Cargo manifest.
1314
/// Cheap to copy.
@@ -145,7 +146,7 @@ this warning.
145146

146147
Ok(requirement)
147148
}
148-
e => Err(From::from(e)),
149+
e => Err(e.into()),
149150
}
150151
},
151152
Ok(v) => Ok(v),
@@ -361,13 +362,13 @@ impl ser::Serialize for Platform {
361362
}
362363

363364
impl FromStr for Platform {
364-
type Err = Box<CargoError>;
365+
type Err = CargoError;
365366

366367
fn from_str(s: &str) -> CargoResult<Platform> {
367368
if s.starts_with("cfg(") && s.ends_with(")") {
368369
let s = &s[4..s.len()-1];
369-
s.parse().map(Platform::Cfg).chain_error(|| {
370-
human(format!("failed to parse `{}` as a cfg expression", s))
370+
s.parse().map(Platform::Cfg).chain_err(|| {
371+
format!("failed to parse `{}` as a cfg expression", s)
371372
})
372373
} else {
373374
Ok(Platform::Name(s.to_string()))

src/cargo/core/package.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ use toml;
1111
use core::{Dependency, Manifest, PackageId, SourceId, Target};
1212
use core::{Summary, SourceMap};
1313
use ops;
14-
use util::{CargoResult, Config, LazyCell, ChainError, internal, human, lev_distance};
14+
use util::{Config, LazyCell, internal, lev_distance};
15+
use util::errors::{CargoResult, CargoResultExt};
1516

1617
/// Information about a package that is available somewhere in the file system.
1718
///
@@ -181,19 +182,19 @@ impl<'cfg> PackageSet<'cfg> {
181182
}
182183

183184
pub fn get(&self, id: &PackageId) -> CargoResult<&Package> {
184-
let slot = self.packages.iter().find(|p| p.0 == *id).chain_error(|| {
185+
let slot = self.packages.iter().find(|p| p.0 == *id).ok_or_else(|| {
185186
internal(format!("couldn't find `{}` in package set", id))
186187
})?;
187188
let slot = &slot.1;
188189
if let Some(pkg) = slot.borrow() {
189190
return Ok(pkg)
190191
}
191192
let mut sources = self.sources.borrow_mut();
192-
let source = sources.get_mut(id.source_id()).chain_error(|| {
193+
let source = sources.get_mut(id.source_id()).ok_or_else(|| {
193194
internal(format!("couldn't find source for `{}`", id))
194195
})?;
195-
let pkg = source.download(id).chain_error(|| {
196-
human("unable to get packages from source")
196+
let pkg = source.download(id).chain_err(|| {
197+
"unable to get packages from source"
197198
})?;
198199
assert!(slot.fill(pkg).is_ok());
199200
Ok(slot.borrow().unwrap())

0 commit comments

Comments
 (0)