summary refs log tree commit diff
path: root/synapse/util/__init__.py
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2015-05-14 13:31:55 +0100
committerErik Johnston <erik@matrix.org>2015-05-14 13:31:55 +0100
commitcdb3757942fefdcdc3d33b9c6d7c9e44decefd6f (patch)
treede499782f13a8c425b5ddc272b7ffdb2e18829a6 /synapse/util/__init__.py
parentMerge branch 'develop' of github.com:matrix-org/synapse into erikj/perf (diff)
downloadsynapse-cdb3757942fefdcdc3d33b9c6d7c9e44decefd6f.tar.xz
Refactor _get_events
Diffstat (limited to 'synapse/util/__init__.py')
-rw-r--r--synapse/util/__init__.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/synapse/util/__init__.py b/synapse/util/__init__.py
index c1a16b639a..b9afb3364d 100644
--- a/synapse/util/__init__.py
+++ b/synapse/util/__init__.py
@@ -29,6 +29,34 @@ def unwrapFirstError(failure):
     return failure.value.subFailure
 
 
+def unwrap_deferred(d):
+    """Given a deferred that we know has completed, return its value or raise
+    the failure as an exception
+    """
+    if not d.called:
+        raise RuntimeError("deferred has not finished")
+
+    res = []
+
+    def f(r):
+        res.append(r)
+        return r
+    d.addCallback(f)
+
+    if res:
+        return res[0]
+
+    def f(r):
+        res.append(r)
+        return r
+    d.addErrback(f)
+
+    if res:
+        res[0].raiseException()
+    else:
+        raise RuntimeError("deferred did not call callbacks")
+
+
 class Clock(object):
     """A small utility that obtains current time-of-day so that time may be
     mocked during unit-tests.