Skip to content

Commit

Permalink
Generalize abnormal exit
Browse files Browse the repository at this point in the history
Replace "fatal()" with a more general implementation that can support a
wider scope of abnormal program terminations.
  • Loading branch information
commonquail committed Oct 15, 2023
1 parent 421e2cb commit 3536492
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ Some text is exempt from wrapping:

let cfg = Config::new(&m);
if let Err(ref e) = cfg {
fatal(e);
exit_abnormally(e);
}
let cfg = cfg.unwrap();

Expand All @@ -166,16 +166,8 @@ Some text is exempt from wrapping:
.map(|text| commitmsgfmt.filter(&text))
.and_then(to_stdout);

match result {
Ok(()) => (),
Err(ref err) => match *err {
CliError::Io(ref e) if e.kind() == io::ErrorKind::BrokenPipe => {
std::process::exit(141 /* 128 + 13 */);
}
_ => {
fatal(err);
}
},
if let Err(ref e) = result {
exit_abnormally(e);
}
}

Expand Down Expand Up @@ -207,9 +199,19 @@ fn to_stdout<'a>(msg: String) -> CliResult<'a, ()> {
Ok(())
}

fn fatal(e: &CliError) {
eprintln!("fatal: {}", e);
::std::process::exit(1);
fn exit_abnormally(e: &CliError) {
let ret = match e {
CliError::Io(ref e) if e.kind() == io::ErrorKind::BrokenPipe => {
let ret = 128 + 13;
debug_assert!(ret == 141);
ret
}
_ => {
eprintln!("fatal: {}", e);
1
}
};
::std::process::exit(ret);
}

#[cfg(test)]
Expand Down

0 comments on commit 3536492

Please sign in to comment.