Pretty-print errors

This commit is contained in:
Félix Saparelli 2022-05-31 23:01:12 +12:00
parent 02c8c0af00
commit 84ebc0039e
No known key found for this signature in database
GPG key ID: B948C4BAE44FC474
2 changed files with 17 additions and 13 deletions

View file

@ -1,7 +1,7 @@
use std::process::{ExitCode, Termination};
use log::warn;
use miette::Diagnostic;
use miette::{Report, Diagnostic};
use thiserror::Error;
/// Errors emitted by the library portion of cargo-binstall.
@ -182,12 +182,13 @@ impl BinstallError {
impl Termination for BinstallError {
fn report(self) -> ExitCode {
let code = self.exit_code();
if let BinstallError::UserAbort = self {
warn!("Installation cancelled");
} else {
eprintln!("{self:?}");
eprintln!("{:?}", Report::new(self));
}
self.exit_code()
code
}
}

View file

@ -90,11 +90,15 @@ impl Termination for MainExit {
fn report(self) -> ExitCode {
match self {
Self::Success(spent) => {
info!("Installation complete! [{spent:?}]");
info!("Installation completed in {spent:?}");
ExitCode::SUCCESS
}
Self::Error(err) => err.report(),
Self::Error(err) => {
error!("Fatal error:");
err.report()
}
Self::Report(err) => {
error!("Fatal error:");
eprintln!("{err:?}");
ExitCode::from(16)
}
@ -112,14 +116,13 @@ fn main() -> MainExit {
let done = start.elapsed();
debug!("run time: {done:?}");
if let Err(err) = result {
match err.downcast::<BinstallError>() {
Ok(liberr) => MainExit::Error(liberr),
Err(binerr) => MainExit::Report(binerr),
}
} else {
MainExit::Success(done)
}
result
.map(|_| MainExit::Success(done))
.unwrap_or_else(|err| {
err.downcast::<BinstallError>()
.map(MainExit::Error)
.unwrap_or_else(MainExit::Report)
})
}
async fn entry() -> Result<()> {