changeset 1062:95758ef3faa8

bridge: async failures are more detailed (full class name + error message)
author souliane <souliane@mailoo.org>
date Sat, 07 Jun 2014 15:20:39 +0200
parents 3700165d68dc
children 6ec513ad92c2
files frontends/src/bridge/DBus.py frontends/src/bridge/bridge_frontend.py src/bridge/DBus.py src/bridge/bridge_constructor/bridge_constructor.py src/bridge/bridge_constructor/dbus_core_template.py src/bridge/bridge_constructor/dbus_frontend_template.py
diffstat 6 files changed, 69 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/bridge/DBus.py	Mon Jun 02 19:25:06 2014 +0200
+++ b/frontends/src/bridge/DBus.py	Sat Jun 07 15:20:39 2014 +0200
@@ -18,7 +18,7 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 from sat.core.i18n import _
-from bridge_frontend import BridgeFrontend
+from bridge_frontend import BridgeFrontend, BridgeException
 import dbus
 from sat.core.log import getLogger
 log = getLogger(__name__)
@@ -34,6 +34,16 @@
 const_PLUGIN_SUFFIX = ".plugin"
 
 
+def dbus_to_bridge_exception(dbus_e):
+    """Convert a DBusException to a BridgeException.
+
+    @param dbus_e (DBusException)
+    @return: BridgeException
+    """
+    name = dbus_e.get_dbus_name()[len(const_ERROR_PREFIX) + 1:]
+    return BridgeException(name, dbus_e.get_dbus_message())
+
+
 class DBusBridgeFrontend(BridgeFrontend):
     def __init__(self):
         try:
@@ -91,7 +101,7 @@
 
                 if async:
                     kwargs['reply_handler'] = _callback
-                    kwargs['error_handler'] = lambda err: _errback(err._dbus_error_name[len(const_ERROR_PREFIX) + 1:])
+                    kwargs['error_handler'] = lambda err: _errback(dbus_to_bridge_exception(err))
 
                 return method(*args, **kwargs)
 
@@ -100,16 +110,16 @@
         return self.db_core_iface.addContact(entity_jid, profile_key)
 
     def asyncConnect(self, profile_key="@DEFAULT@", password='', callback=None, errback=None):
-        return self.db_core_iface.asyncConnect(profile_key, password, reply_handler=callback, error_handler=lambda err:errback(err._dbus_error_name[len(const_ERROR_PREFIX)+1:]))
+        return self.db_core_iface.asyncConnect(profile_key, password, reply_handler=callback, error_handler=lambda err:errback(dbus_to_bridge_exception(err)))
 
     def asyncCreateProfile(self, profile, password='', callback=None, errback=None):
-        return self.db_core_iface.asyncCreateProfile(profile, password, reply_handler=callback, error_handler=lambda err:errback(err._dbus_error_name[len(const_ERROR_PREFIX)+1:]))
+        return self.db_core_iface.asyncCreateProfile(profile, password, reply_handler=callback, error_handler=lambda err:errback(dbus_to_bridge_exception(err)))
 
     def asyncDeleteProfile(self, profile, callback=None, errback=None):
-        return self.db_core_iface.asyncDeleteProfile(profile, reply_handler=callback, error_handler=lambda err:errback(err._dbus_error_name[len(const_ERROR_PREFIX)+1:]))
+        return self.db_core_iface.asyncDeleteProfile(profile, reply_handler=callback, error_handler=lambda err:errback(dbus_to_bridge_exception(err)))
 
     def asyncGetParamA(self, name, category, attribute="value", security_limit=-1, profile_key="@DEFAULT@", callback=None, errback=None):
-        return unicode(self.db_core_iface.asyncGetParamA(name, category, attribute, security_limit, profile_key, reply_handler=callback, error_handler=lambda err:errback(err._dbus_error_name[len(const_ERROR_PREFIX)+1:])))
+        return unicode(self.db_core_iface.asyncGetParamA(name, category, attribute, security_limit, profile_key, reply_handler=callback, error_handler=lambda err:errback(dbus_to_bridge_exception(err))))
 
     def confirmationAnswer(self, id, accepted, data, profile):
         return self.db_core_iface.confirmationAnswer(id, accepted, data, profile)
@@ -118,10 +128,10 @@
         return self.db_core_iface.delContact(entity_jid, profile_key)
 
     def discoInfos(self, entity_jid, profile_key, callback=None, errback=None):
-        return self.db_core_iface.discoInfos(entity_jid, profile_key, reply_handler=callback, error_handler=lambda err:errback(err._dbus_error_name[len(const_ERROR_PREFIX)+1:]))
+        return self.db_core_iface.discoInfos(entity_jid, profile_key, reply_handler=callback, error_handler=lambda err:errback(dbus_to_bridge_exception(err)))
 
     def discoItems(self, entity_jid, profile_key, callback=None, errback=None):
-        return self.db_core_iface.discoItems(entity_jid, profile_key, reply_handler=callback, error_handler=lambda err:errback(err._dbus_error_name[len(const_ERROR_PREFIX)+1:]))
+        return self.db_core_iface.discoItems(entity_jid, profile_key, reply_handler=callback, error_handler=lambda err:errback(dbus_to_bridge_exception(err)))
 
     def disconnect(self, profile_key="@DEFAULT@"):
         return self.db_core_iface.disconnect(profile_key)
@@ -139,7 +149,7 @@
         return self.db_core_iface.getEntityData(jid, keys, profile)
 
     def getHistory(self, from_jid, to_jid, limit, between=True, profile="@NONE@", callback=None, errback=None):
-        return self.db_core_iface.getHistory(from_jid, to_jid, limit, between, profile, reply_handler=callback, error_handler=lambda err:errback(err._dbus_error_name[len(const_ERROR_PREFIX)+1:]))
+        return self.db_core_iface.getHistory(from_jid, to_jid, limit, between, profile, reply_handler=callback, error_handler=lambda err:errback(dbus_to_bridge_exception(err)))
 
     def getLastResource(self, contact_jid, profile_key="@DEFAULT@"):
         return unicode(self.db_core_iface.getLastResource(contact_jid, profile_key))
@@ -154,16 +164,16 @@
         return unicode(self.db_core_iface.getParamA(name, category, attribute, profile_key))
 
     def getParams(self, security_limit=-1, app='', profile_key="@DEFAULT@", callback=None, errback=None):
-        return unicode(self.db_core_iface.getParams(security_limit, app, profile_key, reply_handler=callback, error_handler=lambda err:errback(err._dbus_error_name[len(const_ERROR_PREFIX)+1:])))
+        return unicode(self.db_core_iface.getParams(security_limit, app, profile_key, reply_handler=callback, error_handler=lambda err:errback(dbus_to_bridge_exception(err))))
 
     def getParamsCategories(self, ):
         return self.db_core_iface.getParamsCategories()
 
     def getParamsForCategory(self, category, security_limit=-1, app='', profile_key="@DEFAULT@", callback=None, errback=None):
-        return unicode(self.db_core_iface.getParamsForCategory(category, security_limit, app, profile_key, reply_handler=callback, error_handler=lambda err:errback(err._dbus_error_name[len(const_ERROR_PREFIX)+1:])))
+        return unicode(self.db_core_iface.getParamsForCategory(category, security_limit, app, profile_key, reply_handler=callback, error_handler=lambda err:errback(dbus_to_bridge_exception(err))))
 
     def getParamsUI(self, security_limit=-1, app='', profile_key="@DEFAULT@", callback=None, errback=None):
-        return unicode(self.db_core_iface.getParamsUI(security_limit, app, profile_key, reply_handler=callback, error_handler=lambda err:errback(err._dbus_error_name[len(const_ERROR_PREFIX)+1:])))
+        return unicode(self.db_core_iface.getParamsUI(security_limit, app, profile_key, reply_handler=callback, error_handler=lambda err:errback(dbus_to_bridge_exception(err))))
 
     def getPresenceStatuses(self, profile_key="@DEFAULT@"):
         return self.db_core_iface.getPresenceStatuses(profile_key)
@@ -178,7 +188,7 @@
         return self.db_core_iface.getProgress(id, profile)
 
     def getReady(self, callback=None, errback=None):
-        return self.db_core_iface.getReady(reply_handler=callback, error_handler=lambda err:errback(err._dbus_error_name[len(const_ERROR_PREFIX)+1:]))
+        return self.db_core_iface.getReady(reply_handler=callback, error_handler=lambda err:errback(dbus_to_bridge_exception(err)))
 
     def getVersion(self, ):
         return unicode(self.db_core_iface.getVersion())
@@ -193,7 +203,7 @@
         return self.db_core_iface.isConnected(profile_key)
 
     def launchAction(self, callback_id, data, profile_key="@DEFAULT@", callback=None, errback=None):
-        return self.db_core_iface.launchAction(callback_id, data, profile_key, reply_handler=callback, error_handler=lambda err:errback(err._dbus_error_name[len(const_ERROR_PREFIX)+1:]))
+        return self.db_core_iface.launchAction(callback_id, data, profile_key, reply_handler=callback, error_handler=lambda err:errback(dbus_to_bridge_exception(err)))
 
     def loadParamsTemplate(self, filename):
         return self.db_core_iface.loadParamsTemplate(filename)
@@ -205,7 +215,7 @@
         return self.db_core_iface.saveParamsTemplate(filename)
 
     def sendMessage(self, to_jid, message, subject='', mess_type="auto", extra={}, profile_key="@NONE@", callback=None, errback=None):
-        return self.db_core_iface.sendMessage(to_jid, message, subject, mess_type, extra, profile_key, reply_handler=callback, error_handler=lambda err:errback(err._dbus_error_name[len(const_ERROR_PREFIX)+1:]))
+        return self.db_core_iface.sendMessage(to_jid, message, subject, mess_type, extra, profile_key, reply_handler=callback, error_handler=lambda err:errback(dbus_to_bridge_exception(err)))
 
     def setParam(self, name, value, category, security_limit=-1, profile_key="@DEFAULT@"):
         return self.db_core_iface.setParam(name, value, category, security_limit, profile_key)
--- a/frontends/src/bridge/bridge_frontend.py	Mon Jun 02 19:25:06 2014 +0200
+++ b/frontends/src/bridge/bridge_frontend.py	Sat Jun 07 15:20:39 2014 +0200
@@ -17,10 +17,32 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+
 class BridgeFrontend(object):
     def __init__(self):
         print "Bridge frontend initialization"
 
-
     def register(self, functionName, handler):
         raise NotImplementedError
+
+
+class BridgeException(Exception):
+    """An exception which has been raised from the backend and arrived to the frontend."""
+
+    def __init__(self, name, message):
+        """
+
+        @param name (str): full exception class name (with module)
+        @param message (str): error message
+        """
+        Exception.__init__(self)
+        self.fullname = unicode(name)
+        self.message = unicode(message)
+        self.module, dummy, self.classname = unicode(self.fullname).rpartition('.')
+
+    def __str__(self):
+        message = (': %s' % self.message) if self.message else ''
+        return self.classname + message
+
+    def __eq__(self, other):
+        return self.classname == other
--- a/src/bridge/DBus.py	Mon Jun 02 19:25:06 2014 +0200
+++ b/src/bridge/DBus.py	Sat Jun 07 15:20:39 2014 +0200
@@ -59,12 +59,13 @@
     def __init__(self, twisted_error):
         super(GenericException, self).__init__()
         try:
-            error_name = str(twisted_error.value().__class__.__name__)
             # twisted_error.value is a class
+            class_ = twisted_error.value().__class__
         except TypeError:
             # twisted_error.value is an instance
-            error_name = str(twisted_error.value.__class__.__name__)
-        self._dbus_error_name = const_ERROR_PREFIX + "." + error_name
+            class_ = twisted_error.value.__class__
+            self.args = (twisted_error.getErrorMessage(),)
+        self._dbus_error_name = '.'.join([const_ERROR_PREFIX, class_.__module__, class_.__name__])
 
 
 class DbusObject(dbus.service.Object):
--- a/src/bridge/bridge_constructor/bridge_constructor.py	Mon Jun 02 19:25:06 2014 +0200
+++ b/src/bridge/bridge_constructor/bridge_constructor.py	Sat Jun 07 15:20:39 2014 +0200
@@ -450,7 +450,7 @@
                 completion['args_result'] = self.getArguments(function['sig_in'], name=arg_doc)
                 completion['async_args'] = 'callback=None, errback=None' if async else ''
                 completion['async_comma'] = ', ' if async and function['sig_in'] else ''
-                completion['async_args_result'] = 'reply_handler=callback, error_handler=lambda err:errback(err._dbus_error_name[len(const_ERROR_PREFIX)+1:])' if async else ''
+                completion['async_args_result'] = 'reply_handler=callback, error_handler=lambda err:errback(dbus_to_bridge_exception(err))' if async else ''
                 result = "self.db_%(category)s_iface.%(name)s(%(args_result)s%(async_comma)s%(async_args_result)s)" % completion
                 completion['result'] = ("unicode(%s)" if self.options.unicode and function['sig_out'] == 's' else "%s") % result
                 methods_part.append("""\
--- a/src/bridge/bridge_constructor/dbus_core_template.py	Mon Jun 02 19:25:06 2014 +0200
+++ b/src/bridge/bridge_constructor/dbus_core_template.py	Sat Jun 07 15:20:39 2014 +0200
@@ -59,12 +59,13 @@
     def __init__(self, twisted_error):
         super(GenericException, self).__init__()
         try:
-            error_name = str(twisted_error.value().__class__.__name__)
             # twisted_error.value is a class
+            class_ = twisted_error.value().__class__
         except TypeError:
             # twisted_error.value is an instance
-            error_name = str(twisted_error.value.__class__.__name__)
-        self._dbus_error_name = const_ERROR_PREFIX + "." + error_name
+            class_ = twisted_error.value.__class__
+            self.args = (twisted_error.getErrorMessage(),)
+        self._dbus_error_name = '.'.join([const_ERROR_PREFIX, class_.__module__, class_.__name__])
 
 
 class DbusObject(dbus.service.Object):
--- a/src/bridge/bridge_constructor/dbus_frontend_template.py	Mon Jun 02 19:25:06 2014 +0200
+++ b/src/bridge/bridge_constructor/dbus_frontend_template.py	Sat Jun 07 15:20:39 2014 +0200
@@ -18,7 +18,7 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 from sat.core.i18n import _
-from bridge_frontend import BridgeFrontend
+from bridge_frontend import BridgeFrontend, BridgeException
 import dbus
 from sat.core.log import getLogger
 log = getLogger(__name__)
@@ -34,6 +34,16 @@
 const_PLUGIN_SUFFIX = ".plugin"
 
 
+def dbus_to_bridge_exception(dbus_e):
+    """Convert a DBusException to a BridgeException.
+
+    @param dbus_e (DBusException)
+    @return: BridgeException
+    """
+    name = dbus_e.get_dbus_name()[len(const_ERROR_PREFIX) + 1:]
+    return BridgeException(name, dbus_e.get_dbus_message())
+
+
 class DBusBridgeFrontend(BridgeFrontend):
     def __init__(self):
         try:
@@ -91,7 +101,7 @@
 
                 if async:
                     kwargs['reply_handler'] = _callback
-                    kwargs['error_handler'] = lambda err: _errback(err._dbus_error_name[len(const_ERROR_PREFIX) + 1:])
+                    kwargs['error_handler'] = lambda err: _errback(dbus_to_bridge_exception(err))
 
                 return method(*args, **kwargs)