tony / oxwm

Merge pull request #106 from ScottADeJong/update/errors Browse files
use  `ConfigError` type in `window_manager::try_reload_config` Result and `ErrorOverlay::show_error`
Emzy committed 2025-12-26 parents c95b17e e81197d commit e64975ecc19812caa09f29efaa82e46b9be05b4b
Showing 3 changed files with 25 additions and 17 deletions. UnifiedSplit
+9 −0 src/errors.rs
@@ -29,6 +29,9 @@ pub enum ConfigError {
29 UnknownBlockCommand(String), 29 UnknownBlockCommand(String),
30 MissingCommandArg { command: String, field: String }, 30 MissingCommandArg { command: String, field: String },
31 ValidationError(String), 31 ValidationError(String),
32 + NoConfigPathSet,
33 + NoConfigAtPath,
34 + CouldNotReadConfig(std::io::Error),
32 } 35 }
33 36
34 #[derive(Debug)] 37 #[derive(Debug)]
@@ -97,6 +100,12 @@ impl std::fmt::Display for ConfigError {
97 write!(f, "{} command requires {}", command, field) 100 write!(f, "{} command requires {}", command, field)
98 } 101 }
99 Self::ValidationError(msg) => write!(f, "{}", msg), 102 Self::ValidationError(msg) => write!(f, "{}", msg),
103 + Self::NoConfigPathSet => write!(
104 + f,
105 + "Could not find config file. Config path should've been set while loading"
106 + ),
107 + Self::NoConfigAtPath => write!(f, "Could not find config file, has it been moved?"),
108 + Self::CouldNotReadConfig(e) => write!(f, "Could not read config: {e}"),
100 } 109 }
101 } 110 }
102 } 111 }
+3 −3 src/overlay/error.rs
@@ -1,6 +1,6 @@
1 use super::{Overlay, OverlayBase}; 1 use super::{Overlay, OverlayBase};
2 use crate::bar::font::Font; 2 use crate::bar::font::Font;
3 -use crate::errors::X11Error; 3 +use crate::errors::{ConfigError, X11Error};
4 use x11rb::connection::Connection; 4 use x11rb::connection::Connection;
5 use x11rb::protocol::xproto::*; 5 use x11rb::protocol::xproto::*;
6 use x11rb::rust_connection::RustConnection; 6 use x11rb::rust_connection::RustConnection;
@@ -47,14 +47,14 @@ impl ErrorOverlay {
47 &mut self, 47 &mut self,
48 connection: &RustConnection, 48 connection: &RustConnection,
49 font: &Font, 49 font: &Font,
50 - error_text: &str, 50 + error: ConfigError,
51 monitor_x: i16, 51 monitor_x: i16,
52 monitor_y: i16, 52 monitor_y: i16,
53 screen_width: u16, 53 screen_width: u16,
54 screen_height: u16, 54 screen_height: u16,
55 ) -> Result<(), X11Error> { 55 ) -> Result<(), X11Error> {
56 let max_line_width = (screen_width as i16 / 2 - PADDING * 4).max(300) as u16; 56 let max_line_width = (screen_width as i16 / 2 - PADDING * 4).max(300) as u16;
57 - let error_with_instruction = format!("{}\n\nFix the config file and reload.", error_text); 57 + let error_with_instruction = format!("{}\n\nFix the config file and reload.", error);
58 self.lines = self.wrap_text(&error_with_instruction, font, max_line_width); 58 self.lines = self.wrap_text(&error_with_instruction, font, max_line_width);
59 59
60 let mut content_width = 0u16; 60 let mut content_width = 0u16;
+13 −14 src/window_manager.rs
@@ -323,12 +323,11 @@ impl WindowManager {
323 let monitor_y = monitor.screen_y as i16; 323 let monitor_y = monitor.screen_y as i16;
324 let screen_width = monitor.screen_width as u16; 324 let screen_width = monitor.screen_width as u16;
325 let screen_height = monitor.screen_height as u16; 325 let screen_height = monitor.screen_height as u16;
326 - let error = format!("{error}");
327 326
328 if let Err(e) = self.overlay.show_error( 327 if let Err(e) = self.overlay.show_error(
329 &self.connection, 328 &self.connection,
330 &self.font, 329 &self.font,
331 - error.as_str(), 330 + error,
332 monitor_x, 331 monitor_x,
333 monitor_y, 332 monitor_y,
334 screen_width, 333 screen_width,
@@ -338,23 +337,23 @@ impl WindowManager {
338 } 337 }
339 } 338 }
340 339
341 - fn try_reload_config(&mut self) -> Result<(), String> { 340 + fn try_reload_config(&mut self) -> Result<(), ConfigError> {
342 - let lua_path = 341 + let lua_path = self
343 - self.config.path.as_ref().ok_or( 342 + .config
344 - "Could not find config file. Config path should've been set while loading", 343 + .path
345 - )?; 344 + .as_ref()
345 + .ok_or(ConfigError::NoConfigPathSet)?;
346 346
347 if !lua_path.exists() { 347 if !lua_path.exists() {
348 - return Err("Could not find config file, has it been moved?".to_string()); 348 + return Err(ConfigError::NoConfigAtPath);
349 } 349 }
350 350
351 - let config_str = std::fs::read_to_string(lua_path) 351 + let config_str =
352 - .map_err(|e| format!("Failed to read config: {}", e))?; 352 + std::fs::read_to_string(lua_path).map_err(|e| ConfigError::CouldNotReadConfig(e))?;
353 353
354 let config_dir = lua_path.parent(); 354 let config_dir = lua_path.parent();
355 355
356 - let new_config = crate::config::parse_lua_config(&config_str, config_dir) 356 + let new_config = crate::config::parse_lua_config(&config_str, config_dir)?;
357 - .map_err(|e| format!("{}", e))?;
358 357
359 let lua_path = self.config.path.take(); 358 let lua_path = self.config.path.take();
360 359
@@ -2881,7 +2880,7 @@ impl WindowManager {
2881 } 2880 }
2882 Err(err) => { 2881 Err(err) => {
2883 eprintln!("Config reload error: {}", err); 2882 eprintln!("Config reload error: {}", err);
2884 - self.error_message = Some(err.clone()); 2883 + self.error_message = Some(err.to_string());
2885 let monitor = &self.monitors[self.selected_monitor]; 2884 let monitor = &self.monitors[self.selected_monitor];
2886 let monitor_x = monitor.screen_x as i16; 2885 let monitor_x = monitor.screen_x as i16;
2887 let monitor_y = monitor.screen_y as i16; 2886 let monitor_y = monitor.screen_y as i16;
@@ -2890,7 +2889,7 @@ impl WindowManager {
2890 match self.overlay.show_error( 2889 match self.overlay.show_error(
2891 &self.connection, 2890 &self.connection,
2892 &self.font, 2891 &self.font,
2893 - &err, 2892 + err,
2894 monitor_x, 2893 monitor_x,
2895 monitor_y, 2894 monitor_y,
2896 screen_width, 2895 screen_width,