From d3f72b51b782b283b98dec37f4ced0559382bad5 Mon Sep 17 00:00:00 2001 From: rufevean Date: Sun, 1 Sep 2024 20:38:03 +0530 Subject: [PATCH] refactor - improved error --- src/config/mod.rs | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/config/mod.rs b/src/config/mod.rs index d35a249a80a..10daaacbd74 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -294,6 +294,7 @@ impl Config { Config::from_toml_for_style_edition( &toml, file_path.parent().unwrap(), + file_path, edition, style_edition, version, @@ -370,13 +371,14 @@ impl Config { } #[allow(dead_code)] - pub(super) fn from_toml(toml: &str, dir: &Path) -> Result { - Self::from_toml_for_style_edition(toml, dir, None, None, None) + pub(super) fn from_toml(toml: &str, dir: &Path,file_path : &Path) -> Result { + Self::from_toml_for_style_edition(toml, dir,file_path, None, None, None) } pub(crate) fn from_toml_for_style_edition( toml: &str, dir: &Path, + file_path: &Path, edition: Option, style_edition: Option, version: Option, @@ -402,12 +404,22 @@ impl Config { } Ok(parsed_config.to_parsed_config(style_edition, edition, version, dir)) } - Err(e) => { - err.push_str("Error: Decoding config file failed:\n"); - err.push_str(format!("{e}\n").as_str()); - err.push_str("Please check your config file."); - Err(err) - } +Err(e) => { + +let config_file_path_str = file_path.to_string_lossy(); + + let err_msg = format!( + "The file `{}` failed to parse.\n\ + Error details: {}\n\ + Help: Ensure that the configuration file at `{}` is correctly formatted.", + config_file_path_str, + e, + config_file_path_str + ); + + Err(err_msg) + } + } } }