-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Propagate errors sensibly from proxied IS requests #2147
Changes from 2 commits
a90a0f5
2e16529
a1595ce
70caf49
a46982c
1a9255c
c366276
8180490
82ae023
5fd12dc
c038040
482a2ad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -145,6 +145,9 @@ def post_json_get_json(self, uri, post_json): | |
|
||
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 @@ def get_file(self, url, output_stream, max_size=None): | |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a bit confused - why can't we just pass the Matrix-style error response through to the client? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Basically we just want to indicate that an error has occurred, and since post_json_get_json just returns the body, previously we were masking the error status code and behaving as if the endpoint returned 2xx which confused things. The main thing is that we raise an exception on error responses, and if we turn it into a SynapseException then it just works without having to handle it specifically in all the different places. |
||
""" | ||
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. | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we really want to raise an exception on 300s?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably not, yeah