From 84ebc0039e82ec63a8fb2536ad4999cf8fd1f72f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fe=CC=81lix=20Saparelli?= Date: Tue, 31 May 2022 23:01:12 +1200 Subject: [PATCH] Pretty-print errors --- src/errors.rs | 7 ++++--- src/main.rs | 23 +++++++++++++---------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/errors.rs b/src/errors.rs index 544272dc..e6e4ab94 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -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 } } diff --git a/src/main.rs b/src/main.rs index 2fe56c1b..fa70206d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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::() { - 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::() + .map(MainExit::Error) + .unwrap_or_else(MainExit::Report) + }) } async fn entry() -> Result<()> {