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
1 changed files with 12 additions and 7 deletions

View File

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