changeset 1752:cbcc223c323a

core (disco): display a warning when a disco info/items request failed, and return empty DiscoInfo/DiscoITems
author Goffi <goffi@goffi.org>
date Thu, 17 Dec 2015 21:58:41 +0100
parents 77870d2e2902
children 27a140aa5023
files src/memory/disco.py
diffstat 1 files changed, 24 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/src/memory/disco.py	Wed Dec 16 13:29:24 2015 +0100
+++ b/src/memory/disco.py	Thu Dec 17 21:58:41 2015 +0100
@@ -34,10 +34,8 @@
 from hashlib import sha1
 
 
-PRESENCE = '/presence'
-NS_ENTITY_CAPABILITY = 'http://jabber.org/protocol/caps'
-CAPABILITY_UPDATE = PRESENCE + '/c[@xmlns="' + NS_ENTITY_CAPABILITY + '"]'
 TIMEOUT = 15
+CAP_HASH_ERROR = 'ERROR'
 
 class HashGenerationError(Exception):
     pass
@@ -64,7 +62,9 @@
     """
 
     def __init__(self, persistent):
-        self.hashes = {}
+        self.hashes = {
+            CAP_HASH_ERROR: disco.DiscoInfo(), # used when we can't get disco infos
+            }
         self.persistent = persistent
 
     def __getitem__(self, key):
@@ -90,6 +90,7 @@
         d.addCallback(fillHashes)
         return d
 
+
 class Discovery(object):
     """ Manage capabilities of entities """
 
@@ -165,8 +166,21 @@
                 self.hashes[cap_hash] = disco_infos
                 self.host.memory.updateEntityData(jid_, C.ENTITY_CAP_HASH, cap_hash, profile_key=client.profile)
                 return disco_infos
+            def infosEb(fail):
+                if fail.check(defer.CancelledError):
+                    reason = u"request time-out"
+                else:
+                    try:
+                        reason = unicode(fail.value)
+                    except AttributeError:
+                        reason = unicode(fail)
+                    log.warning(u"Error while requesting disco infos from {jid}: {reason}".format(jid=jid_.full(), reason=reason))
+                self.host.memory.updateEntityData(jid_, C.ENTITY_CAP_HASH, CAP_HASH_ERROR, profile_key=client.profile)
+                disco_infos = self.hashes[CAP_HASH_ERROR]
+                return disco_infos
             d = client.disco.requestInfo(jid_)
             d.addCallback(infosCb)
+            d.addErrback(infosEb)
             return d
         else:
             disco_infos = self.hashes[cap_hash]
@@ -193,7 +207,12 @@
                 items = yield client.disco.requestItems(jid_, nodeIdentifier)
                 self.host.memory.updateEntityData(jid_, "DISCO_ITEMS", items, profile_key=client.profile)
         else:
-            items = yield client.disco.requestItems(jid_, nodeIdentifier)
+            try:
+                items = yield client.disco.requestItems(jid_, nodeIdentifier)
+            except StanzaError as e:
+                log.warning(u"Error while requesting items for {jid}: {reason}"
+                    .format(jid=jid_.full(), reason=e.condition))
+                items = disco.DiscoItems()
 
         defer.returnValue(items)