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

View file

@ -16,6 +16,7 @@
from twisted.internet import defer from twisted.internet import defer
from synapse.api.errors import SynapseError, Codes
from base import RestServlet, client_path_pattern from base import RestServlet, client_path_pattern
import json import json
@ -44,8 +45,10 @@ class ClientDirectoryServer(RestServlet):
@defer.inlineCallbacks @defer.inlineCallbacks
def on_PUT(self, request, room_alias): def on_PUT(self, request, room_alias):
# TODO(erikj): Exceptions content = _parse_json(request)
content = json.loads(request.content.read()) if not "room_id" in content:
raise SynapseError(400, "Missing room_id key",
errcode=Codes.BAD_JSON)
logger.debug("Got content: %s", content) logger.debug("Got content: %s", content)
@ -72,3 +75,14 @@ class ClientDirectoryServer(RestServlet):
logger.exception("Failed to create association") logger.exception("Failed to create association")
defer.returnValue((200, {})) 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)