Add CLI error structs, improve routing and error reporting from API

This commit is contained in:
timvisee 2018-03-28 01:25:34 +02:00
parent affa6e65d2
commit b2e63b9efc
No known key found for this signature in database
GPG key ID: 109CBA0BF74036C2
8 changed files with 61 additions and 19 deletions

View file

@ -18,14 +18,21 @@ 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);
// Report each printable error, count them
let count = err.causes()
.map(|err| format!("{}", err))
.filter(|err| !err.is_empty())
.enumerate()
.map(|(i, err)| if i == 0 {
eprintln!("{} {}", "error:".red().bold(), err);
} else {
eprintln!("{} {}", "caused by:".red().bold(), err);
})
.count();
// Print the causes
let mut cause = err.cause();
while let Some(err) = cause {
eprintln!("{} {}", "caused by:".red().bold(), err);
cause = err.cause();
// Fall back to a basic message
if count == 0 {
eprintln!("{} {}", "error:".red().bold(), "An undefined error occurred");
}
}