From 7ff4f0727ef05b63be4b91290d0d4b8f3f3854b4 Mon Sep 17 00:00:00 2001 From: MaxVerevkin Date: Sun, 13 Oct 2024 10:44:31 +0300 Subject: fix error handling in wm_info_provider --- src/main.rs | 2 +- src/state.rs | 19 ++++++++++++------- src/wm_info_provider/hyprland.rs | 2 +- src/wm_info_provider/river.rs | 3 ++- 4 files changed, 16 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 18d604e..076d17b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -96,7 +96,7 @@ fn main() -> anyhow::Result<()> { .unwrap() .child .kill(); - ctx.state.set_error(ctx.conn, e); + ctx.state.set_error(ctx.conn, "status", e); Ok(event_loop::Action::Unregister) } } diff --git a/src/state.rs b/src/state.rs index 279aac3..72bf3f0 100644 --- a/src/state.rs +++ b/src/state.rs @@ -33,6 +33,7 @@ pub struct State { pub pending_outputs: Vec, pub hidden: bool, + pub has_error: bool, pub bars: Vec, pub shared_state: SharedState, @@ -99,6 +100,7 @@ impl State { .collect(), hidden: false, + has_error: false, bars: Vec::new(), shared_state: SharedState { @@ -114,27 +116,30 @@ impl State { }; if let Err(e) = error { - this.set_error(conn, e.to_string()); + this.set_error(conn, "init", e.to_string()); } this } pub fn set_blocks(&mut self, conn: &mut Connection, blocks: Vec) { - self.shared_state - .blocks_cache - .process_new_blocks(&self.shared_state.config, blocks); - self.draw_all(conn); + if !self.has_error { + self.shared_state + .blocks_cache + .process_new_blocks(&self.shared_state.config, blocks); + self.draw_all(conn); + } } - pub fn set_error(&mut self, conn: &mut Connection, error: impl Display) { + pub fn set_error(&mut self, conn: &mut Connection, context: &str, error: impl Display) { self.set_blocks( conn, vec![Block { - full_text: error.to_string(), + full_text: format!("{context}: {error}"), ..Default::default() }], ); + self.has_error = true; } pub fn draw_all(&mut self, conn: &mut Connection) { diff --git a/src/wm_info_provider/hyprland.rs b/src/wm_info_provider/hyprland.rs index 0d7d4ec..19a259a 100644 --- a/src/wm_info_provider/hyprland.rs +++ b/src/wm_info_provider/hyprland.rs @@ -39,7 +39,7 @@ impl WmInfoProvider for HyprlandInfoProvider { match hyprland_cb(ctx.conn, ctx.state) { Ok(()) => Ok(event_loop::Action::Keep), Err(e) => { - ctx.state.set_error(ctx.conn, e); + ctx.state.set_error(ctx.conn, "hyprland", e); Ok(event_loop::Action::Unregister) } } diff --git a/src/wm_info_provider/river.rs b/src/wm_info_provider/river.rs index 6b3c8b0..61b7e2b 100644 --- a/src/wm_info_provider/river.rs +++ b/src/wm_info_provider/river.rs @@ -211,6 +211,7 @@ fn seat_status_cb(ctx: EventCtx) { fn river_command_cb(ctx: EventCtx) { if let zriver_command_callback_v1::Event::Failure(msg) = ctx.event { - ctx.state.set_error(ctx.conn, msg.to_string_lossy()) + ctx.state + .set_error(ctx.conn, "river", msg.to_string_lossy()) } } -- cgit v1.3.1