tony / oxwm

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