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

Indicate what endpoint came back with a JSON response we were unable to parse #14097

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/14097.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Indicate what endpoint came back with a JSON response we were unable to parse.
9 changes: 8 additions & 1 deletion synapse/http/servlet.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from twisted.web.server import Request

from synapse.api.errors import Codes, SynapseError
from synapse.http import redact_uri
from synapse.http.server import HttpServer
from synapse.types import JsonDict, RoomAlias, RoomID
from synapse.util import json_decoder
Expand Down Expand Up @@ -664,7 +665,13 @@ def parse_json_value_from_request(
try:
content = json_decoder.decode(content_bytes.decode("utf-8"))
except Exception as e:
logger.warning("Unable to parse JSON: %s (%s)", e, content_bytes)
logger.warning(
"Unable to parse JSON from %s %s response: %s (%s)",
request.method.decode("ascii", errors="replace"),
redact_uri(request.uri.decode("ascii", errors="replace")),
e,
content_bytes,
)
raise SynapseError(
HTTPStatus.BAD_REQUEST, "Content not JSON.", errcode=Codes.NOT_JSON
)
Expand Down
4 changes: 3 additions & 1 deletion tests/http/test_servlet.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@

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

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

request.method = bytes("STUB_METHOD", "ascii")
request.uri = bytes("/test_stub_uri", "ascii")
request.content = BytesIO(content)
return request

Expand Down