Skip to content

Commit 8a3fe48

Browse files
committed
notifier: Ignore errors and move on
Better than crashing the entire flow
1 parent 97886d0 commit 8a3fe48

File tree

2 files changed

+33
-19
lines changed

2 files changed

+33
-19
lines changed

trakt_scrobbler/__init__.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,29 @@
66
import confuse
77
import yaml
88

9+
APP_NAME = 'Trakt Scrobbler'
910
# For reasons beyond my understanding, we cannot instantiate
1011
# DesktopNotifier _after_ we have imported pythoncom on Windows.
1112
# Trying to do so gives error "The application called an interface that was marshalled for a different thread"
1213
#
1314
# Possibly a conflict between win32 (pythoncom) and winrt (notifier) APIs.
1415
#
1516
# So workaround is to instantiate the notifier _before_ any other import happens
16-
APP_NAME = 'Trakt Scrobbler'
17-
from desktop_notifier.main import DesktopNotifier
18-
19-
notifier = DesktopNotifier(APP_NAME)
20-
if sys.platform == 'win32':
21-
# this is another workaround for windows.
22-
# See https://github.com/samschott/desktop-notifier/issues/95
23-
notifier._did_request_authorisation = True
24-
# else:
25-
# # can be initialized later
26-
# notifier = None
17+
try:
18+
from desktop_notifier.main import DesktopNotifier
19+
20+
notifier = DesktopNotifier(APP_NAME)
21+
except Exception as e:
22+
# FIXME: haven't even initialized logging yet, where to log??
23+
print("Error setting up notifier!", e, file=sys.stderr)
24+
print("\nReport this issue to github.", file=sys.stderr)
25+
notifier = None
26+
else:
27+
if sys.platform == 'win32':
28+
# this is another workaround for windows.
29+
# See https://github.com/samschott/desktop-notifier/issues/95
30+
notifier._did_request_authorisation = True
31+
2732

2833
from trakt_scrobbler.log_config import LOGGING_CONF
2934
from trakt_scrobbler.__version__ import __version__ # noqa

trakt_scrobbler/notifier.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def flatten_categories(categories: dict, parents=[]):
6666
merge_categories(categories, user_notif_categories)
6767
enabled_categories = set(flatten_categories(categories))
6868

69-
if enabled_categories:
69+
if notifier is not None and enabled_categories:
7070
logger.debug(
7171
"Notifications enabled for categories: "
7272
f"{', '.join(sorted(enabled_categories))}"
@@ -92,10 +92,13 @@ def notify_loop():
9292
notif_action_interface = config['general']['notif_actions']['primary_interface'].get(
9393
confuse.Choice(['button', 'click'], default='button')
9494
)
95-
logger.debug(
96-
"Notif actions enabled for categories: "
97-
f"{', '.join(sorted(enabled_notif_action_categories))}"
98-
)
95+
if notifier is not None:
96+
logger.debug(
97+
"Notif actions enabled for categories: "
98+
f"{', '.join(sorted(enabled_notif_action_categories))}"
99+
)
100+
else:
101+
logger.warning("Notifier is not initialized!")
99102

100103

101104
def notify(
@@ -108,6 +111,8 @@ def notify(
108111
):
109112
if stdout:
110113
print(body)
114+
if notifier is None:
115+
return
111116
if category not in enabled_categories:
112117
return
113118
if actions and category in enabled_notif_action_categories:
@@ -119,9 +124,13 @@ def notify(
119124
else:
120125
on_clicked = None
121126
actions = ()
122-
notif_task = notifier.send(
123-
title, body, icon="", on_clicked=on_clicked, buttons=actions
124-
)
127+
try:
128+
notif_task = notifier.send(
129+
title, body, icon="", on_clicked=on_clicked, buttons=actions
130+
)
131+
except Exception:
132+
logger.error("Error when creating notification", exc_info=True)
133+
return
125134
fut = asyncio.run_coroutine_threadsafe(notif_task, notif_loop)
126135
try:
127136
# wait for the notification to be _sent_

0 commit comments

Comments
 (0)