Nicely print CLI errors with their causes

This commit is contained in:
timvisee 2018-03-27 23:31:25 +02:00
parent a4b7736019
commit 8ae79c3e4f
No known key found for this signature in database
GPG key ID: 109CBA0BF74036C2
6 changed files with 82 additions and 50 deletions

View file

@ -1,6 +1,6 @@
#[cfg(feature = "clipboard")]
extern crate clipboard;
extern crate failure;
extern crate colored;
extern crate open;
#[cfg(feature = "clipboard")]
@ -11,14 +11,33 @@ use std::process::{exit, ExitStatus};
#[cfg(feature = "clipboard")]
use self::clipboard::{ClipboardContext, ClipboardProvider};
use self::failure::{Fail};
use self::colored::*;
use failure::{self, Fail};
use ffsend_api::url::Url;
/// Print the given error in a proper format for the user,
/// with it's causes.
pub fn print_error<E: Fail>(err: E) {
// Print the main error
eprintln!("{} {}", "error:".red().bold(), err);
// Print the causes
let mut cause = err.cause();
while let Some(err) = cause {
eprintln!("{} {}", "caused by:".red().bold(), err);
cause = err.cause();
}
}
/// Quit the application with an error code,
/// and print the given error.
pub fn quit_error<E: Fail>(err: E) -> ! {
// Print the error message
eprintln!("error: {}", err);
// Print the error
print_error(err);
// Print some additional information
eprintln!("\nFor detailed errors try '{}'", "--verbose".yellow());
eprintln!("For more information try '{}'", "--help".yellow());
// Quit
exit(1);