Unwind stack for cli errors

This commit is contained in:
Aleksey Kladov 2018-03-09 10:43:00 +03:00
parent f214564552
commit 16bde4e0ae
5 changed files with 23 additions and 10 deletions

View File

@ -31,18 +31,18 @@ fn main() {
}
};
match main_inner(&mut config) {
let result = {
init_git_transports(&mut config);
let _token = cargo::util::job::setup();
cli::do_main(&mut config)
};
match result {
Err(e) => cargo::exit_with_error(e, &mut *config.shell()),
Ok(()) => {}
}
}
fn main_inner(config: &mut Config) -> CliResult {
init_git_transports(config);
let _token = cargo::util::job::setup();
cli::do_main(config)
}
fn aliased_command(config: &Config, command: &str) -> CargoResult<Option<Vec<String>>> {
let alias_name = format!("alias.{}", command);
let mut result = Ok(None);

View File

@ -29,7 +29,7 @@ use search_directories;
use is_executable;
pub fn do_main(config: &mut Config) -> CliResult {
let args = cli().get_matches();
let args = cli().get_matches_safe()?;
let is_verbose = args.occurrences_of("verbose") > 0;
if args.is_present("version") {
let version = cargo::version();
@ -547,7 +547,7 @@ fn execute_subcommand(config: &mut Config, args: ArgMatches) -> CliResult {
alias.extend(args.values_of("").unwrap_or_default().map(|s| s.to_string()));
let args = cli()
.setting(AppSettings::NoBinaryName)
.get_matches_from(alias);
.get_matches_from_safe(alias)?;
return execute_subcommand(config, args);
}
let mut ext_args: Vec<&str> = vec![cmd];

View File

@ -13,6 +13,7 @@
#[macro_use] extern crate serde_derive;
#[macro_use] extern crate serde_json;
extern crate atty;
extern crate clap;
extern crate crates_io as registry;
extern crate crossbeam;
extern crate curl;
@ -113,6 +114,11 @@ pub fn print_json<T: ser::Serialize>(obj: &T) {
pub fn exit_with_error(err: CliError, shell: &mut Shell) -> ! {
debug!("exit_with_error; err={:?}", err);
if let Some(ref err) = err.error {
if let Some(clap_err) = err.downcast_ref::<clap::Error>() {
clap_err.exit()
}
}
let CliError { error, exit_code, unknown } = err;
// exit_code == 0 is non-fatal error, e.g. docopt version info

View File

@ -6,6 +6,7 @@ use std::str;
use core::{TargetKind, Workspace};
use failure::{Context, Error, Fail};
use clap;
pub use failure::Error as CargoError;
pub type CargoResult<T> = Result<T, Error>;
@ -169,6 +170,13 @@ impl From<CargoError> for CliError {
}
}
impl From<clap::Error> for CliError {
fn from(err: clap::Error) -> CliError {
let code = if err.use_stderr() { 1 } else { 0 };
CliError::new(err.into(), code)
}
}
// =============================================================================
// Construction helpers

View File

@ -3,7 +3,6 @@ use std::fs::{self, File, OpenOptions};
use std::io::prelude::*;
use cargo::util::ProcessBuilder;
use cargotest::ChannelChanger;
use cargotest::install::{cargo_home, has_installed_exe};
use cargotest::support::git;
use cargotest::support::paths;