mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-04-21 13:08:42 +00:00
Merge pull request #223 from NobodyXu/fix/join-err
Fix: join err handling, unify it using `BinstallError`
This commit is contained in:
commit
6964eee5d1
3 changed files with 17 additions and 14 deletions
|
@ -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
|
||||
|
|
|
@ -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> {
|
||||
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()),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
13
src/main.rs
13
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| {
|
||||
result.map_or_else(
|
||||
|join_err| MainExit::Error(BinstallError::from(join_err)),
|
||||
|res| {
|
||||
res.map(|_| MainExit::Success(done)).unwrap_or_else(|err| {
|
||||
err.downcast::<BinstallError>()
|
||||
.map(MainExit::Error)
|
||||
.unwrap_or_else(MainExit::Report)
|
||||
})
|
||||
})
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
async fn entry(jobserver_client: LazyJobserverClient) -> Result<()> {
|
||||
|
|
Loading…
Add table
Reference in a new issue