From edba4d24243f2c854208accffc2e888242f32d70 Mon Sep 17 00:00:00 2001 From: Ottatop Date: Sun, 6 Aug 2023 19:41:48 -0500 Subject: [PATCH] Improve error handling --- shell.nix | 1 + src/api.rs | 20 +++++++++++++++++--- src/state.rs | 36 +++++++++++++++++++++++++++++++----- 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/shell.nix b/shell.nix index 0bd897e..6834c9c 100644 --- a/shell.nix +++ b/shell.nix @@ -17,6 +17,7 @@ pkgs.mkShell { pkgs.libGL pkgs.libGL.dev pkgs.egl-wayland + pkgs.xwayland ]; shellHook = '' export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${pkgs.wayland}/lib:${pkgs.libxkbcommon}/lib:${pkgs.libGL}/lib diff --git a/src/api.rs b/src/api.rs index dd1fb31..77d03d3 100644 --- a/src/api.rs +++ b/src/api.rs @@ -89,14 +89,28 @@ impl PinnacleSocketSource { pub fn new(sender: Sender) -> Result { let socket_path = Path::new(SOCKET_PATH); + // TODO: use anyhow + if let Ok(exists) = socket_path.try_exists() { if exists { - std::fs::remove_file(socket_path)?; + if let Err(err) = std::fs::remove_file(socket_path) { + tracing::error!("Failed to remove old socket: {err}"); + return Err(err); + } } } - let listener = UnixListener::bind(socket_path)?; - listener.set_nonblocking(true)?; + let listener = match UnixListener::bind(socket_path) { + Ok(listener) => listener, + Err(err) => { + tracing::error!("Failed to bind to socket: {err}"); + return Err(err); + } + }; + if let Err(err) = listener.set_nonblocking(true) { + tracing::error!("Failed to set socket to nonblocking: {err}"); + return Err(err); + } let socket = Generic::new(listener, Interest::READ, Mode::Level); diff --git a/src/state.rs b/src/state.rs index 9de5cb2..c6a5697 100644 --- a/src/state.rs +++ b/src/state.rs @@ -749,6 +749,10 @@ impl State { let socket_name = socket.socket_name().to_os_string(); std::env::set_var("WAYLAND_DISPLAY", socket_name.clone()); + tracing::info!( + "Set WAYLAND_DISPLAY to {}", + socket_name.clone().to_string_lossy() + ); // Opening a new process will use up a few file descriptors, around 10 for Alacritty, for // example. Because of this, opening up only around 100 processes would exhaust the file @@ -756,12 +760,15 @@ impl State { // // To fix this, I just set the limit to be higher. As Pinnacle is the whole graphical // environment, I *think* this is ok. + tracing::info!("Trying to raise file descriptor limit..."); if let Err(err) = smithay::reexports::nix::sys::resource::setrlimit( smithay::reexports::nix::sys::resource::Resource::RLIMIT_NOFILE, 65536, 65536 * 2, ) { tracing::error!("Could not raise fd limit: errno {err}"); + } else { + tracing::info!("Fd raise success!"); } loop_handle.insert_source(socket, |stream, _metadata, data| { @@ -789,7 +796,17 @@ impl State { // TODO: there should only ever be one client working at a time, and creating a new client // | when one is already running should be impossible. // INFO: this source try_clone()s the stream - loop_handle.insert_source(PinnacleSocketSource::new(tx_channel)?, |stream, _, data| { + + // TODO: probably use anyhow or something + let socket_source = match PinnacleSocketSource::new(tx_channel) { + Ok(source) => source, + Err(err) => { + tracing::error!("Failed to create the socket source: {err}"); + Err(err)? + } + }; + + loop_handle.insert_source(socket_source, |stream, _, data| { if let Some(old_stream) = data .state .api_state @@ -817,7 +834,10 @@ impl State { }); if Path::new(&config_path).exists() { - let lua_path = std::env::var("LUA_PATH").expect("Lua is not installed!"); + let lua_path = std::env::var("LUA_PATH").unwrap_or_else(|_| { + tracing::info!("LUA_PATH was not set, using empty string"); + "".to_string() + }); let mut local_lua_path = std::env::current_dir() .expect("Couldn't get current dir") .to_string_lossy() @@ -826,15 +846,21 @@ impl State { let new_lua_path = format!("{local_lua_path}/?.lua;{local_lua_path}/?/init.lua;{local_lua_path}/lib/?.lua;{local_lua_path}/lib/?/init.lua;{lua_path}"); - let lua_cpath = std::env::var("LUA_CPATH").expect("Lua is not installed!"); + let lua_cpath = std::env::var("LUA_CPATH").unwrap_or_else(|_| { + tracing::info!("LUA_CPATH was not set, using empty string"); + "".to_string() + }); let new_lua_cpath = format!("{local_lua_path}/lib/?.so;{lua_cpath}"); - std::process::Command::new("lua") + if let Err(err) = std::process::Command::new("lua") .arg(config_path) .env("LUA_PATH", new_lua_path) .env("LUA_CPATH", new_lua_cpath) .spawn() - .expect("Could not start config process"); + { + tracing::error!("Failed to start Lua: {err}"); + return Err(err)?; + } } else { tracing::error!("Could not find {}", config_path); }