From 01adc71646481cf433d0f2b5c3d4c3e507c1f1b6 Mon Sep 17 00:00:00 2001 From: miruka Date: Wed, 13 Jan 2021 11:23:38 -0400 Subject: [PATCH] Catch error when sending dbus notification fails --- src/backend/backend.py | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/backend/backend.py b/src/backend/backend.py index ff73c10c..5cf1597f 100644 --- a/src/backend/backend.py +++ b/src/backend/backend.py @@ -7,6 +7,7 @@ import os import re import sys import time +import traceback from datetime import datetime, timedelta from pathlib import Path from typing import Any, DefaultDict, Dict, List, Optional, Tuple, Union @@ -136,7 +137,8 @@ class Backend: self.mxc_events: DefaultDict[str, List[Event]] = DefaultDict(list) - self.notification_avatar_cache: Dict[str, Path] = {} # {mxc: path} + self.notification_avatar_cache: Dict[str, Path] = {} # {mxc: path} + self.notifications_working: bool = True def __repr__(self) -> str: @@ -561,11 +563,19 @@ class Backend: self, title: str, body: str = "", image: Union[Path, str] = "", ) -> None: # XXX: images on windows must be .ICO - plyer.notification.notify( - title = title, - message = body, - app_name = __app_name__, - app_icon = str(image), - timeout = 10, - toast = False, - ) + + try: + plyer.notification.notify( + title = title, + message = body, + app_name = __app_name__, + app_icon = str(image), + timeout = 10, + toast = False, + ) + self.notifications_working = True + except Exception: # noqa + if self.notifications_working: + trace = traceback.format_exc().rstrip() + log.error("Sending desktop notification failed\n%s", trace) + self.notifications_working = False