From 6dfb46dc0824b7fad7aaa00c7eccdae2bf6fb58d Mon Sep 17 00:00:00 2001 From: Greg Price Date: Wed, 19 Jul 2017 16:23:12 -0700 Subject: [PATCH] JsonableError: Rename `status_code` and rely more on its default. With #5598 there will soon be an application-level error code optionally associated with a `JsonableError`, so rename this field to make clear that it specifically refers to an HTTP status code. Also take this opportunity to eliminate most of the places that refer to it, which only do so to repeat the default value. --- zerver/lib/exceptions.py | 8 ++++---- zerver/lib/request.py | 6 ++---- zerver/middleware.py | 2 +- zerver/views/messages.py | 5 ++--- zerver/views/streams.py | 6 +++--- 5 files changed, 12 insertions(+), 15 deletions(-) diff --git a/zerver/lib/exceptions.py b/zerver/lib/exceptions.py index 6997c82154..0f042de5af 100644 --- a/zerver/lib/exceptions.py +++ b/zerver/lib/exceptions.py @@ -5,12 +5,12 @@ from django.core.exceptions import PermissionDenied class JsonableError(Exception): msg = None # type: Text - status_code = None # type: int + http_status_code = 400 # type: int - def __init__(self, msg, status_code=400): - # type: (Text) -> None + def __init__(self, msg, http_status_code=400): + # type: (Text, int) -> None self.msg = msg - self.status_code = status_code + self.http_status_code = http_status_code def __str__(self): # type: () -> str diff --git a/zerver/lib/request.py b/zerver/lib/request.py index d236f06d40..62964715f3 100644 --- a/zerver/lib/request.py +++ b/zerver/lib/request.py @@ -11,18 +11,16 @@ from django.utils.translation import ugettext as _ from zerver.lib.exceptions import JsonableError class RequestVariableMissingError(JsonableError): - def __init__(self, var_name, status_code=400): + def __init__(self, var_name): self.var_name = var_name - self.status_code = status_code def to_json_error_msg(self): return _("Missing '%s' argument") % (self.var_name,) class RequestVariableConversionError(JsonableError): - def __init__(self, var_name, bad_value, status_code=400): + def __init__(self, var_name, bad_value): self.var_name = var_name self.bad_value = bad_value - self.status_code = status_code def to_json_error_msg(self): return (_("Bad value for '%(var_name)s': %(value)s") % diff --git a/zerver/middleware.py b/zerver/middleware.py index 440c3fa8a8..510bce8ddc 100644 --- a/zerver/middleware.py +++ b/zerver/middleware.py @@ -286,7 +286,7 @@ class JsonErrorHandler(MiddlewareMixin): def process_exception(self, request, exception): # type: (HttpRequest, Exception) -> Optional[HttpResponse] if isinstance(exception, JsonableError): - return json_error(exception.to_json_error_msg(), status=exception.status_code) + return json_error(exception.to_json_error_msg(), status=exception.http_status_code) if request.error_format == "JSON": logging.error(traceback.format_exc()) return json_error(_("Internal server error"), status=500) diff --git a/zerver/views/messages.py b/zerver/views/messages.py index 618e10a3a1..58919fbb08 100644 --- a/zerver/views/messages.py +++ b/zerver/views/messages.py @@ -56,10 +56,9 @@ import six LARGER_THAN_MAX_MESSAGE_ID = 10000000000000000 class BadNarrowOperator(JsonableError): - def __init__(self, desc, status_code=400): - # type: (str, int) -> None + def __init__(self, desc): + # type: (str) -> None self.desc = desc - self.status_code = status_code def to_json_error_msg(self): # type: () -> str diff --git a/zerver/views/streams.py b/zerver/views/streams.py index 54a0c9837b..154518cf02 100644 --- a/zerver/views/streams.py +++ b/zerver/views/streams.py @@ -35,10 +35,10 @@ from six.moves import urllib import six class PrincipalError(JsonableError): - def __init__(self, principal, status_code=403): - # type: (Text, int) -> None + def __init__(self, principal): + # type: (Text) -> None self.principal = principal # type: Text - self.status_code = status_code # type: int + self.http_status_code = 403 # type: int def to_json_error_msg(self): # type: () -> Text