changeset 203:5fdea93b2541

browser side: errback management in bridge calls
author Goffi <goffi@goffi.org>
date Thu, 20 Jun 2013 12:16:46 +0200
parents 2bc6cf004e61
children 890776a6fdb7
files libervia.py
diffstat 1 files changed, 34 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/libervia.py	Thu Jun 20 12:16:46 2013 +0200
+++ b/libervia.py	Thu Jun 20 12:16:46 2013 +0200
@@ -41,39 +41,55 @@
         JSONProxy.__init__(self, *args, **kwargs)
         self.handler=self
         self.cb={}
+        self.eb={}
 
     def call(self, method, cb, *args):
-        id = self.callMethod(method,args)
+        _id = self.callMethod(method,args)
         if cb:
-            self.cb[id] = cb
+            if isinstance(cb, tuple):
+                if len(cb) != 2:
+                    print ("ERROR: tuple syntax for bridge.call is (callback, errback), aborting")
+                    return
+                self.cb[_id] = cb[0]
+                self.eb[_id] = cb[1]
+            else:
+                self.cb[_id] = cb
 
     def onRemoteResponse(self, response, request_info):
-        if self.cb.has_key(request_info.id):
+        if request_info.id in self.cb:
             _cb = self.cb[request_info.id]
-            if isinstance(_cb, tuple):
-                #we have arguments attached to the callback
-                #we send them after the answer
-                callback, args = _cb
-                callback(response, *args)
-            else:
-                #No additional argument, we call directly the callback
-                _cb(response)
+            # if isinstance(_cb, tuple):
+            #     #we have arguments attached to the callback
+            #     #we send them after the answer
+            #     callback, args = _cb
+            #     callback(response, *args)
+            # else:
+            #     #No additional argument, we call directly the callback
+            _cb(response)
             del self.cb[request_info.id]
+            if request_info.id in self.eb:
+                del self.eb[request_info.id]
         
     def onRemoteError(self, code, errobj, request_info):
         """def dump(obj):
             print "\n\nDUMPING %s\n\n" % obj
             for i in dir(obj):
                 print "%s: %s" % (i, getattr(obj,i))"""
-        if code != 0:
-            print ("Internal server error")
-            """for o in code, error, request_info:
-                dump(o)"""
+        if request_info.id in self.eb:
+            _eb = self.eb[request_info.id]
+            _eb((code, errobj))
+            del self.cb[request_info.id]
+            del self.eb[request_info.id]
         else:
-            if isinstance(errobj['message'],dict):
-               print("Error %s: %s" % (errobj['message']['faultCode'], errobj['message']['faultString']))
+            if code != 0:
+                print ("Internal server error")
+                """for o in code, error, request_info:
+                    dump(o)"""
             else:
-               print("Error: %s" % errobj['message'])
+                if isinstance(errobj['message'],dict):
+                   print("Error %s: %s" % (errobj['message']['faultCode'], errobj['message']['faultString']))
+                else:
+                   print("Error: %s" % errobj['message'])
 
 class RegisterCall(LiberviaJsonProxy):
     def __init__(self):