diff --git a/src/errors.rs b/src/errors.rs index 6be71942..f8451ee3 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -3,6 +3,7 @@ use std::process::{ExitCode, Termination}; use log::{error, warn}; use miette::{Diagnostic, Report}; use thiserror::Error; +use tokio::task; /// Errors emitted by the library portion of cargo-binstall. #[derive(Error, Diagnostic, Debug)] @@ -185,6 +186,10 @@ pub enum BinstallError { help("Remove the `--manifest-path` or only specify one `$crate_name`") )] ManifestPathConflictedWithBatchInstallation, + + #[error("Failed to join tokio::task::JoinHandle")] + #[diagnostic(severity(error), code(binstall::join_error))] + TaskJoinError(#[from] task::JoinError), } impl BinstallError { @@ -213,6 +218,7 @@ impl BinstallError { VersionUnavailable { .. } => 83, DuplicateVersionReq => 84, ManifestPathConflictedWithBatchInstallation => 85, + TaskJoinError(_) => 17, }; // reserved codes diff --git a/src/helpers.rs b/src/helpers.rs index 3381c839..a1ef3c5f 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -53,7 +53,7 @@ pub fn cargo_home() -> Result<&'static Path, io::Error> { pub async fn await_task(task: tokio::task::JoinHandle>) -> miette::Result { match task.await { Ok(res) => res, - Err(join_err) => Err(miette::miette!("Task failed to join: {}", join_err)), + Err(join_err) => Err(BinstallError::from(join_err).into()), } } diff --git a/src/main.rs b/src/main.rs index 31daa6fa..f6759f6a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -133,7 +133,6 @@ enum MainExit { Success(Duration), Error(BinstallError), Report(miette::Report), - JoinErr(JoinError), } impl Termination for MainExit { @@ -149,11 +148,6 @@ impl Termination for MainExit { eprintln!("{err:?}"); ExitCode::from(16) } - Self::JoinErr(err) => { - error!("Fatal error:"); - eprintln!("{err:?}"); - ExitCode::from(17) - } } } } @@ -172,13 +166,16 @@ fn main() -> MainExit { let done = start.elapsed(); debug!("run time: {done:?}"); - result.map_or_else(MainExit::JoinErr, |res| { - res.map(|_| MainExit::Success(done)).unwrap_or_else(|err| { - err.downcast::() - .map(MainExit::Error) - .unwrap_or_else(MainExit::Report) - }) - }) + result.map_or_else( + |join_err| MainExit::Error(BinstallError::from(join_err)), + |res| { + res.map(|_| MainExit::Success(done)).unwrap_or_else(|err| { + err.downcast::() + .map(MainExit::Error) + .unwrap_or_else(MainExit::Report) + }) + }, + ) } async fn entry(jobserver_client: LazyJobserverClient) -> Result<()> {