exceptions: Make RateLimited into a subclass of JsonableError.

This simplifies the code, as it allows using the mechanism of converting
JsonableErrors into a response instead of having separate, but
ultimately similar, logic in RateLimitMiddleware.
We don't touch tests here because "rate limited" error responses are
already verified in test_external.py.
This commit is contained in:
Mateusz Mandera
2020-11-27 16:33:01 +01:00
committed by Alex Vandiver
parent 92ce2d0e31
commit 43a0c60e96
6 changed files with 45 additions and 25 deletions

View File

@@ -66,10 +66,15 @@ def json_response_from_error(exception: JsonableError) -> HttpResponse:
middleware takes care of transforming it into a response by
calling this function.
'''
return json_response('error',
msg=exception.msg,
data=exception.data,
status=exception.http_status_code)
response = json_response('error',
msg=exception.msg,
data=exception.data,
status=exception.http_status_code)
for header, value in exception.extra_headers.items():
response[header] = value
return response
def json_error(msg: str, data: Mapping[str, Any]={}, status: int=400) -> HttpResponse:
return json_response(res_type="error", msg=msg, data=data, status=status)