fix: prevent crash when spawning non-existent commands
Browse files
Previously, spawning a command that didn't exist would cause the wm to crash. Commit adds proper error handling for `KeyAction::Spawn` to catch IO's `ErrorKind::NotFound` and prints a warning instead of propagating up the call stack.
+40 −0 src/keyboard/handlers.rs
| @@ -1,3 +1,5 @@ | |||
| 1 | +use std::process::Command; | ||
| 2 | + | ||
| 1 | use anyhow::Result; | 3 | use anyhow::Result; |
| 2 | use x11rb::connection::Connection; | 4 | use x11rb::connection::Connection; |
| 3 | use x11rb::protocol::xproto::*; | 5 | use x11rb::protocol::xproto::*; |
| @@ -92,3 +94,41 @@ pub fn handle_key_press(event: KeyPressEvent, keybindings: &[Key]) -> Result<(Ke | |||
| 92 | 94 | ||
| 93 | Ok((KeyAction::None, Arg::None)) | 95 | Ok((KeyAction::None, Arg::None)) |
| 94 | } | 96 | } |
| 97 | + | ||
| 98 | +pub fn handle_spawn_action(action: KeyAction, arg: &Arg) -> Result<()> { | ||
| 99 | + use std::io::ErrorKind; | ||
| 100 | + match action { | ||
| 101 | + KeyAction::Spawn => match arg { | ||
| 102 | + Arg::Str(command) => match Command::new(command).spawn() { | ||
| 103 | + Err(err) if err.kind() == ErrorKind::NotFound => { | ||
| 104 | + eprintln!( | ||
| 105 | + "KeyAction::Spawn failed: could not spawn \"{}\", command not found", | ||
| 106 | + command | ||
| 107 | + ); | ||
| 108 | + } | ||
| 109 | + Err(err) => Err(err)?, | ||
| 110 | + _ => (), | ||
| 111 | + }, | ||
| 112 | + Arg::Array(command) => { | ||
| 113 | + let Some((cmd, args)) = command.split_first() else { | ||
| 114 | + return Ok(()); | ||
| 115 | + }; | ||
| 116 | + | ||
| 117 | + match Command::new(cmd).args(args).spawn() { | ||
| 118 | + Err(err) if err.kind() == ErrorKind::NotFound => { | ||
| 119 | + eprintln!( | ||
| 120 | + "KeyAction::Spawn failed: could not spawn \"{}\", command not found", | ||
| 121 | + cmd | ||
| 122 | + ); | ||
| 123 | + } | ||
| 124 | + Err(err) => Err(err)?, | ||
| 125 | + _ => (), | ||
| 126 | + } | ||
| 127 | + } | ||
| 128 | + _ => {} | ||
| 129 | + }, | ||
| 130 | + _ => {} | ||
| 131 | + } | ||
| 132 | + | ||
| 133 | + Ok(()) | ||
| 134 | +} | ||
+2 −12 src/window_manager.rs
| @@ -1,6 +1,6 @@ | |||
| 1 | use crate::Config; | 1 | use crate::Config; |
| 2 | use crate::bar::Bar; | 2 | use crate::bar::Bar; |
| 3 | -use crate::keyboard::{self, Arg, KeyAction}; | 3 | +use crate::keyboard::{self, Arg, KeyAction, handlers}; |
| 4 | use crate::layout::GapConfig; | 4 | use crate::layout::GapConfig; |
| 5 | use crate::layout::Layout; | 5 | use crate::layout::Layout; |
| 6 | use crate::layout::tiling::TilingLayout; | 6 | use crate::layout::tiling::TilingLayout; |
| @@ -449,17 +449,7 @@ impl WindowManager { | |||
| 449 | 449 | ||
| 450 | fn handle_key_action(&mut self, action: KeyAction, arg: &Arg) -> Result<()> { | 450 | fn handle_key_action(&mut self, action: KeyAction, arg: &Arg) -> Result<()> { |
| 451 | match action { | 451 | match action { |
| 452 | - KeyAction::Spawn => match arg { | 452 | + KeyAction::Spawn => handlers::handle_spawn_action(action, arg)?, |
| 453 | - Arg::Str(command) => { | ||
| 454 | - std::process::Command::new(command).spawn()?; | ||
| 455 | - } | ||
| 456 | - Arg::Array(cmd) => { | ||
| 457 | - if let Some((program, args)) = cmd.split_first() { | ||
| 458 | - std::process::Command::new(program).args(args).spawn()?; | ||
| 459 | - } | ||
| 460 | - } | ||
| 461 | - _ => {} | ||
| 462 | - }, | ||
| 463 | KeyAction::KillClient => { | 453 | KeyAction::KillClient => { |
| 464 | if let Some(focused) = self.focused_window { | 454 | if let Some(focused) = self.focused_window { |
| 465 | match self.connection.kill_client(focused) { | 455 | match self.connection.kill_client(focused) { |