Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Ensure the msg property of HttpResponseException is a string.
Browse files Browse the repository at this point in the history
  • Loading branch information
clokep committed Jul 29, 2020
1 parent 3345c16 commit c697eb5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
1 change: 1 addition & 0 deletions changelog.d/7979.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Switch to the JSON implementation from the standard library and bump the minimum version of the canonicaljson library to 1.2.0.
16 changes: 12 additions & 4 deletions synapse/http/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,9 @@ def post_urlencoded_get_json(self, uri, args={}, headers=None):
if 200 <= response.code < 300:
return json.loads(body.decode("utf-8"))
else:
raise HttpResponseException(response.code, response.phrase, body)
raise HttpResponseException(
response.code, response.phrase.decode("ascii", errors="replace"), body
)

@defer.inlineCallbacks
def post_json_get_json(self, uri, post_json, headers=None):
Expand Down Expand Up @@ -436,7 +438,9 @@ def post_json_get_json(self, uri, post_json, headers=None):
if 200 <= response.code < 300:
return json.loads(body.decode("utf-8"))
else:
raise HttpResponseException(response.code, response.phrase, body)
raise HttpResponseException(
response.code, response.phrase.decode("ascii", errors="replace"), body
)

@defer.inlineCallbacks
def get_json(self, uri, args={}, headers=None):
Expand Down Expand Up @@ -509,7 +513,9 @@ def put_json(self, uri, json_body, args={}, headers=None):
if 200 <= response.code < 300:
return json.loads(body.decode("utf-8"))
else:
raise HttpResponseException(response.code, response.phrase, body)
raise HttpResponseException(
response.code, response.phrase.decode("ascii", errors="replace"), body
)

@defer.inlineCallbacks
def get_raw(self, uri, args={}, headers=None):
Expand Down Expand Up @@ -544,7 +550,9 @@ def get_raw(self, uri, args={}, headers=None):
if 200 <= response.code < 300:
return body
else:
raise HttpResponseException(response.code, response.phrase, body)
raise HttpResponseException(
response.code, response.phrase.decode("ascii", errors="replace"), body
)

# XXX: FIXME: This is horribly copy-pasted from matrixfederationclient.
# The two should be factored out.
Expand Down
7 changes: 4 additions & 3 deletions synapse/http/matrixfederationclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,14 +447,15 @@ def _send_request(
).inc()

set_tag(tags.HTTP_STATUS_CODE, response.code)
response_phrase = response.phrase.decode("ascii", errors="replace")

if 200 <= response.code < 300:
logger.debug(
"{%s} [%s] Got response headers: %d %s",
request.txn_id,
request.destination,
response.code,
response.phrase.decode("ascii", errors="replace"),
response_phrase,
)
pass
else:
Expand All @@ -463,7 +464,7 @@ def _send_request(
request.txn_id,
request.destination,
response.code,
response.phrase.decode("ascii", errors="replace"),
response_phrase,
)
# :'(
# Update transactions table?
Expand All @@ -487,7 +488,7 @@ def _send_request(
)
body = None

e = HttpResponseException(response.code, response.phrase, body)
e = HttpResponseException(response.code, response_phrase, body)

# Retry if the error is a 429 (Too Many Requests),
# otherwise just raise a standard HttpResponseException
Expand Down

0 comments on commit c697eb5

Please sign in to comment.