Add exception handling to directory servlet, so we don't 500. Mark directory API as volatile in the api docs.

This commit is contained in:
Kegan Dougal 2014-09-03 16:27:01 +01:00
parent 30bcbc433a
commit 581c54bebe
2 changed files with 18 additions and 2 deletions

View File

@ -13,6 +13,7 @@
{
"method": "GET",
"summary": "Get the room ID corresponding to this room alias.",
"notes": "Volatile: This API is likely to change.",
"type": "DirectoryResponse",
"nickname": "get_room_id_for_alias",
"parameters": [
@ -28,6 +29,7 @@
{
"method": "PUT",
"summary": "Create a new mapping from room alias to room ID.",
"notes": "Volatile: This API is likely to change.",
"type": "void",
"nickname": "add_room_alias",
"parameters": [

View File

@ -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)