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

View File

@@ -136,14 +136,36 @@ impl BarWidget for Komorebi {
.clicked()
{
update = Some(ws.to_string());
komorebi_client::send_message(&SocketMessage::MouseFollowsFocus(false))
.unwrap();
komorebi_client::send_message(&SocketMessage::FocusWorkspaceNumber(i)).unwrap();
komorebi_client::send_message(&SocketMessage::MouseFollowsFocus(
komorebi_notification_state.mouse_follows_focus,
))
.unwrap();
komorebi_client::send_message(&SocketMessage::Retile).unwrap();
let mut proceed = true;
if komorebi_client::send_message(&SocketMessage::MouseFollowsFocus(false))
.is_err()
{
tracing::error!("could not send message to komorebi: MouseFollowsFocus");
proceed = false;
}
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()),
)
.clicked()
{
komorebi_client::send_message(&SocketMessage::CycleLayout(
&& komorebi_client::send_message(&SocketMessage::CycleLayout(
CycleDirection::Next,
))
.unwrap();
.is_err()
{
tracing::error!("could not send message to komorebi: CycleLayout");
}
ui.add_space(WIDGET_SPACING);
@@ -184,22 +207,46 @@ impl BarWidget for Komorebi {
.clicked()
{
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,
))
.unwrap();
.is_err()
{
tracing::error!(
"could not send message to komorebi: ReplaceConfiguration"
);
proceed = false;
}
if let Some(rect) = komorebi_notification_state.work_area_offset {
let monitor_index = komorebi_client::send_query(&SocketMessage::Query(
komorebi_client::StateQuery::FocusedMonitorIndex,
))
.unwrap();
komorebi_client::send_message(&SocketMessage::MonitorWorkAreaOffset(
monitor_index.parse::<usize>().unwrap(),
rect,
))
.unwrap();
if proceed {
match komorebi_client::send_query(&SocketMessage::Query(
komorebi_client::StateQuery::FocusedMonitorIndex,
)) {
Ok(idx) => {
if let Ok(monitor_idx) = idx.parse::<usize>() {
if komorebi_client::send_message(
&SocketMessage::MonitorWorkAreaOffset(
monitor_idx,
rect,
),
)
.is_err()
{
tracing::error!(
"could not send message to komorebi: MonitorWorkAreaOffset"
);
}
}
}
Err(_) => {
tracing::error!(
"could not send message to komorebi: Query"
);
}
}
}
}
}
}

View File

@@ -14,6 +14,7 @@ use crate::bar::Komobar;
use crate::config::KomobarConfig;
use crate::config::Position;
use clap::Parser;
use color_eyre::eyre::OptionExt;
use eframe::egui::ViewportBuilder;
use font_loader::system_fonts;
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>(
&komorebi_client::send_query(&SocketMessage::State).unwrap(),
)?;
let state = serde_json::from_str::<komorebi_client::State>(&komorebi_client::send_query(
&SocketMessage::State,
)?)?;
let mut viewport_builder = ViewportBuilder::default()
.with_decorations(false)
@@ -185,12 +186,14 @@ fn main() -> color_eyre::Result<()> {
hotwatch.watch(config_path, move |event| match event.kind {
EventKind::Modify(_) | EventKind::Remove(_) => match KomobarConfig::read(&config_path_cl) {
Ok(updated) => {
tx_config.send(updated).unwrap();
tracing::info!(
"configuration file updated: {}",
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) => {
tracing::error!("{error}");
@@ -216,7 +219,9 @@ fn main() -> color_eyre::Result<()> {
let ctx_komorebi = cc.egui_ctx.clone();
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\"");
for client in listener.incoming() {
@@ -256,14 +261,27 @@ fn main() -> color_eyre::Result<()> {
}
}
if let Ok(notification) =
serde_json::from_str::<komorebi_client::Notification>(
&String::from_utf8(buffer).unwrap(),
)
{
tracing::debug!("received notification from komorebi");
tx_gui.send(notification).unwrap();
ctx_komorebi.request_repaint();
match String::from_utf8(buffer) {
Ok(notification_string) => {
if let Ok(notification) =
serde_json::from_str::<komorebi_client::Notification>(
&notification_string,
)
{
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) => {