Skip to content

Commit

Permalink
fix: correct pre-color loading error handling
Browse files Browse the repository at this point in the history
If we fail before successfully loading the colors, we can't use them to
print the error message.

Gritty previously panicked without providing a meaningful error message,
because the global OnceLock `log::STYLES` has not been set.
Thus, print the error message without colors and exit
with status code 1.
  • Loading branch information
benpueschel committed Aug 6, 2024
1 parent b828443 commit c63ed29
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::Display;

use args::{Args, Commands};
use clap::Parser;
use config::Config;
Expand Down Expand Up @@ -29,14 +31,28 @@ fn load_config(path: &Option<String>) -> Result<Config> {
}
}

/// If we fail before successfully loading the colors, we can't use them to print the error
/// message. In that case, we print the error message without colors and exit with status code 1.
fn exit_no_color(err: impl Display) -> ! {
eprintln!("{}", err);
std::process::exit(1);
}

async fn execute_command(args: Args) -> Result<()> {
if let Commands::CreateConfig = args.subcommand {
log::load_default_colors()?;
if let Err(err) = log::load_default_colors() {
exit_no_color(err);
}
return commands::create_config(&args.config).await;
}

let mut config = load_config(&args.config)?;
log::load_colors(&config)?;
let mut config = match load_config(&args.config) {
Ok(x) => x,
Err(err) => exit_no_color(err),
};
if let Err(err) = log::load_colors(&config) {
exit_no_color(err);
}

// Execute the sub-command
match args.subcommand {
Expand Down

0 comments on commit c63ed29

Please sign in to comment.