Add fatal() function to handle exiting on errors

Panic when testing or exit with return value 2.
This commit is contained in:
Thomas Otto 2021-10-14 00:15:56 +02:00
parent 85ba3ba578
commit 636b56729b
2 changed files with 15 additions and 4 deletions

View File

@ -13,6 +13,7 @@ use crate::cli;
use crate::color;
use crate::delta::State;
use crate::env;
use crate::fatal;
use crate::features::navigate;
use crate::features::side_by_side;
use crate::git_config::{GitConfig, GitConfigEntry};
@ -193,10 +194,7 @@ impl From<cli::Opt> for Config {
// Note that "default" is not documented
Some("ansi") | Some("default") | None => BgFillMethod::TryAnsiSequence,
Some("spaces") => BgFillMethod::Spaces,
_ => {
eprintln!("Invalid option for line-fill-method: Expected \"ansi\" or \"spaces\".");
process::exit(1);
}
_ => fatal("Invalid option for line-fill-method: Expected \"ansi\" or \"spaces\"."),
};
let navigate_regexp = if opt.navigate || opt.show_themes {

View File

@ -35,6 +35,19 @@ use crate::bat_utils::assets::{list_languages, HighlightingAssets};
use crate::bat_utils::output::OutputType;
use crate::delta::delta;
pub fn fatal<T>(errmsg: T) -> !
where
T: AsRef<str> + std::fmt::Display,
{
#[cfg(not(test))]
{
eprintln!("{}", errmsg);
process::exit(2);
}
#[cfg(test)]
panic!("{}\n", errmsg);
}
pub mod errors {
error_chain! {
foreign_links {