Merge pull request #223 from NobodyXu/fix/join-err

Fix: join err handling, unify it using `BinstallError`
This commit is contained in:
Jiahao XU 2022-07-22 11:29:21 +10:00 committed by GitHub
commit 6964eee5d1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 14 deletions

View file

@ -3,6 +3,7 @@ use std::process::{ExitCode, Termination};
use log::{error, warn}; use log::{error, warn};
use miette::{Diagnostic, Report}; use miette::{Diagnostic, Report};
use thiserror::Error; use thiserror::Error;
use tokio::task;
/// Errors emitted by the library portion of cargo-binstall. /// Errors emitted by the library portion of cargo-binstall.
#[derive(Error, Diagnostic, Debug)] #[derive(Error, Diagnostic, Debug)]
@ -185,6 +186,10 @@ pub enum BinstallError {
help("Remove the `--manifest-path` or only specify one `$crate_name`") help("Remove the `--manifest-path` or only specify one `$crate_name`")
)] )]
ManifestPathConflictedWithBatchInstallation, ManifestPathConflictedWithBatchInstallation,
#[error("Failed to join tokio::task::JoinHandle")]
#[diagnostic(severity(error), code(binstall::join_error))]
TaskJoinError(#[from] task::JoinError),
} }
impl BinstallError { impl BinstallError {
@ -213,6 +218,7 @@ impl BinstallError {
VersionUnavailable { .. } => 83, VersionUnavailable { .. } => 83,
DuplicateVersionReq => 84, DuplicateVersionReq => 84,
ManifestPathConflictedWithBatchInstallation => 85, ManifestPathConflictedWithBatchInstallation => 85,
TaskJoinError(_) => 17,
}; };
// reserved codes // reserved codes

View file

@ -53,7 +53,7 @@ pub fn cargo_home() -> Result<&'static Path, io::Error> {
pub async fn await_task<T>(task: tokio::task::JoinHandle<miette::Result<T>>) -> miette::Result<T> { pub async fn await_task<T>(task: tokio::task::JoinHandle<miette::Result<T>>) -> miette::Result<T> {
match task.await { match task.await {
Ok(res) => res, Ok(res) => res,
Err(join_err) => Err(miette::miette!("Task failed to join: {}", join_err)), Err(join_err) => Err(BinstallError::from(join_err).into()),
} }
} }

View file

@ -133,7 +133,6 @@ enum MainExit {
Success(Duration), Success(Duration),
Error(BinstallError), Error(BinstallError),
Report(miette::Report), Report(miette::Report),
JoinErr(JoinError),
} }
impl Termination for MainExit { impl Termination for MainExit {
@ -149,11 +148,6 @@ impl Termination for MainExit {
eprintln!("{err:?}"); eprintln!("{err:?}");
ExitCode::from(16) 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(); let done = start.elapsed();
debug!("run time: {done:?}"); debug!("run time: {done:?}");
result.map_or_else(MainExit::JoinErr, |res| { result.map_or_else(
res.map(|_| MainExit::Success(done)).unwrap_or_else(|err| { |join_err| MainExit::Error(BinstallError::from(join_err)),
err.downcast::<BinstallError>() |res| {
.map(MainExit::Error) res.map(|_| MainExit::Success(done)).unwrap_or_else(|err| {
.unwrap_or_else(MainExit::Report) err.downcast::<BinstallError>()
}) .map(MainExit::Error)
}) .unwrap_or_else(MainExit::Report)
})
},
)
} }
async fn entry(jobserver_client: LazyJobserverClient) -> Result<()> { async fn entry(jobserver_client: LazyJobserverClient) -> Result<()> {