mirror of
https://git.anonymousland.org/anonymousland/synapse.git
synced 2025-05-02 14:06:02 -04:00
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.
This commit is contained in:
parent
247c736b9b
commit
a90a0f5c8a
3 changed files with 42 additions and 6 deletions
|
@ -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.
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue