diff --git a/frontends/src/bridge/DBus.py b/frontends/src/bridge/DBus.py index b98bba0f..dce89993 100644 --- a/frontends/src/bridge/DBus.py +++ b/frontends/src/bridge/DBus.py @@ -27,6 +27,8 @@ from sat.core.exceptions import BridgeExceptionNoService, BridgeInitError from dbus.mainloop.glib import DBusGMainLoop DBusGMainLoop(set_as_default=True) +import ast + const_INT_PREFIX = "org.goffi.SAT" # Interface prefix const_ERROR_PREFIX = const_INT_PREFIX + ".error" const_OBJ_PATH = '/org/goffi/SAT/bridge' @@ -42,7 +44,15 @@ def dbus_to_bridge_exception(dbus_e): @return: BridgeException """ name = dbus_e.get_dbus_name()[len(const_ERROR_PREFIX) + 1:] - return BridgeException(name, dbus_e.get_dbus_message()) + # XXX: dbus_e.args doesn't contain the original DBusException args, but we + # receive its serialized form in dbus_e.args[0]. From that we can rebuild + # the original arguments list thanks to ast.literal_eval (secure eval). + message = dbus_e.get_dbus_message() # similar to dbus_e.args[0] + try: + message, condition = ast.literal_eval(message) + except (SyntaxError, ValueError, TypeError): + condition = '' + return BridgeException(name, message, condition) class DBusBridgeFrontend(BridgeFrontend): diff --git a/frontends/src/bridge/bridge_frontend.py b/frontends/src/bridge/bridge_frontend.py index 7543311a..e29f642a 100755 --- a/frontends/src/bridge/bridge_frontend.py +++ b/frontends/src/bridge/bridge_frontend.py @@ -29,15 +29,17 @@ class BridgeFrontend(object): class BridgeException(Exception): """An exception which has been raised from the backend and arrived to the frontend.""" - def __init__(self, name, message): + def __init__(self, name, message='', condition=''): """ @param name (str): full exception class name (with module) @param message (str): error message + @param condition (str) : error condition """ Exception.__init__(self) self.fullname = unicode(name) self.message = unicode(message) + self.condition = unicode(condition) if condition else '' self.module, dummy, self.classname = unicode(self.fullname).rpartition('.') def __str__(self): diff --git a/src/bridge/DBus.py b/src/bridge/DBus.py index 44c14fea..9040837b 100755 --- a/src/bridge/DBus.py +++ b/src/bridge/DBus.py @@ -57,6 +57,11 @@ class DeferredNotAsync(dbus.DBusException): class GenericException(dbus.DBusException): def __init__(self, twisted_error): + """ + + @param twisted_error (Failure): instance of twisted Failure + @return: DBusException + """ super(GenericException, self).__init__() try: # twisted_error.value is a class @@ -64,7 +69,11 @@ class GenericException(dbus.DBusException): except TypeError: # twisted_error.value is an instance class_ = twisted_error.value.__class__ - self.args = (twisted_error.getErrorMessage(),) + message = twisted_error.getErrorMessage() + try: + self.args = (message, twisted_error.value.condition) + except AttributeError: + self.args = (message,) self._dbus_error_name = '.'.join([const_ERROR_PREFIX, class_.__module__, class_.__name__]) diff --git a/src/bridge/bridge_constructor/dbus_core_template.py b/src/bridge/bridge_constructor/dbus_core_template.py index 93e6e23b..99b464e5 100644 --- a/src/bridge/bridge_constructor/dbus_core_template.py +++ b/src/bridge/bridge_constructor/dbus_core_template.py @@ -57,6 +57,11 @@ class DeferredNotAsync(dbus.DBusException): class GenericException(dbus.DBusException): def __init__(self, twisted_error): + """ + + @param twisted_error (Failure): instance of twisted Failure + @return: DBusException + """ super(GenericException, self).__init__() try: # twisted_error.value is a class @@ -64,7 +69,11 @@ class GenericException(dbus.DBusException): except TypeError: # twisted_error.value is an instance class_ = twisted_error.value.__class__ - self.args = (twisted_error.getErrorMessage(),) + message = twisted_error.getErrorMessage() + try: + self.args = (message, twisted_error.value.condition) + except AttributeError: + self.args = (message,) self._dbus_error_name = '.'.join([const_ERROR_PREFIX, class_.__module__, class_.__name__]) diff --git a/src/bridge/bridge_constructor/dbus_frontend_template.py b/src/bridge/bridge_constructor/dbus_frontend_template.py index f75c79d1..3249dbbc 100644 --- a/src/bridge/bridge_constructor/dbus_frontend_template.py +++ b/src/bridge/bridge_constructor/dbus_frontend_template.py @@ -27,6 +27,8 @@ from sat.core.exceptions import BridgeExceptionNoService, BridgeInitError from dbus.mainloop.glib import DBusGMainLoop DBusGMainLoop(set_as_default=True) +import ast + const_INT_PREFIX = "org.goffi.SAT" # Interface prefix const_ERROR_PREFIX = const_INT_PREFIX + ".error" const_OBJ_PATH = '/org/goffi/SAT/bridge' @@ -42,7 +44,15 @@ def dbus_to_bridge_exception(dbus_e): @return: BridgeException """ name = dbus_e.get_dbus_name()[len(const_ERROR_PREFIX) + 1:] - return BridgeException(name, dbus_e.get_dbus_message()) + # XXX: dbus_e.args doesn't contain the original DBusException args, but we + # receive its serialized form in dbus_e.args[0]. From that we can rebuild + # the original arguments list thanks to ast.literal_eval (secure eval). + message = dbus_e.get_dbus_message() # similar to dbus_e.args[0] + try: + message, condition = ast.literal_eval(message) + except (SyntaxError, ValueError, TypeError): + condition = '' + return BridgeException(name, message, condition) class DBusBridgeFrontend(BridgeFrontend):