From bf623aed0ddb610caf7105a765fbaa1ef26b1ac7 Mon Sep 17 00:00:00 2001 From: Ivin Joel Abraham Date: Sat, 15 Mar 2025 08:55:23 +0530 Subject: [PATCH] refactor: improve error handling and logging for config loading Added detailed error logging when reading and parsing configuration files to help diagnose configuration issues. Previously, file read errors were silently converted to empty strings, which could lead to confusing JSON parse errors without indicating the root cause. Signed-off-by: Ivin Joel Abraham --- src/app/config.rs | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/app/config.rs b/src/app/config.rs index 270f45ae..f6d49514 100644 --- a/src/app/config.rs +++ b/src/app/config.rs @@ -83,9 +83,14 @@ impl Config { fn load_file() -> Option { if let Ok(config_path) = env::var("PATCH_HUB_CONFIG_PATH") { if Path::new(&config_path).is_file() { - let file_contents = fs::read_to_string(&config_path).unwrap_or(String::new()); - if let Ok(config) = serde_json::from_str(&file_contents) { - return Some(config); + match fs::read_to_string(&config_path) { + Ok(file_contents) => match serde_json::from_str(&file_contents) { + Ok(config) => return Some(config), + Err(e) => eprintln!("Failed to parse config file {}: {}", config_path, e), + }, + Err(e) => { + eprintln!("Failed to read config file {}: {}", config_path, e) + } } } } @@ -95,9 +100,14 @@ impl Config { env::var("HOME").unwrap() ); if Path::new(&config_path).is_file() { - let file_contents = fs::read_to_string(&config_path).unwrap_or(String::new()); - if let Ok(config) = serde_json::from_str(&file_contents) { - return Some(config); + match fs::read_to_string(&config_path) { + Ok(file_contents) => match serde_json::from_str(&file_contents) { + Ok(config) => return Some(config), + Err(e) => eprintln!("Failed to parse config file {}: {}", config_path, e), + }, + Err(e) => { + eprintln!("Failed to read config file {}: {}", config_path, e) + } } } @@ -128,9 +138,11 @@ impl Config { pub fn build() -> Self { let mut config = Self::load_file().unwrap_or_else(|| { + eprintln!("No valid config file found, using default configuration"); let config = Self::default(); - // TODO: Better handle this error - let _ = config.save_patch_hub_config(); + config.save_patch_hub_config().unwrap_or_else(|e| { + eprintln!("Failed to save default config: {}", e); + }); config });