refactor(bar): change panics to error logs

This commit goes through all komorebi_client calls which return a
Result<T> and replaces unwraps with error logs to avoid runtime panics.
This commit is contained in:
LGUG2Z
2024-09-18 14:50:21 -07:00
parent df19d06333
commit df409902bb
2 changed files with 103 additions and 38 deletions
+70 -23
View File
@@ -136,14 +136,36 @@ impl BarWidget for Komorebi {
.clicked() .clicked()
{ {
update = Some(ws.to_string()); update = Some(ws.to_string());
komorebi_client::send_message(&SocketMessage::MouseFollowsFocus(false)) let mut proceed = true;
.unwrap();
komorebi_client::send_message(&SocketMessage::FocusWorkspaceNumber(i)).unwrap(); if komorebi_client::send_message(&SocketMessage::MouseFollowsFocus(false))
komorebi_client::send_message(&SocketMessage::MouseFollowsFocus( .is_err()
komorebi_notification_state.mouse_follows_focus, {
)) tracing::error!("could not send message to komorebi: MouseFollowsFocus");
.unwrap(); proceed = false;
komorebi_client::send_message(&SocketMessage::Retile).unwrap(); }
if proceed
&& komorebi_client::send_message(&SocketMessage::FocusWorkspaceNumber(i))
.is_err()
{
tracing::error!("could not send message to komorebi: FocusWorkspaceNumber");
proceed = false;
}
if proceed
&& komorebi_client::send_message(&SocketMessage::MouseFollowsFocus(
komorebi_notification_state.mouse_follows_focus,
))
.is_err()
{
tracing::error!("could not send message to komorebi: MouseFollowsFocus");
proceed = false;
}
if proceed && komorebi_client::send_message(&SocketMessage::Retile).is_err() {
tracing::error!("could not send message to komorebi: Retile");
}
} }
} }
@@ -163,11 +185,12 @@ impl BarWidget for Komorebi {
.sense(Sense::click()), .sense(Sense::click()),
) )
.clicked() .clicked()
{ && komorebi_client::send_message(&SocketMessage::CycleLayout(
komorebi_client::send_message(&SocketMessage::CycleLayout(
CycleDirection::Next, CycleDirection::Next,
)) ))
.unwrap(); .is_err()
{
tracing::error!("could not send message to komorebi: CycleLayout");
} }
ui.add_space(WIDGET_SPACING); ui.add_space(WIDGET_SPACING);
@@ -184,22 +207,46 @@ impl BarWidget for Komorebi {
.clicked() .clicked()
{ {
let canonicalized = dunce::canonicalize(path.clone()).unwrap_or(path); let canonicalized = dunce::canonicalize(path.clone()).unwrap_or(path);
komorebi_client::send_message(&SocketMessage::ReplaceConfiguration( let mut proceed = true;
if komorebi_client::send_message(&SocketMessage::ReplaceConfiguration(
canonicalized, canonicalized,
)) ))
.unwrap(); .is_err()
{
tracing::error!(
"could not send message to komorebi: ReplaceConfiguration"
);
proceed = false;
}
if let Some(rect) = komorebi_notification_state.work_area_offset { if let Some(rect) = komorebi_notification_state.work_area_offset {
let monitor_index = komorebi_client::send_query(&SocketMessage::Query( if proceed {
komorebi_client::StateQuery::FocusedMonitorIndex, match komorebi_client::send_query(&SocketMessage::Query(
)) komorebi_client::StateQuery::FocusedMonitorIndex,
.unwrap(); )) {
Ok(idx) => {
komorebi_client::send_message(&SocketMessage::MonitorWorkAreaOffset( if let Ok(monitor_idx) = idx.parse::<usize>() {
monitor_index.parse::<usize>().unwrap(), if komorebi_client::send_message(
rect, &SocketMessage::MonitorWorkAreaOffset(
)) monitor_idx,
.unwrap(); rect,
),
)
.is_err()
{
tracing::error!(
"could not send message to komorebi: MonitorWorkAreaOffset"
);
}
}
}
Err(_) => {
tracing::error!(
"could not send message to komorebi: Query"
);
}
}
}
} }
} }
} }
+33 -15
View File
@@ -14,6 +14,7 @@ use crate::bar::Komobar;
use crate::config::KomobarConfig; use crate::config::KomobarConfig;
use crate::config::Position; use crate::config::Position;
use clap::Parser; use clap::Parser;
use color_eyre::eyre::OptionExt;
use eframe::egui::ViewportBuilder; use eframe::egui::ViewportBuilder;
use font_loader::system_fonts; use font_loader::system_fonts;
use hotwatch::EventKind; use hotwatch::EventKind;
@@ -130,11 +131,11 @@ fn main() -> color_eyre::Result<()> {
} }
}; };
let config_path = config_path.unwrap(); let config_path = config_path.ok_or_eyre("config path not found")?;
let state = serde_json::from_str::<komorebi_client::State>( let state = serde_json::from_str::<komorebi_client::State>(&komorebi_client::send_query(
&komorebi_client::send_query(&SocketMessage::State).unwrap(), &SocketMessage::State,
)?; )?)?;
let mut viewport_builder = ViewportBuilder::default() let mut viewport_builder = ViewportBuilder::default()
.with_decorations(false) .with_decorations(false)
@@ -185,12 +186,14 @@ fn main() -> color_eyre::Result<()> {
hotwatch.watch(config_path, move |event| match event.kind { hotwatch.watch(config_path, move |event| match event.kind {
EventKind::Modify(_) | EventKind::Remove(_) => match KomobarConfig::read(&config_path_cl) { EventKind::Modify(_) | EventKind::Remove(_) => match KomobarConfig::read(&config_path_cl) {
Ok(updated) => { Ok(updated) => {
tx_config.send(updated).unwrap();
tracing::info!( tracing::info!(
"configuration file updated: {}", "configuration file updated: {}",
config_path_cl.as_path().to_string_lossy() config_path_cl.as_path().to_string_lossy()
); );
if let Err(error) = tx_config.send(updated) {
tracing::error!("could not send configuration update to gui: {error}")
}
} }
Err(error) => { Err(error) => {
tracing::error!("{error}"); tracing::error!("{error}");
@@ -216,7 +219,9 @@ fn main() -> color_eyre::Result<()> {
let ctx_komorebi = cc.egui_ctx.clone(); let ctx_komorebi = cc.egui_ctx.clone();
std::thread::spawn(move || { std::thread::spawn(move || {
let listener = komorebi_client::subscribe("komorebi-bar").unwrap(); let listener = komorebi_client::subscribe("komorebi-bar")
.expect("could not subscribe to komorebi notifications");
tracing::info!("subscribed to komorebi notifications: \"komorebi-bar\""); tracing::info!("subscribed to komorebi notifications: \"komorebi-bar\"");
for client in listener.incoming() { for client in listener.incoming() {
@@ -256,14 +261,27 @@ fn main() -> color_eyre::Result<()> {
} }
} }
if let Ok(notification) = match String::from_utf8(buffer) {
serde_json::from_str::<komorebi_client::Notification>( Ok(notification_string) => {
&String::from_utf8(buffer).unwrap(), if let Ok(notification) =
) serde_json::from_str::<komorebi_client::Notification>(
{ &notification_string,
tracing::debug!("received notification from komorebi"); )
tx_gui.send(notification).unwrap(); {
ctx_komorebi.request_repaint(); tracing::debug!("received notification from komorebi");
if let Err(error) = tx_gui.send(notification) {
tracing::error!("could not send komorebi notification update to gui: {error}")
}
ctx_komorebi.request_repaint();
}
}
Err(error) => {
tracing::error!(
"komorebi notification string was invalid utf8: {error}"
)
}
} }
} }
Err(error) => { Err(error) => {