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

Commit 1bf2832

Browse files
Indicate what endpoint came back with a JSON response we were unable to parse (#14097)
**Before:** ``` WARNING - POST-11 - Unable to parse JSON: Expecting value: line 1 column 1 (char 0) (b'') ``` **After:** ``` WARNING - POST-11 - Unable to parse JSON from POST /_matrix/client/v3/join/%21ZlmJtelqFroDRJYZaq:hs1?server_name=hs1 response: Expecting value: line 1 column 1 (char 0) (b'') ``` --- It's possible to figure out which endpoint these warnings were coming from before but you had to follow the request ID `POST-11` to the log line that says `Completed request [...]`. Including this key information next to the JSON parsing error makes it much easier to reason whether it matters or not. ``` 2022-09-29T08:23:25.7875506Z synapse_main | 2022-09-29 08:21:10,336 - synapse.http.matrixfederationclient - 299 - INFO - POST-11 - {GET-O-13} [hs1] Completed request: 200 OK in 0.53 secs, got 450 bytes - GET matrix://hs1/_matrix/federation/v1/make_join/%21ohtKoQiXlPePSycXwp%3Ahs1/%40charlie%3Ahs2?ver=1&ver=2&ver=3&ver=4&ver=5&ver=6&ver=org.matrix.msc2176&ver=7&ver=8&ver=9&ver=org.matrix.msc3787&ver=10&ver=org.matrix.msc2716v4 ``` --- As a note, having no `body` is normal for the `/join` endpoint and it can handle it. https://github.com/matrix-org/synapse/blob/0c853e09709d52783efd37060ed9e8f55a4fc704/synapse/rest/client/room.py#L398-L403 Alternatively we could remove these extra logs but they are probably more usually helpful to figure out what went wrong.
1 parent e03d7c5 commit 1bf2832

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

changelog.d/14097.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Indicate what endpoint came back with a JSON response we were unable to parse.

synapse/http/servlet.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from twisted.web.server import Request
3636

3737
from synapse.api.errors import Codes, SynapseError
38+
from synapse.http import redact_uri
3839
from synapse.http.server import HttpServer
3940
from synapse.types import JsonDict, RoomAlias, RoomID
4041
from synapse.util import json_decoder
@@ -664,7 +665,13 @@ def parse_json_value_from_request(
664665
try:
665666
content = json_decoder.decode(content_bytes.decode("utf-8"))
666667
except Exception as e:
667-
logger.warning("Unable to parse JSON: %s (%s)", e, content_bytes)
668+
logger.warning(
669+
"Unable to parse JSON from %s %s response: %s (%s)",
670+
request.method.decode("ascii", errors="replace"),
671+
redact_uri(request.uri.decode("ascii", errors="replace")),
672+
e,
673+
content_bytes,
674+
)
668675
raise SynapseError(
669676
HTTPStatus.BAD_REQUEST, "Content not JSON.", errcode=Codes.NOT_JSON
670677
)

tests/http/test_servlet.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@
3535

3636
def make_request(content):
3737
"""Make an object that acts enough like a request."""
38-
request = Mock(spec=["content"])
38+
request = Mock(spec=["method", "uri", "content"])
3939

4040
if isinstance(content, dict):
4141
content = json.dumps(content).encode("utf8")
4242

43+
request.method = bytes("STUB_METHOD", "ascii")
44+
request.uri = bytes("/test_stub_uri", "ascii")
4345
request.content = BytesIO(content)
4446
return request
4547

0 commit comments

Comments
 (0)