diff options
author | Kegan Dougal <kegan@matrix.org> | 2014-09-03 16:27:01 +0100 |
---|---|---|
committer | Kegan Dougal <kegan@matrix.org> | 2014-09-03 16:27:01 +0100 |
commit | 581c54bebef865039352f4ec141c98413cb45f53 (patch) | |
tree | 4f249c2cbe11122152594a96eec1b2a59fb673d8 /synapse | |
parent | Fix up directory server to not require uploading room hosts. Update the room ... (diff) | |
download | synapse-581c54bebef865039352f4ec141c98413cb45f53.tar.xz |
Add exception handling to directory servlet, so we don't 500. Mark directory API as volatile in the api docs.
Diffstat (limited to '')
-rw-r--r-- | synapse/rest/directory.py | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/synapse/rest/directory.py b/synapse/rest/directory.py index 29ace4f1d3..d02b15395b 100644 --- a/synapse/rest/directory.py +++ b/synapse/rest/directory.py @@ -16,6 +16,7 @@ from twisted.internet import defer +from synapse.api.errors import SynapseError, Codes from base import RestServlet, client_path_pattern import json @@ -44,8 +45,10 @@ class ClientDirectoryServer(RestServlet): @defer.inlineCallbacks def on_PUT(self, request, room_alias): - # TODO(erikj): Exceptions - content = json.loads(request.content.read()) + content = _parse_json(request) + if not "room_id" in content: + raise SynapseError(400, "Missing room_id key", + errcode=Codes.BAD_JSON) logger.debug("Got content: %s", content) @@ -72,3 +75,14 @@ class ClientDirectoryServer(RestServlet): logger.exception("Failed to create association") defer.returnValue((200, {})) + + +def _parse_json(request): + try: + content = json.loads(request.content.read()) + if type(content) != dict: + raise SynapseError(400, "Content must be a JSON object.", + errcode=Codes.NOT_JSON) + return content + except ValueError: + raise SynapseError(400, "Content not JSON.", errcode=Codes.NOT_JSON) |