From 112c7ea315bdf341fc56a190138c290a5b7acffc Mon Sep 17 00:00:00 2001 From: Mark Haines Date: Wed, 3 Sep 2014 09:37:44 +0100 Subject: Set status message for ratelimit error responses --- synapse/api/errors.py | 2 ++ synapse/handlers/_base.py | 2 +- synapse/http/server.py | 14 +++++++++----- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/synapse/api/errors.py b/synapse/api/errors.py index 23ce0af277..ad79bc7ff9 100644 --- a/synapse/api/errors.py +++ b/synapse/api/errors.py @@ -39,6 +39,7 @@ class CodeMessageException(Exception): super(CodeMessageException, self).__init__("%d: %s" % (code, msg)) self.code = code self.msg = msg + self.response_code_message = None def error_dict(self): return cs_error(self.msg) @@ -107,6 +108,7 @@ class LimitExceededError(SynapseError): errcode=Codes.LIMIT_EXCEEDED): super(LimitExceededError, self).__init__(code, msg, errcode) self.retry_after_ms = retry_after_ms + self.response_code_message = "Too Many Requests" def error_dict(self): return cs_error( diff --git a/synapse/handlers/_base.py b/synapse/handlers/_base.py index 935adea1ac..57429ad2ef 100644 --- a/synapse/handlers/_base.py +++ b/synapse/handlers/_base.py @@ -39,7 +39,7 @@ class BaseHandler(object): ) if not allowed: raise LimitExceededError( - retry_after_ms=1000*(time_allowed - time_now), + retry_after_ms=int(1000*(time_allowed - time_now)), ) diff --git a/synapse/http/server.py b/synapse/http/server.py index 0b87718bfa..74c220e869 100644 --- a/synapse/http/server.py +++ b/synapse/http/server.py @@ -140,7 +140,8 @@ class JsonResource(HttpServer, resource.Resource): self._send_response( request, e.code, - cs_exception(e) + cs_exception(e), + response_code_message=e.response_code_message ) except Exception as e: logger.exception(e) @@ -150,7 +151,8 @@ class JsonResource(HttpServer, resource.Resource): {"error": "Internal server error"} ) - def _send_response(self, request, code, response_json_object): + def _send_response(self, request, code, response_json_object, + response_code_message=None): # could alternatively use request.notifyFinish() and flip a flag when # the Deferred fires, but since the flag is RIGHT THERE it seems like # a waste. @@ -166,7 +168,8 @@ class JsonResource(HttpServer, resource.Resource): json_bytes = encode_pretty_printed_json(response_json_object) # TODO: Only enable CORS for the requests that need it. - respond_with_json_bytes(request, code, json_bytes, send_cors=True) + respond_with_json_bytes(request, code, json_bytes, send_cors=True, + response_code_message=response_code_message) @staticmethod def _request_user_agent_is_curl(request): @@ -350,7 +353,8 @@ class ContentRepoResource(resource.Resource): send_cors=True) -def respond_with_json_bytes(request, code, json_bytes, send_cors=False): +def respond_with_json_bytes(request, code, json_bytes, send_cors=False, + response_code_message=None): """Sends encoded JSON in response to the given request. Args: @@ -362,7 +366,7 @@ def respond_with_json_bytes(request, code, json_bytes, send_cors=False): Returns: twisted.web.server.NOT_DONE_YET""" - request.setResponseCode(code) + request.setResponseCode(code, message=response_code_message) request.setHeader(b"Content-Type", b"application/json") if send_cors: -- cgit 1.4.1