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 std::process::{ExitCode, Termination};
use log::warn; use log::warn;
use miette::Diagnostic; use miette::{Report, Diagnostic};
use thiserror::Error; use thiserror::Error;
/// Errors emitted by the library portion of cargo-binstall. /// Errors emitted by the library portion of cargo-binstall.
@ -182,12 +182,13 @@ impl BinstallError {
impl Termination for BinstallError { impl Termination for BinstallError {
fn report(self) -> ExitCode { fn report(self) -> ExitCode {
let code = self.exit_code();
if let BinstallError::UserAbort = self { if let BinstallError::UserAbort = self {
warn!("Installation cancelled"); warn!("Installation cancelled");
} else { } 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 { fn report(self) -> ExitCode {
match self { match self {
Self::Success(spent) => { Self::Success(spent) => {
info!("Installation complete! [{spent:?}]"); info!("Installation completed in {spent:?}");
ExitCode::SUCCESS ExitCode::SUCCESS
} }
Self::Error(err) => err.report(), Self::Error(err) => {
error!("Fatal error:");
err.report()
}
Self::Report(err) => { Self::Report(err) => {
error!("Fatal error:");
eprintln!("{err:?}"); eprintln!("{err:?}");
ExitCode::from(16) ExitCode::from(16)
} }
@ -112,14 +116,13 @@ fn main() -> MainExit {
let done = start.elapsed(); let done = start.elapsed();
debug!("run time: {done:?}"); debug!("run time: {done:?}");
if let Err(err) = result { result
match err.downcast::<BinstallError>() { .map(|_| MainExit::Success(done))
Ok(liberr) => MainExit::Error(liberr), .unwrap_or_else(|err| {
Err(binerr) => MainExit::Report(binerr), err.downcast::<BinstallError>()
} .map(MainExit::Error)
} else { .unwrap_or_else(MainExit::Report)
MainExit::Success(done) })
}
} }
async fn entry() -> Result<()> { async fn entry() -> Result<()> {