bridge: DBusException also transports the twisted failure condition
This commit is contained in:
parent
8867ac6c60
commit
bba25dd62d
5 changed files with 45 additions and 5 deletions
|
|
@ -27,6 +27,8 @@ from sat.core.exceptions import BridgeExceptionNoService, BridgeInitError
|
||||||
from dbus.mainloop.glib import DBusGMainLoop
|
from dbus.mainloop.glib import DBusGMainLoop
|
||||||
DBusGMainLoop(set_as_default=True)
|
DBusGMainLoop(set_as_default=True)
|
||||||
|
|
||||||
|
import ast
|
||||||
|
|
||||||
const_INT_PREFIX = "org.goffi.SAT" # Interface prefix
|
const_INT_PREFIX = "org.goffi.SAT" # Interface prefix
|
||||||
const_ERROR_PREFIX = const_INT_PREFIX + ".error"
|
const_ERROR_PREFIX = const_INT_PREFIX + ".error"
|
||||||
const_OBJ_PATH = '/org/goffi/SAT/bridge'
|
const_OBJ_PATH = '/org/goffi/SAT/bridge'
|
||||||
|
|
@ -42,7 +44,15 @@ def dbus_to_bridge_exception(dbus_e):
|
||||||
@return: BridgeException
|
@return: BridgeException
|
||||||
"""
|
"""
|
||||||
name = dbus_e.get_dbus_name()[len(const_ERROR_PREFIX) + 1:]
|
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):
|
class DBusBridgeFrontend(BridgeFrontend):
|
||||||
|
|
|
||||||
|
|
@ -29,15 +29,17 @@ class BridgeFrontend(object):
|
||||||
class BridgeException(Exception):
|
class BridgeException(Exception):
|
||||||
"""An exception which has been raised from the backend and arrived to the frontend."""
|
"""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 name (str): full exception class name (with module)
|
||||||
@param message (str): error message
|
@param message (str): error message
|
||||||
|
@param condition (str) : error condition
|
||||||
"""
|
"""
|
||||||
Exception.__init__(self)
|
Exception.__init__(self)
|
||||||
self.fullname = unicode(name)
|
self.fullname = unicode(name)
|
||||||
self.message = unicode(message)
|
self.message = unicode(message)
|
||||||
|
self.condition = unicode(condition) if condition else ''
|
||||||
self.module, dummy, self.classname = unicode(self.fullname).rpartition('.')
|
self.module, dummy, self.classname = unicode(self.fullname).rpartition('.')
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
|
|
||||||
|
|
@ -57,6 +57,11 @@ class DeferredNotAsync(dbus.DBusException):
|
||||||
|
|
||||||
class GenericException(dbus.DBusException):
|
class GenericException(dbus.DBusException):
|
||||||
def __init__(self, twisted_error):
|
def __init__(self, twisted_error):
|
||||||
|
"""
|
||||||
|
|
||||||
|
@param twisted_error (Failure): instance of twisted Failure
|
||||||
|
@return: DBusException
|
||||||
|
"""
|
||||||
super(GenericException, self).__init__()
|
super(GenericException, self).__init__()
|
||||||
try:
|
try:
|
||||||
# twisted_error.value is a class
|
# twisted_error.value is a class
|
||||||
|
|
@ -64,7 +69,11 @@ class GenericException(dbus.DBusException):
|
||||||
except TypeError:
|
except TypeError:
|
||||||
# twisted_error.value is an instance
|
# twisted_error.value is an instance
|
||||||
class_ = twisted_error.value.__class__
|
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__])
|
self._dbus_error_name = '.'.join([const_ERROR_PREFIX, class_.__module__, class_.__name__])
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -57,6 +57,11 @@ class DeferredNotAsync(dbus.DBusException):
|
||||||
|
|
||||||
class GenericException(dbus.DBusException):
|
class GenericException(dbus.DBusException):
|
||||||
def __init__(self, twisted_error):
|
def __init__(self, twisted_error):
|
||||||
|
"""
|
||||||
|
|
||||||
|
@param twisted_error (Failure): instance of twisted Failure
|
||||||
|
@return: DBusException
|
||||||
|
"""
|
||||||
super(GenericException, self).__init__()
|
super(GenericException, self).__init__()
|
||||||
try:
|
try:
|
||||||
# twisted_error.value is a class
|
# twisted_error.value is a class
|
||||||
|
|
@ -64,7 +69,11 @@ class GenericException(dbus.DBusException):
|
||||||
except TypeError:
|
except TypeError:
|
||||||
# twisted_error.value is an instance
|
# twisted_error.value is an instance
|
||||||
class_ = twisted_error.value.__class__
|
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__])
|
self._dbus_error_name = '.'.join([const_ERROR_PREFIX, class_.__module__, class_.__name__])
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,8 @@ from sat.core.exceptions import BridgeExceptionNoService, BridgeInitError
|
||||||
from dbus.mainloop.glib import DBusGMainLoop
|
from dbus.mainloop.glib import DBusGMainLoop
|
||||||
DBusGMainLoop(set_as_default=True)
|
DBusGMainLoop(set_as_default=True)
|
||||||
|
|
||||||
|
import ast
|
||||||
|
|
||||||
const_INT_PREFIX = "org.goffi.SAT" # Interface prefix
|
const_INT_PREFIX = "org.goffi.SAT" # Interface prefix
|
||||||
const_ERROR_PREFIX = const_INT_PREFIX + ".error"
|
const_ERROR_PREFIX = const_INT_PREFIX + ".error"
|
||||||
const_OBJ_PATH = '/org/goffi/SAT/bridge'
|
const_OBJ_PATH = '/org/goffi/SAT/bridge'
|
||||||
|
|
@ -42,7 +44,15 @@ def dbus_to_bridge_exception(dbus_e):
|
||||||
@return: BridgeException
|
@return: BridgeException
|
||||||
"""
|
"""
|
||||||
name = dbus_e.get_dbus_name()[len(const_ERROR_PREFIX) + 1:]
|
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):
|
class DBusBridgeFrontend(BridgeFrontend):
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue