diff --git a/apprise/plugins/NotifyDBus.py b/apprise/plugins/NotifyDBus.py index a1da9724..92b99eae 100644 --- a/apprise/plugins/NotifyDBus.py +++ b/apprise/plugins/NotifyDBus.py @@ -54,6 +54,7 @@ try: from dbus import Interface from dbus import Byte from dbus import ByteArray + from dbus import DBusException # # now we try to determine which mainloop(s) we can access @@ -249,7 +250,20 @@ class NotifyDBus(NotifyBase): return False # Acquire our session - session = SessionBus(mainloop=MAINLOOP_MAP[self.schema]) + try: + session = SessionBus(mainloop=MAINLOOP_MAP[self.schema]) + + except DBusException: + # Handle exception + self.logger.warning('Failed to send DBus notification.') + self.logger.exception('DBus Exception') + return False + + # If there is no title, but there is a body, swap the two to get rid + # of the weird whitespace + if not title: + title = body + body = '' # acquire our dbus object dbus_obj = session.get_object( diff --git a/test/test_glib_plugin.py b/test/test_glib_plugin.py index aa2ca8d1..07d89e12 100644 --- a/test/test_glib_plugin.py +++ b/test/test_glib_plugin.py @@ -50,6 +50,8 @@ if 'dbus' not in sys.modules: # Environment doesn't allow for dbus pytest.skip("Skipping dbus-python based tests", allow_module_level=True) +from dbus import DBusException # noqa E402 + @mock.patch('dbus.SessionBus') @mock.patch('dbus.Interface') @@ -344,6 +346,15 @@ def test_dbus_plugin(mock_mainloop, mock_byte, mock_bytearray, # Verify this all works in the event a ValueError is also thronw # out of the call to gi.require_version() + mock_sessionbus.side_effect = DBusException('test') + # Handle Dbus Session Initialization error + assert obj.notify( + title='title', body='body', + notify_type=apprise.NotifyType.INFO) is False + + # Return side effect to normal + mock_sessionbus.side_effect = None + # Emulate require_version function: gi.require_version.side_effect = ValueError()