From 90944eb0ef6625eb5e0296e9a4abf6b3b390d68f Mon Sep 17 00:00:00 2001 From: Pedro Burgos Date: Sat, 21 Nov 2020 19:06:33 +0100 Subject: [PATCH] Improve error messages when parsing config files (#62) * Better error messages when parsing xml config files * Store config file path in eww_config struct * cargo fmt --- src/config/eww_config.rs | 12 ++++++++---- src/main.rs | 2 -- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/config/eww_config.rs b/src/config/eww_config.rs index 923e2ec..e064062 100644 --- a/src/config/eww_config.rs +++ b/src/config/eww_config.rs @@ -11,6 +11,7 @@ use super::{ xml_ext::{XmlElement, XmlNode}, EwwWindowDefinition, ScriptVar, WindowName, }; +use std::path::PathBuf; #[derive(Debug, Clone)] pub struct EwwConfig { @@ -20,6 +21,7 @@ pub struct EwwConfig { // TODO make this a hashmap script_vars: Vec, + pub filepath: PathBuf, } impl EwwConfig { @@ -36,7 +38,8 @@ impl EwwConfig { pub fn read_from_file>(path: P) -> Result { let content = util::replace_env_var_references(std::fs::read_to_string(path.as_ref())?); - let document = roxmltree::Document::parse(&content)?; + let document = roxmltree::Document::parse(&content) + .with_context(|| format!("error parsing xml config at: {}", path.as_ref().display()))?; let result = EwwConfig::from_xml_element(XmlNode::from(document.root_element()).as_element()?.clone(), path.as_ref()); result @@ -53,7 +56,7 @@ impl EwwConfig { EwwConfig::read_from_file(basepath.join(childpath)) }) .collect::>>() - .context(format!("error handling include definitions: {}", path.display()))?, + .context(format!("error handling include definitions at: {}", path.display()))?, Err(_) => Vec::new(), }; @@ -65,7 +68,7 @@ impl EwwConfig { Ok((def.name.clone(), def)) }) .collect::>>() - .with_context(|| format!("error parsing widget definitions: {}", path.display()))?; + .with_context(|| format!("error parsing widget definitions at: {}", path.display()))?; let windows = xml .child("windows")? @@ -75,7 +78,7 @@ impl EwwConfig { Ok((def.name.to_owned(), def)) }) .collect::>>() - .with_context(|| format!("error parsing window definitions: {}", path.display()))?; + .with_context(|| format!("error parsing window definitions at: {}", path.display()))?; let variables_block = xml.child("variables").ok(); @@ -107,6 +110,7 @@ impl EwwConfig { windows, initial_variables, script_vars, + filepath: path.to_path_buf(), }; EwwConfig::merge_includes(current_config, includes) } diff --git a/src/main.rs b/src/main.rs index e4d58f5..8f7668c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -54,11 +54,9 @@ fn main() { opts::Action::WithServer(action) => { log::info!("Trying to find server process"); if let Ok(stream) = net::UnixStream::connect(&*IPC_SOCKET_PATH) { - client::forward_command_to_server(stream, action).context("Error while forwarding command to server")?; } else if action.needs_server_running() { println!("No eww server running"); - } else { log::info!("No server running, initializing server..."); server::initialize_server(opts.should_detach, action)?;