Edited /presence REST servlet to raise SynapseErrors to return a standard error response, rather than a string.

This commit is contained in:
Kegan Dougal 2014-09-03 11:56:47 +01:00
parent c54d8df504
commit 10afd895c4

View File

@ -17,6 +17,7 @@
"""
from twisted.internet import defer
from synapse.api.errors import SynapseError
from base import RestServlet, client_path_pattern
import json
@ -58,11 +59,15 @@ class PresenceStatusRestServlet(RestServlet):
if "status_msg" in content:
state["status_msg"] = content.pop("status_msg")
if not isinstance(state["status_msg"], basestring):
raise SynapseError(400, "status_msg must be a string.")
if content:
raise KeyError()
except SynapseError as e:
raise e
except:
defer.returnValue((400, "Unable to parse state"))
raise SynapseError(400, "Unable to parse state")
yield self.handlers.presence_handler.set_state(
target_user=user, auth_user=auth_user, state=state)
@ -83,10 +88,10 @@ class PresenceListRestServlet(RestServlet):
user = self.hs.parse_userid(user_id)
if not user.is_mine:
defer.returnValue((400, "User not hosted on this Home Server"))
raise SynapseError(400, "User not hosted on this Home Server")
if auth_user != user:
defer.returnValue((400, "Cannot get another user's presence list"))
raise SynapseError(400, "Cannot get another user's presence list")
presence = yield self.handlers.presence_handler.get_presence_list(
observer_user=user, accepted=True)
@ -104,17 +109,17 @@ class PresenceListRestServlet(RestServlet):
user = self.hs.parse_userid(user_id)
if not user.is_mine:
defer.returnValue((400, "User not hosted on this Home Server"))
raise SynapseError(400, "User not hosted on this Home Server")
if auth_user != user:
defer.returnValue((
400, "Cannot modify another user's presence list"))
raise SynapseError(
400, "Cannot modify another user's presence list")
try:
content = json.loads(request.content.read())
except:
logger.exception("JSON parse error")
defer.returnValue((400, "Unable to parse content"))
raise SynapseError(400, "Unable to parse content")
deferreds = []