Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pydbus/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .bus import SystemBus, SessionBus, connect
from .exceptions import DBusException
from gi.repository.GLib import Variant

__all__ = ["SystemBus", "SessionBus", "connect", "Variant"]
__all__ = ["SystemBus", "SessionBus", "connect", "DBusException", "Variant"]
3 changes: 3 additions & 0 deletions pydbus/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class DBusException(Exception):
dbus_name = 'org.freedesktop.DBus.Error.Failed'
silent = False
17 changes: 12 additions & 5 deletions pydbus/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from functools import partial
from .method_call_context import MethodCallContext
import logging
from .exceptions import DBusException

try:
from inspect import signature, Parameter
Expand Down Expand Up @@ -87,15 +88,21 @@ def call_method(self, connection, sender, object_path, interface_name, method_na
else:
invocation.return_value(GLib.Variant("(" + "".join(outargs) + ")", result))

# catch DBusExceptions and turn them into dbus_errors
except DBusException as e:
if not e.silent:
logger = logging.getLogger(__name__)
logger.exception("DBusException while handling %s.%s()", interface_name, method_name)

invocation.return_dbus_error(e.dbus_name, str(e))

# catch all other Exceptions report and reraise them
except Exception as e:
logger = logging.getLogger(__name__)
logger.exception("Exception while handling %s.%s()", interface_name, method_name)

#TODO Think of a better way to translate Python exception types to DBus error types.
e_type = type(e).__name__
if not "." in e_type:
e_type = "unknown." + e_type
invocation.return_dbus_error(e_type, str(e))
invocation.return_dbus_error(DBusException.dbus_name, "Uncought exception while handling.")
raise e

def Get(self, interface_name, property_name):
type = self.readable_properties[interface_name + "." + property_name]
Expand Down
77 changes: 77 additions & 0 deletions tests/dbus_exception.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
from pydbus import SessionBus, DBusException
from gi.repository import GLib
from threading import Thread
import sys

done = 0
loop = GLib.MainLoop()

class CustomDBusException(DBusException):
dbus_name = "net.lew21.pydbus.Test.Error.Custom"
silent = True

class TestObject(object):
'''
<node>
<interface name='net.lvht.ExceptionTest'>
<method name='QuitService'>
</method>
<method name='RaiseException'>
</method>
<method name='RaiseDBusException'>
</method>
</interface>
</node>
'''
def __init__(self, id):
self.id = id

def QuitService(self):
loop.quit()
return None

def RaiseException(self):
raise Exception("sensitive")
return None

def RaiseDBusException(self):
raise CustomDBusException("insensitive")
return None


bus = SessionBus()

with bus.publish("net.lew21.pydbus.Test", TestObject("Main")):
remoteMain = bus.get("net.lew21.pydbus.Test")

def t1_func():
try:
remoteMain.RaiseDBusException()
except Exception as e:
if 'insensitive' not in str(e):
raise e
if CustomDBusException.dbus_name not in str(e):
raise e
try:
remoteMain.RaiseException()
except Exception as e:
if 'sensitive' in str(e):
raise e
if DBusException.dbus_name not in str(e):
raise e
remoteMain.QuitService()

t1 = Thread(None, t1_func)
t1.daemon = True

def handle_timeout():
print("ERROR: Timeout.")
sys.exit(1)

GLib.timeout_add_seconds(2, handle_timeout)

t1.start()

loop.run()

t1.join()
1 change: 1 addition & 0 deletions tests/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ then
"$PYTHON" $TESTS_DIR/publish.py
"$PYTHON" $TESTS_DIR/publish_properties.py
"$PYTHON" $TESTS_DIR/publish_multiface.py
"$PYTHON" $TESTS_DIR/dbus_exception.py
fi