Skip to content

Commit 37cffbe

Browse files
committed
Start migration to the failure crate
This commit is the initial steps to migrate Cargo's error handling from the `error-chain` crate to the `failure` crate. This is intended to be a low-cost (in terms of diff) transition where possible so it's note "purely idiomatic `failure` crate" just yet. The `error-chain` dependency is dropped in this commit and Cargo now canonically uses the `Error` type from the `failure` crate. The main last remnant of `error-chain` is a custom local extension trait to use `chain_err` instead of `with_context`. I'll try to follow up with a commit that renames this later but I wanted to make sure everything worked first! (and `chain_err` is used practically everywhere). Some minor tweaks happened in the tests as I touched up a few error messages here and there but overall the UI of Cargo should be exactly the same before and after this commit.
1 parent 4bbfc70 commit 37cffbe

Some content is hidden

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

67 files changed

+511
-589
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ crypto-hash = "0.3"
2424
curl = "0.4.6"
2525
docopt = "0.8.1"
2626
env_logger = "0.4"
27-
error-chain = "0.11.0"
27+
failure = "0.1.1"
2828
filetime = "0.1"
2929
flate2 = "0.2"
3030
fs2 = "0.4"

src/bin/bench.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::env;
22

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

88
#[derive(Deserialize)]
@@ -144,8 +144,8 @@ pub fn execute(options: Options, config: &mut Config) -> CliResult {
144144
None => Ok(()),
145145
Some(err) => {
146146
Err(match err.exit.as_ref().and_then(|e| e.code()) {
147-
Some(i) => CliError::new("bench failed".into(), i),
148-
None => CliError::new(CargoErrorKind::CargoTestErrorKind(err).into(), 101)
147+
Some(i) => CliError::new(format_err!("bench failed"), i),
148+
None => CliError::new(err.into(), 101)
149149
})
150150
}
151151
}

src/bin/cargo.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
extern crate cargo;
22
extern crate env_logger;
3+
#[macro_use]
4+
extern crate failure;
35
extern crate git2_curl;
46
extern crate toml;
57
#[macro_use]
@@ -15,8 +17,8 @@ use std::fs;
1517
use std::path::{Path, PathBuf};
1618

1719
use cargo::core::shell::{Shell, Verbosity};
18-
use cargo::util::{self, CliResult, lev_distance, Config, CargoResult, CargoError, CargoErrorKind};
19-
use cargo::util::CliError;
20+
use cargo::util::{self, CliResult, lev_distance, Config, CargoResult};
21+
use cargo::util::{CliError, ProcessError};
2022

2123
#[derive(Deserialize)]
2224
pub struct Flags {
@@ -87,7 +89,7 @@ fn main() {
8789
let args: Vec<_> = try!(env::args_os()
8890
.map(|s| {
8991
s.into_string().map_err(|s| {
90-
CargoError::from(format!("invalid unicode in argument: {:?}", s))
92+
format_err!("invalid unicode in argument: {:?}", s)
9193
})
9294
})
9395
.collect());
@@ -315,15 +317,15 @@ fn execute_external_subcommand(config: &Config, cmd: &str, args: &[String]) -> C
315317
let command = match path {
316318
Some(command) => command,
317319
None => {
318-
return Err(CargoError::from(match find_closest(config, cmd) {
319-
Some(closest) => {
320-
format!("no such subcommand: `{}`\n\n\tDid you mean `{}`?\n",
320+
let err = match find_closest(config, cmd) {
321+
Some(closest) => {
322+
format_err!("no such subcommand: `{}`\n\n\tDid you mean `{}`?\n",
321323
cmd,
322324
closest)
323-
}
324-
None => format!("no such subcommand: `{}`", cmd),
325-
})
326-
.into())
325+
}
326+
None => format_err!("no such subcommand: `{}`", cmd),
327+
};
328+
return Err(CliError::new(err, 101))
327329
}
328330
};
329331

@@ -336,7 +338,7 @@ fn execute_external_subcommand(config: &Config, cmd: &str, args: &[String]) -> C
336338
Err(e) => e,
337339
};
338340

339-
if let &CargoErrorKind::ProcessErrorKind(ref perr) = err.kind() {
341+
if let Some(perr) = err.downcast_ref::<ProcessError>() {
340342
if let Some(code) = perr.exit.as_ref().and_then(|c| c.code()) {
341343
return Err(CliError::code(code));
342344
}

src/bin/check.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ pub fn execute(options: Options, config: &mut Config) -> CliResult {
114114
Some("test") => true,
115115
None => false,
116116
Some(profile) => {
117-
let err = format!("unknown profile: `{}`, only `test` is currently supported",
118-
profile).into();
117+
let err = format_err!("unknown profile: `{}`, only `test` is \
118+
currently supported", profile);
119119
return Err(CliError::new(err, 101))
120120
}
121121
};

src/bin/help.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ pub fn execute(_: Options, _: &mut 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("help command should not be executed directly".into(), 101))
21+
Err(CliError::new(format_err!("help command should not be executed directly"),
22+
101))
2223
}

src/bin/install.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use cargo::ops;
22
use cargo::core::{SourceId, GitReference};
3-
use cargo::util::{CargoError, CliResult, Config, ToUrl};
3+
use cargo::util::{CliResult, Config, ToUrl};
44

55
#[derive(Deserialize)]
66
pub struct Options {
@@ -154,7 +154,7 @@ pub fn execute(options: Options, config: &mut Config) -> CliResult {
154154

155155
let krates = options.arg_crate.iter().map(|s| &s[..]).collect::<Vec<_>>();
156156
let vers = match (&options.flag_vers, &options.flag_version) {
157-
(&Some(_), &Some(_)) => return Err(CargoError::from("Invalid arguments.").into()),
157+
(&Some(_), &Some(_)) => return Err(format_err!("invalid arguments").into()),
158158
(&Some(ref v), _) | (_, &Some(ref v)) => Some(v.as_ref()),
159159
_ => None,
160160
};

src/bin/locate_project.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ pub fn execute(flags: LocateProjectFlags, config: &mut Config) -> CliResult {
2727
let root = find_root_manifest_for_wd(flags.flag_manifest_path, config.cwd())?;
2828

2929
let string = root.to_str()
30-
.ok_or_else(|| "Your project path contains \
31-
characters not representable in \
32-
Unicode".into())
30+
.ok_or_else(|| format_err!("your project path contains \
31+
characters not representable in \
32+
Unicode"))
3333
.map_err(|e| CliError::new(e, 1))?;
3434

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

src/bin/login.rs

Lines changed: 6 additions & 4 deletions
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::{CargoError, CliResult, CargoResultExt, Config};
7+
use cargo::util::{CliResult, CargoResultExt, Config, CargoError};
88

99
#[derive(Deserialize)]
1010
pub struct Options {
@@ -48,15 +48,17 @@ pub fn execute(options: Options, config: &mut Config) -> CliResult {
4848
&options.flag_z)?;
4949

5050
if options.flag_registry.is_some() && !config.cli_unstable().unstable_options {
51-
return Err(CargoError::from("registry option is an unstable feature and requires -Zunstable-options to use.").into());
51+
return Err(format_err!("registry option is an unstable feature and \
52+
requires -Zunstable-options to use.").into());
5253
}
5354

5455
let token = match options.arg_token {
5556
Some(token) => token,
5657
None => {
5758
let host = match options.flag_registry {
5859
Some(ref _registry) => {
59-
return Err(CargoError::from("token must be provided when --registry is provided.").into());
60+
return Err(format_err!("token must be provided when \
61+
--registry is provided.").into())
6062
}
6163
None => {
6264
let src = SourceId::crates_io(config)?;
@@ -71,7 +73,7 @@ pub fn execute(options: Options, config: &mut Config) -> CliResult {
7173
let input = io::stdin();
7274
input.lock().read_line(&mut line).chain_err(|| {
7375
"failed to read stdin"
74-
})?;
76+
}).map_err(CargoError::from)?;
7577
line.trim().to_string()
7678
}
7779
};

src/bin/owner.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use cargo::ops;
2-
use cargo::util::{CargoError, CliResult, Config};
2+
use cargo::util::{CliResult, Config};
33

44
#[derive(Deserialize)]
55
pub struct Options {
@@ -67,7 +67,8 @@ pub fn execute(options: Options, config: &mut Config) -> CliResult {
6767
};
6868

6969
if opts.registry.is_some() && !config.cli_unstable().unstable_options {
70-
return Err(CargoError::from("registry option is an unstable feature and requires -Zunstable-options to use.").into());
70+
return Err(format_err!("registry option is an unstable feature and \
71+
requires -Zunstable-options to use.").into())
7172
}
7273

7374
ops::modify_owners(config, &opts)?;

src/bin/publish.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use cargo::core::Workspace;
22
use cargo::ops;
3-
use cargo::util::{CargoError, CliResult, Config};
3+
use cargo::util::{CliResult, Config};
44
use cargo::util::important_paths::find_root_manifest_for_wd;
55

66
#[derive(Deserialize)]
@@ -74,7 +74,8 @@ pub fn execute(options: Options, config: &mut Config) -> CliResult {
7474
} = options;
7575

7676
if registry.is_some() && !config.cli_unstable().unstable_options {
77-
return Err(CargoError::from("registry option is an unstable feature and requires -Zunstable-options to use.").into());
77+
return Err(format_err!("registry option is an unstable feature and \
78+
requires -Zunstable-options to use.").into())
7879
}
7980

8081
// TODO: Deprecated

0 commit comments

Comments
 (0)