summary refs log tree commit diff
path: root/synapse/http/client.py
diff options
context:
space:
mode:
authorDavid Baker <dave@matrix.org>2017-04-21 11:32:48 +0100
committerDavid Baker <dave@matrix.org>2017-04-21 11:32:48 +0100
commita90a0f5c8a38ff7f99a12dd436b75680d3fee747 (patch)
tree46cd84b656d2a0c410c19c2c46fdca2a95b289b7 /synapse/http/client.py
parentMerge pull request #2115 from matrix-org/erikj/dedupe_federation_repl (diff)
downloadsynapse-a90a0f5c8a38ff7f99a12dd436b75680d3fee747.tar.xz
Propagate errors sensibly from proxied IS requests
When we're proxying Matrix endpoints, parse out Matrix error
responses and turn them into SynapseErrors so they can be
propagated sensibly upstream.
Diffstat (limited to 'synapse/http/client.py')
-rw-r--r--synapse/http/client.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/synapse/http/client.py b/synapse/http/client.py
index ca2f770f5d..c8b76b2191 100644
--- a/synapse/http/client.py
+++ b/synapse/http/client.py
@@ -145,6 +145,9 @@ class SimpleHttpClient(object):
 
         body = yield preserve_context_over_fn(readBody, response)
 
+        if response.code / 100 != 2:
+            raise CodeMessageException(response.code, body)
+
         defer.returnValue(json.loads(body))
 
     @defer.inlineCallbacks
@@ -306,6 +309,33 @@ class SimpleHttpClient(object):
         defer.returnValue((length, headers, response.request.absoluteURI, response.code))
 
 
+class MatrixProxyClient(object):
+    """
+    An HTTP client that proxies other Matrix endpoints, ie. if the remote endpoint
+    returns Matrix-style error response, this will raise the appropriate SynapseError
+    """
+    def __init__(self, hs):
+        self.simpleHttpClient = SimpleHttpClient(hs)
+
+    @defer.inlineCallbacks
+    def post_json_get_json(self, uri, post_json):
+        try:
+            result = yield self.simpleHttpClient.post_json_get_json(uri, post_json)
+            defer.returnValue(result)
+        except CodeMessageException as cme:
+            ex = None
+            try:
+                errbody = json.loads(cme.msg)
+                errcode = errbody['errcode']
+                errtext = errbody['error']
+                ex = SynapseError(cme.code, errtext, errcode)
+            except:
+                pass
+            if ex is not None:
+                raise ex
+            raise cme
+
+
 # XXX: FIXME: This is horribly copy-pasted from matrixfederationclient.
 # The two should be factored out.