Skip to content

Commit

Permalink
Auto merge of #2666 - matklad:simplify, r=alexcrichton
Browse files Browse the repository at this point in the history
Simplify error printing
  • Loading branch information
bors committed May 9, 2016
2 parents b304305 + 61cb937 commit d6e1726
Showing 1 changed file with 11 additions and 22 deletions.
33 changes: 11 additions & 22 deletions src/cargo/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use docopt::Docopt;
use core::{Shell, MultiShell, ShellConfig, Verbosity, ColorConfig};
use core::shell::Verbosity::{Verbose};
use core::shell::ColorConfig::{Auto};
use term::color::{BLACK, RED};
use term::color::{BLACK};

pub use util::{CargoError, CliError, CliResult, human, Config, ChainError};

Expand Down Expand Up @@ -178,34 +178,23 @@ pub fn shell(verbosity: Verbosity, color_config: ColorConfig) -> MultiShell {
}
}

// `output` print variant error strings to either stderr or stdout.
// For fatal errors, print to stderr;
// and for others, e.g. docopt version info, print to stdout.
fn output(err: String, shell: &mut MultiShell, fatal: bool) {
let (std_shell, color, message) = if fatal {
(shell.err(), RED, Some("error:"))
} else {
(shell.out(), BLACK, None)
};
let _ = match message{
Some(text) => std_shell.say_status(text, err.to_string(), color, false),
None => std_shell.say(err, color)
};
}

pub fn handle_error(err: CliError, shell: &mut MultiShell) {
debug!("handle_error; err={:?}", err);

let CliError { error, exit_code, unknown } = err;
let fatal = exit_code != 0; // exit_code == 0 is non-fatal error
// exit_code == 0 is non-fatal error, e.g. docopt version info
let fatal = exit_code != 0;

let hide = unknown && shell.get_verbose() != Verbose;
if hide {
let _ = shell.err().say_status("error:", "An unknown error occurred",
RED, false);

let _ignored_result = if hide {
shell.error("An unknown error occurred")
} else if fatal {
shell.error(&error)
} else {
output(error.to_string(), shell, fatal);
}
shell.say(&error, BLACK)
};

if !handle_cause(&error, shell) || hide {
let _ = shell.err().say("\nTo learn more, run the command again \
with --verbose.".to_string(), BLACK);
Expand Down

0 comments on commit d6e1726

Please sign in to comment.