From 2cd133b9fbcab21ad8f938b6512cb186630cd74b Mon Sep 17 00:00:00 2001 From: Andrew Langmeier Date: Sun, 30 Jul 2023 08:29:29 -0400 Subject: [PATCH] Fixes #2250; Error instead of panic when root directory or config file not present (#2262) --- src/main.rs | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 983f7b2a..a65af390 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,18 +26,27 @@ fn get_config_file_path(dir: &Path, config_path: &Path) -> (PathBuf, PathBuf) { std::process::exit(1); }); - // if we got here we found root_dir so config file should exist so we can unwrap safely - let config_file = root_dir - .join(config_path) - .canonicalize() - .unwrap_or_else(|_| panic!("could not find directory containing config file")); + // if we got here we found root_dir so config file should exist so we could theoretically unwrap safely + let config_file_uncanonicalized = root_dir.join(config_path); + let config_file = config_file_uncanonicalized.canonicalize().unwrap_or_else(|e| { + messages::unravel_errors( + &format!("Could not find canonical path of {}", config_file_uncanonicalized.display()), + &e.into(), + ); + std::process::exit(1); + }); + (root_dir.to_path_buf(), config_file) } fn main() { let cli = Cli::parse(); - let cli_dir: PathBuf = cli.root.canonicalize().unwrap_or_else(|_| { - panic!("Could not find canonical path of root dir: {}", cli.root.display()) + let cli_dir: PathBuf = cli.root.canonicalize().unwrap_or_else(|e| { + messages::unravel_errors( + &format!("Could not find canonical path of root dir: {}", cli.root.display()), + &e.into(), + ); + std::process::exit(1); }); match cli.command {