mirror of
https://github.com/timvisee/ffsend.git
synced 2025-10-06 02:29:57 +02:00
Add CLI error structs, improve routing and error reporting from API
This commit is contained in:
parent
affa6e65d2
commit
b2e63b9efc
8 changed files with 61 additions and 19 deletions
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue