Browse Source

added exception handling for (dbus://) SessionBus

pull/175/head
Chris Caron 5 years ago
parent
commit
5ee9065b5b
  1. 14
      apprise/plugins/NotifyDBus.py
  2. 11
      test/test_glib_plugin.py

14
apprise/plugins/NotifyDBus.py

@ -54,6 +54,7 @@ try:
from dbus import Interface from dbus import Interface
from dbus import Byte from dbus import Byte
from dbus import ByteArray from dbus import ByteArray
from dbus import DBusException
# #
# now we try to determine which mainloop(s) we can access # now we try to determine which mainloop(s) we can access
@ -249,8 +250,21 @@ class NotifyDBus(NotifyBase):
return False return False
# Acquire our session # Acquire our session
try:
session = SessionBus(mainloop=MAINLOOP_MAP[self.schema]) 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 # acquire our dbus object
dbus_obj = session.get_object( dbus_obj = session.get_object(
NOTIFY_DBUS_INTERFACE, NOTIFY_DBUS_INTERFACE,

11
test/test_glib_plugin.py

@ -50,6 +50,8 @@ if 'dbus' not in sys.modules:
# Environment doesn't allow for dbus # Environment doesn't allow for dbus
pytest.skip("Skipping dbus-python based tests", allow_module_level=True) pytest.skip("Skipping dbus-python based tests", allow_module_level=True)
from dbus import DBusException # noqa E402
@mock.patch('dbus.SessionBus') @mock.patch('dbus.SessionBus')
@mock.patch('dbus.Interface') @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 # Verify this all works in the event a ValueError is also thronw
# out of the call to gi.require_version() # 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: # Emulate require_version function:
gi.require_version.side_effect = ValueError() gi.require_version.side_effect = ValueError()

Loading…
Cancel
Save