From d56a024940b9fbde85d9013d69ea1ebe73ed01dc Mon Sep 17 00:00:00 2001 From: Alexander Bantyev Date: Wed, 16 Sep 2020 18:54:35 +0300 Subject: [PATCH] Handle notification server restarts gracefully --- common/src/lib.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/common/src/lib.rs b/common/src/lib.rs index 0e8d033..5e6d642 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -200,10 +200,21 @@ pub mod notify { } } - pub fn update(&mut self) { + fn try_update(&mut self) -> Result<(), String> { self.notification.update(self.title.as_deref().unwrap_or(""), self.get_full_text().as_deref(), self.icon.as_deref()).unwrap(); self.notification.set_urgency(self.urgency); - self.notification.show().unwrap(); + self.notification.show().or(Err("Failed to show the notification".to_string())) + } + + pub fn update(&mut self) { + // The notification server may restart, and in that case the notification becomes invalid; + // We need to handle that situation. + self.try_update().unwrap_or_else(|_| { + libnotify::uninit(); + init_if_not_already(); + self.notification = Notification::new("", None, None); + self.try_update().unwrap(); + }); } } }