oxwm

https://git.tonybtw.com/oxwm.git git://git.tonybtw.com/oxwm.git

Made error handling more consistent.

Commit
e81197daf108f0dbad8c2186e8f1785b200e3426
Parent
c95b17e
Author
Scott DeJong <ScottADeJong@gmail.com>
Date
2025-12-26 15:32:38

Diff

diff --git a/src/errors.rs b/src/errors.rs
index 7f85943..fbba5db 100644
--- a/src/errors.rs
+++ b/src/errors.rs
@@ -29,6 +29,9 @@ pub enum ConfigError {
     UnknownBlockCommand(String),
     MissingCommandArg { command: String, field: String },
     ValidationError(String),
+    NoConfigPathSet,
+    NoConfigAtPath,
+    CouldNotReadConfig(std::io::Error),
 }
 
 #[derive(Debug)]
@@ -97,6 +100,12 @@ impl std::fmt::Display for ConfigError {
                 write!(f, "{} command requires {}", command, field)
             }
             Self::ValidationError(msg) => write!(f, "{}", msg),
+            Self::NoConfigPathSet => write!(
+                f,
+                "Could not find config file. Config path should've been set while loading"
+            ),
+            Self::NoConfigAtPath => write!(f, "Could not find config file, has it been moved?"),
+            Self::CouldNotReadConfig(e) => write!(f, "Could not read config: {e}"),
         }
     }
 }
diff --git a/src/overlay/error.rs b/src/overlay/error.rs
index 1bc9688..128729e 100644
--- a/src/overlay/error.rs
+++ b/src/overlay/error.rs
@@ -1,6 +1,6 @@
 use super::{Overlay, OverlayBase};
 use crate::bar::font::Font;
-use crate::errors::X11Error;
+use crate::errors::{ConfigError, X11Error};
 use x11rb::connection::Connection;
 use x11rb::protocol::xproto::*;
 use x11rb::rust_connection::RustConnection;
@@ -47,14 +47,14 @@ impl ErrorOverlay {
         &mut self,
         connection: &RustConnection,
         font: &Font,
-        error_text: &str,
+        error: ConfigError,
         monitor_x: i16,
         monitor_y: i16,
         screen_width: u16,
         screen_height: u16,
     ) -> Result<(), X11Error> {
         let max_line_width = (screen_width as i16 / 2 - PADDING * 4).max(300) as u16;
-        let error_with_instruction = format!("{}\n\nFix the config file and reload.", error_text);
+        let error_with_instruction = format!("{}\n\nFix the config file and reload.", error);
         self.lines = self.wrap_text(&error_with_instruction, font, max_line_width);
 
         let mut content_width = 0u16;
diff --git a/src/window_manager.rs b/src/window_manager.rs
index 38a89f7..cd39358 100644
--- a/src/window_manager.rs
+++ b/src/window_manager.rs
@@ -323,12 +323,11 @@ impl WindowManager {
         let monitor_y = monitor.screen_y as i16;
         let screen_width = monitor.screen_width as u16;
         let screen_height = monitor.screen_height as u16;
-        let error = format!("{error}");
 
         if let Err(e) = self.overlay.show_error(
             &self.connection,
             &self.font,
-            error.as_str(),
+            error,
             monitor_x,
             monitor_y,
             screen_width,
@@ -338,23 +337,23 @@ impl WindowManager {
         }
     }
 
-    fn try_reload_config(&mut self) -> Result<(), String> {
-        let lua_path =
-            self.config.path.as_ref().ok_or(
-                "Could not find config file. Config path should've been set while loading",
-            )?;
+    fn try_reload_config(&mut self) -> Result<(), ConfigError> {
+        let lua_path = self
+            .config
+            .path
+            .as_ref()
+            .ok_or(ConfigError::NoConfigPathSet)?;
 
         if !lua_path.exists() {
-            return Err("Could not find config file, has it been moved?".to_string());
+            return Err(ConfigError::NoConfigAtPath);
         }
 
-        let config_str = std::fs::read_to_string(lua_path)
-            .map_err(|e| format!("Failed to read config: {}", e))?;
+        let config_str =
+            std::fs::read_to_string(lua_path).map_err(|e| ConfigError::CouldNotReadConfig(e))?;
 
         let config_dir = lua_path.parent();
 
-        let new_config = crate::config::parse_lua_config(&config_str, config_dir)
-            .map_err(|e| format!("{}", e))?;
+        let new_config = crate::config::parse_lua_config(&config_str, config_dir)?;
 
         let lua_path = self.config.path.take();
 
@@ -2881,7 +2880,7 @@ impl WindowManager {
                                 }
                                 Err(err) => {
                                     eprintln!("Config reload error: {}", err);
-                                    self.error_message = Some(err.clone());
+                                    self.error_message = Some(err.to_string());
                                     let monitor = &self.monitors[self.selected_monitor];
                                     let monitor_x = monitor.screen_x as i16;
                                     let monitor_y = monitor.screen_y as i16;
@@ -2890,7 +2889,7 @@ impl WindowManager {
                                     match self.overlay.show_error(
                                         &self.connection,
                                         &self.font,
-                                        &err,
+                                        err,
                                         monitor_x,
                                         monitor_y,
                                         screen_width,