Skip to content

Commit 6989632

Browse files
authored
Fix Error message outputs ANSI escape sequences (#2556)
Fixed #2554 --------- Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>
1 parent 618ca07 commit 6989632

2 files changed

Lines changed: 26 additions & 19 deletions

File tree

crates/bin/src/bin_util.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ use std::{
33
time::Duration,
44
};
55

6-
use binstalk::errors::BinstallError;
7-
use binstalk::helpers::tasks::AutoAbortJoinHandle;
6+
use binstalk::{errors::BinstallError, helpers::tasks::AutoAbortJoinHandle};
87
use miette::Result;
98
use tokio::runtime::Runtime;
10-
use tracing::{error, info};
9+
use tracing::{error, info, warn};
1110

1211
use crate::signal::cancel_on_user_sig_term;
1312

@@ -19,20 +18,28 @@ pub enum MainExit {
1918

2019
impl Termination for MainExit {
2120
fn report(self) -> ExitCode {
22-
match self {
21+
let (code, err) = match self {
2322
Self::Success(spent) => {
2423
if let Some(spent) = spent {
2524
info!("Done in {spent:?}");
2625
}
27-
ExitCode::SUCCESS
26+
return ExitCode::SUCCESS;
2827
}
29-
Self::Error(err) => err.report(),
30-
Self::Report(err) => {
31-
error!("Fatal error:");
32-
println!("{err:?}");
33-
ExitCode::from(16)
28+
Self::Error(err) => {
29+
let code = err.exit_code();
30+
let Some(report) = err.get_report() else {
31+
warn!("Installation cancelled");
32+
return code;
33+
};
34+
(code, report)
3435
}
35-
}
36+
Self::Report(err) => (ExitCode::from(16), err),
37+
};
38+
39+
error!("Fatal error:");
40+
println!("{err:?}");
41+
42+
code
3643
}
3744
}
3845

crates/binstalk/src/errors.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use itertools::Itertools;
1212
use miette::{Diagnostic, Report};
1313
use thiserror::Error;
1414
use tokio::task;
15-
use tracing::{error, warn};
1615

1716
use crate::{
1817
bins,
@@ -525,18 +524,19 @@ impl BinstallError {
525524
Some(Self::Errors(CrateErrors(errors.into_boxed_slice())))
526525
}
527526
}
528-
}
529527

530-
impl Termination for BinstallError {
531-
fn report(self) -> ExitCode {
532-
let code = self.exit_code();
528+
pub fn get_report(self) -> Option<Report> {
533529
if let BinstallError::UserAbort = self {
534-
warn!("Installation cancelled");
530+
None
535531
} else {
536-
error!("Fatal error:\n{:?}", Report::new(self));
532+
Some(Report::new(self))
537533
}
534+
}
535+
}
538536

539-
code
537+
impl Termination for BinstallError {
538+
fn report(self) -> ExitCode {
539+
self.exit_code()
540540
}
541541
}
542542

0 commit comments

Comments
 (0)