Make better use of defer.Fail for returning deferred errors.
authorCameron Dale <camrdale@gmail.com>
Tue, 15 Apr 2008 23:07:05 +0000 (16:07 -0700)
committerCameron Dale <camrdale@gmail.com>
Tue, 15 Apr 2008 23:07:05 +0000 (16:07 -0700)
apt_p2p/Hash.py
apt_p2p/MirrorManager.py
apt_p2p_Khashmir/DHT.py

index bf23e96..fe26a01 100644 (file)
@@ -157,9 +157,7 @@ class HashObject:
         """Hashes a file in a separate thread, returning a deferred that will callback with the result."""
         file.restat(False)
         if not file.exists():
-            df = defer.Deferred()
-            df.errback(HashError("file not found"))
-            return df
+            return defer.fail(HashError("file not found"))
         
         df = threads.deferToThread(self._hashInThread, file)
         return df
index 5f51175..301c0db 100644 (file)
@@ -111,9 +111,7 @@ class MirrorManager:
         site, baseDir, path = self.extractPath(url)
         if site in self.apt_caches and baseDir in self.apt_caches[site]:
             return self.apt_caches[site][baseDir].findHash(path)
-        d = defer.Deferred()
-        d.errback(MirrorError("Site Not Found"))
-        return d
+        return defer.fail(MirrorError("Site Not Found"))
     
     def cleanup(self):
         for site in self.apt_caches.keys():
index 400b10d..1870841 100644 (file)
@@ -249,15 +249,12 @@ class DHT:
 
     def getValue(self, key):
         """See L{apt_p2p.interfaces.IDHT}."""
-        d = defer.Deferred()
-
         if self.config is None:
-            d.errback(DHTError("configuration not loaded"))
-            return d
+            return defer.fail(DHTError("configuration not loaded"))
         if not self.joined:
-            d.errback(DHTError("have not joined a network yet"))
-            return d
+            return defer.fail(DHTError("have not joined a network yet"))
         
+        d = defer.Deferred()
         key = self._normKey(key)
 
         if key not in self.retrieving:
@@ -283,15 +280,12 @@ class DHT:
 
     def storeValue(self, key, value):
         """See L{apt_p2p.interfaces.IDHT}."""
-        d = defer.Deferred()
-
         if self.config is None:
-            d.errback(DHTError("configuration not loaded"))
-            return d
+            return defer.fail(DHTError("configuration not loaded"))
         if not self.joined:
-            d.errback(DHTError("have not joined a network yet"))
-            return d
+            return defer.fail(DHTError("have not joined a network yet"))
 
+        d = defer.Deferred()
         key = self._normKey(key)
         bvalue = bencode(value)