Set status message for ratelimit error responses

This commit is contained in:
Mark Haines 2014-09-03 09:37:44 +01:00
parent 30ad0c5674
commit 112c7ea315
3 changed files with 12 additions and 6 deletions

View File

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

View File

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

View File

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