mirror of https://github.com/zulip/zulip.git
JsonableError: Add types, and eliminate duck-typing.
In order to benefit from the modern conveniences of type-checking, add concrete, non-Any types to the interface for JsonableError. Relatedly, there's no need at this point to duck-type things at the places where we receive a JsonableError and try to use it. Simplify those by using straightforward standard typing.
This commit is contained in:
parent
fcb889c5c0
commit
ff5013c619
|
@ -1,12 +1,14 @@
|
|||
# This mypy stubs file ensures that mypy can correctly analyze REQ.
|
||||
from typing import Any, Callable, TypeVar
|
||||
from typing import Any, Callable, Text, TypeVar
|
||||
from django.http import HttpResponse
|
||||
|
||||
ViewFuncT = TypeVar('ViewFuncT', bound=Callable[..., HttpResponse])
|
||||
|
||||
class JsonableError(Exception):
|
||||
error = ... # type: Any
|
||||
def to_json_error_msg(self) -> Any: ...
|
||||
error = ... # type: Text
|
||||
status_code = ... # type: int
|
||||
def __init__(self, error: Text) -> None: ...
|
||||
def to_json_error_msg(self) -> Text: ...
|
||||
|
||||
class RequestVariableMissingError(JsonableError): ...
|
||||
class RequestVariableConversionError(JsonableError): ...
|
||||
|
|
|
@ -48,5 +48,5 @@ def json_success(data=None):
|
|||
return json_response(data=data)
|
||||
|
||||
def json_error(msg, data=None, status=400):
|
||||
# type: (str, Optional[Dict[str, Any]], int) -> HttpResponse
|
||||
# type: (Text, Optional[Dict[str, Any]], int) -> HttpResponse
|
||||
return json_response(res_type="error", msg=msg, data=data, status=status)
|
||||
|
|
|
@ -284,14 +284,9 @@ class LogRequests(MiddlewareMixin):
|
|||
|
||||
class JsonErrorHandler(MiddlewareMixin):
|
||||
def process_exception(self, request, exception):
|
||||
# type: (HttpRequest, Any) -> Optional[HttpResponse]
|
||||
if hasattr(exception, 'to_json_error_msg') and callable(exception.to_json_error_msg):
|
||||
try:
|
||||
status_code = exception.status_code
|
||||
except Exception:
|
||||
logging.warning("Jsonable exception %s missing status code!" % (exception,))
|
||||
status_code = 400
|
||||
return json_error(exception.to_json_error_msg(), status=status_code)
|
||||
# type: (HttpRequest, Exception) -> Optional[HttpResponse]
|
||||
if isinstance(exception, JsonableError):
|
||||
return json_error(exception.to_json_error_msg(), status=exception.status_code)
|
||||
if request.error_format == "JSON":
|
||||
logging.error(traceback.format_exc())
|
||||
return json_error(_("Internal server error"), status=500)
|
||||
|
|
|
@ -539,10 +539,7 @@ def fetch_events(query):
|
|||
logging.info("Disconnected handler for queue %s (%s/%s)" % (queue_id, user_profile_email,
|
||||
client_type_name))
|
||||
except JsonableError as e:
|
||||
if hasattr(e, 'to_json_error_msg') and callable(e.to_json_error_msg):
|
||||
return dict(type="error", handler_id=handler_id,
|
||||
message=e.to_json_error_msg())
|
||||
raise e
|
||||
return dict(type="error", handler_id=handler_id, message=e.to_json_error_msg())
|
||||
|
||||
client.connect_handler(handler_id, client_type_name)
|
||||
return dict(type="async")
|
||||
|
|
Loading…
Reference in New Issue