2017-07-21 02:17:28 +02:00
|
|
|
from enum import Enum
|
2020-06-11 00:54:34 +02:00
|
|
|
from typing import Any, Dict, List, NoReturn, Optional, Type, TypeVar
|
2013-10-10 21:37:26 +02:00
|
|
|
|
2018-03-22 21:43:28 +01:00
|
|
|
from django.utils.translation import ugettext as _
|
2013-05-29 23:58:07 +02:00
|
|
|
|
2019-10-09 02:02:06 +02:00
|
|
|
T = TypeVar("T", bound="AbstractEnum")
|
|
|
|
|
2017-07-21 02:17:28 +02:00
|
|
|
class AbstractEnum(Enum):
|
|
|
|
'''An enumeration whose members are used strictly for their names.'''
|
|
|
|
|
2019-10-09 02:02:06 +02:00
|
|
|
def __new__(cls: Type[T]) -> T:
|
2017-07-21 02:17:28 +02:00
|
|
|
obj = object.__new__(cls)
|
2017-08-25 20:01:20 +02:00
|
|
|
obj._value_ = len(cls.__members__) + 1
|
2017-07-21 02:17:28 +02:00
|
|
|
return obj
|
|
|
|
|
|
|
|
# Override all the `Enum` methods that use `_value_`.
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def __repr__(self) -> str:
|
2018-05-15 22:57:40 +02:00
|
|
|
return str(self) # nocoverage
|
2017-07-21 02:17:28 +02:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def value(self) -> None:
|
2018-05-15 22:57:40 +02:00
|
|
|
raise AssertionError("Not implemented")
|
2017-07-21 02:17:28 +02:00
|
|
|
|
2018-12-08 18:20:39 +01:00
|
|
|
def __reduce_ex__(self, proto: object) -> NoReturn:
|
2018-05-15 22:57:40 +02:00
|
|
|
raise AssertionError("Not implemented")
|
2017-07-21 02:17:28 +02:00
|
|
|
|
|
|
|
class ErrorCode(AbstractEnum):
|
|
|
|
BAD_REQUEST = () # Generic name, from the name of HTTP 400.
|
|
|
|
REQUEST_VARIABLE_MISSING = ()
|
|
|
|
REQUEST_VARIABLE_INVALID = ()
|
2018-11-15 05:31:34 +01:00
|
|
|
INVALID_JSON = ()
|
2017-07-21 02:18:33 +02:00
|
|
|
BAD_IMAGE = ()
|
2018-01-26 16:13:33 +01:00
|
|
|
REALM_UPLOAD_QUOTA = ()
|
2017-07-21 02:17:28 +02:00
|
|
|
BAD_NARROW = ()
|
2018-08-21 08:14:46 +02:00
|
|
|
CANNOT_DEACTIVATE_LAST_USER = ()
|
2018-04-24 20:22:38 +02:00
|
|
|
MISSING_HTTP_EVENT_HEADER = ()
|
2018-03-22 21:43:28 +01:00
|
|
|
STREAM_DOES_NOT_EXIST = ()
|
2017-07-21 02:17:28 +02:00
|
|
|
UNAUTHORIZED_PRINCIPAL = ()
|
2018-05-22 16:45:21 +02:00
|
|
|
UNEXPECTED_WEBHOOK_EVENT_TYPE = ()
|
2017-07-21 02:20:31 +02:00
|
|
|
BAD_EVENT_QUEUE_ID = ()
|
2017-07-25 03:30:13 +02:00
|
|
|
CSRF_FAILED = ()
|
2017-07-25 02:02:30 +02:00
|
|
|
INVITATION_FAILED = ()
|
2017-10-12 03:02:35 +02:00
|
|
|
INVALID_ZULIP_SERVER = ()
|
2018-12-23 00:13:57 +01:00
|
|
|
INVALID_MARKDOWN_INCLUDE_STATEMENT = ()
|
2018-11-09 19:45:25 +01:00
|
|
|
REQUEST_CONFUSING_VAR = ()
|
2019-01-05 20:18:18 +01:00
|
|
|
INVALID_API_KEY = ()
|
2019-11-16 09:26:28 +01:00
|
|
|
INVALID_ZOOM_TOKEN = ()
|
2017-07-21 02:17:28 +02:00
|
|
|
|
JsonableError: Move into a normally-typed file.
The file `zerver/lib/request.py` doesn't have type annotations
of its own; if they did, they would duplicate the annotations that
exist in its stub file `zerver/lib/request.pyi`. The latter exists
so that we can provide types for the highly dynamic `REQ` and
`has_request_variables`, which are beyond the type-checker's ken
to type-check, but we should minimize the scope of code that gets
that kind of treatment and `JsonableError` is not at all the sort of
code that needs it.
So move the definition of `JsonableError` into a file that does
get type-checked.
In doing so, the type-checker points out one issue already:
`__str__` should return a `str`, but we had it returning a `Text`,
which on Python 2 is not the same thing. Indeed, because the
message we pass to the `JsonableError` constructor is generally
translated, it may well be a Unicode string stuffed full of
non-ASCII characters. This is potentially a bit of a landmine.
But (a) it can only possibly matter in Python 2 which we intend to
be off before long, and (b) AFAIK it hasn't been biting us in
practice, so we've probably reasonably well worked around it where
it could matter. Leave it as is.
2017-07-20 00:51:54 +02:00
|
|
|
class JsonableError(Exception):
|
2017-07-21 02:17:28 +02:00
|
|
|
'''A standardized error format we can turn into a nice JSON HTTP response.
|
|
|
|
|
2018-08-22 17:14:27 +02:00
|
|
|
This class can be invoked in a couple ways.
|
2017-07-21 02:17:28 +02:00
|
|
|
|
|
|
|
* Easiest, but completely machine-unreadable:
|
|
|
|
|
|
|
|
raise JsonableError(_("No such widget: {}").format(widget_name))
|
|
|
|
|
|
|
|
The message may be passed through to clients and shown to a user,
|
|
|
|
so translation is required. Because the text will vary depending
|
|
|
|
on the user's language, it's not possible for code to distinguish
|
|
|
|
this error from others in a non-buggy way.
|
|
|
|
|
|
|
|
* Fully machine-readable, with an error code and structured data:
|
|
|
|
|
|
|
|
class NoSuchWidgetError(JsonableError):
|
|
|
|
code = ErrorCode.NO_SUCH_WIDGET
|
|
|
|
data_fields = ['widget_name']
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def __init__(self, widget_name: str) -> None:
|
2020-05-09 00:10:17 +02:00
|
|
|
self.widget_name: str = widget_name
|
2017-07-21 02:17:28 +02:00
|
|
|
|
|
|
|
@staticmethod
|
2017-11-05 11:15:10 +01:00
|
|
|
def msg_format() -> str:
|
2017-07-21 02:17:28 +02:00
|
|
|
return _("No such widget: {widget_name}")
|
|
|
|
|
|
|
|
raise NoSuchWidgetError(widget_name)
|
|
|
|
|
2018-08-22 17:14:27 +02:00
|
|
|
Now both server and client code see a `widget_name` attribute
|
|
|
|
and an error code.
|
2017-07-21 02:17:28 +02:00
|
|
|
|
|
|
|
Subclasses may also override `http_status_code`.
|
|
|
|
'''
|
|
|
|
|
2018-08-22 17:14:27 +02:00
|
|
|
# Override this in subclasses, as needed.
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
code: ErrorCode = ErrorCode.BAD_REQUEST
|
2017-07-21 02:17:28 +02:00
|
|
|
|
|
|
|
# Override this in subclasses if providing structured data.
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
data_fields: List[str] = []
|
2017-07-21 02:17:28 +02:00
|
|
|
|
|
|
|
# Optionally override this in subclasses to return a different HTTP status,
|
|
|
|
# like 403 or 404.
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
http_status_code: int = 400
|
JsonableError: Move into a normally-typed file.
The file `zerver/lib/request.py` doesn't have type annotations
of its own; if they did, they would duplicate the annotations that
exist in its stub file `zerver/lib/request.pyi`. The latter exists
so that we can provide types for the highly dynamic `REQ` and
`has_request_variables`, which are beyond the type-checker's ken
to type-check, but we should minimize the scope of code that gets
that kind of treatment and `JsonableError` is not at all the sort of
code that needs it.
So move the definition of `JsonableError` into a file that does
get type-checked.
In doing so, the type-checker points out one issue already:
`__str__` should return a `str`, but we had it returning a `Text`,
which on Python 2 is not the same thing. Indeed, because the
message we pass to the `JsonableError` constructor is generally
translated, it may well be a Unicode string stuffed full of
non-ASCII characters. This is potentially a bit of a landmine.
But (a) it can only possibly matter in Python 2 which we intend to
be off before long, and (b) AFAIK it hasn't been biting us in
practice, so we've probably reasonably well worked around it where
it could matter. Leave it as is.
2017-07-20 00:51:54 +02:00
|
|
|
|
2018-08-22 17:14:27 +02:00
|
|
|
def __init__(self, msg: str) -> None:
|
2017-07-21 02:17:28 +02:00
|
|
|
# `_msg` is an implementation detail of `JsonableError` itself.
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
self._msg: str = msg
|
JsonableError: Move into a normally-typed file.
The file `zerver/lib/request.py` doesn't have type annotations
of its own; if they did, they would duplicate the annotations that
exist in its stub file `zerver/lib/request.pyi`. The latter exists
so that we can provide types for the highly dynamic `REQ` and
`has_request_variables`, which are beyond the type-checker's ken
to type-check, but we should minimize the scope of code that gets
that kind of treatment and `JsonableError` is not at all the sort of
code that needs it.
So move the definition of `JsonableError` into a file that does
get type-checked.
In doing so, the type-checker points out one issue already:
`__str__` should return a `str`, but we had it returning a `Text`,
which on Python 2 is not the same thing. Indeed, because the
message we pass to the `JsonableError` constructor is generally
translated, it may well be a Unicode string stuffed full of
non-ASCII characters. This is potentially a bit of a landmine.
But (a) it can only possibly matter in Python 2 which we intend to
be off before long, and (b) AFAIK it hasn't been biting us in
practice, so we've probably reasonably well worked around it where
it could matter. Leave it as is.
2017-07-20 00:51:54 +02:00
|
|
|
|
2017-07-21 02:17:28 +02:00
|
|
|
@staticmethod
|
2018-05-11 01:40:23 +02:00
|
|
|
def msg_format() -> str:
|
2017-07-21 02:17:28 +02:00
|
|
|
'''Override in subclasses. Gets the items in `data_fields` as format args.
|
|
|
|
|
|
|
|
This should return (a translation of) a string literal.
|
|
|
|
The reason it's not simply a class attribute is to allow
|
|
|
|
translation to work.
|
|
|
|
'''
|
|
|
|
# Secretly this gets one more format arg not in `data_fields`: `_msg`.
|
|
|
|
# That's for the sake of the `JsonableError` base logic itself, for
|
|
|
|
# the simplest form of use where we just get a plain message string
|
|
|
|
# at construction time.
|
|
|
|
return '{_msg}'
|
|
|
|
|
|
|
|
#
|
|
|
|
# Infrastructure -- not intended to be overridden in subclasses.
|
|
|
|
#
|
|
|
|
|
|
|
|
@property
|
2018-05-11 01:40:23 +02:00
|
|
|
def msg(self) -> str:
|
2017-07-21 02:17:28 +02:00
|
|
|
format_data = dict(((f, getattr(self, f)) for f in self.data_fields),
|
|
|
|
_msg=getattr(self, '_msg', None))
|
|
|
|
return self.msg_format().format(**format_data)
|
|
|
|
|
|
|
|
@property
|
2017-11-05 11:15:10 +01:00
|
|
|
def data(self) -> Dict[str, Any]:
|
2017-07-21 02:17:28 +02:00
|
|
|
return dict(((f, getattr(self, f)) for f in self.data_fields),
|
|
|
|
code=self.code.name)
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def to_json(self) -> Dict[str, Any]:
|
2017-07-21 02:17:28 +02:00
|
|
|
d = {'result': 'error', 'msg': self.msg}
|
|
|
|
d.update(self.data)
|
|
|
|
return d
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def __str__(self) -> str:
|
2017-08-25 20:01:20 +02:00
|
|
|
return self.msg
|
JsonableError: Move into a normally-typed file.
The file `zerver/lib/request.py` doesn't have type annotations
of its own; if they did, they would duplicate the annotations that
exist in its stub file `zerver/lib/request.pyi`. The latter exists
so that we can provide types for the highly dynamic `REQ` and
`has_request_variables`, which are beyond the type-checker's ken
to type-check, but we should minimize the scope of code that gets
that kind of treatment and `JsonableError` is not at all the sort of
code that needs it.
So move the definition of `JsonableError` into a file that does
get type-checked.
In doing so, the type-checker points out one issue already:
`__str__` should return a `str`, but we had it returning a `Text`,
which on Python 2 is not the same thing. Indeed, because the
message we pass to the `JsonableError` constructor is generally
translated, it may well be a Unicode string stuffed full of
non-ASCII characters. This is potentially a bit of a landmine.
But (a) it can only possibly matter in Python 2 which we intend to
be off before long, and (b) AFAIK it hasn't been biting us in
practice, so we've probably reasonably well worked around it where
it could matter. Leave it as is.
2017-07-20 00:51:54 +02:00
|
|
|
|
2018-03-22 21:43:28 +01:00
|
|
|
class StreamDoesNotExistError(JsonableError):
|
|
|
|
code = ErrorCode.STREAM_DOES_NOT_EXIST
|
|
|
|
data_fields = ['stream']
|
|
|
|
|
|
|
|
def __init__(self, stream: str) -> None:
|
|
|
|
self.stream = stream
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def msg_format() -> str:
|
|
|
|
return _("Stream '{stream}' does not exist")
|
|
|
|
|
2019-01-28 05:28:29 +01:00
|
|
|
class StreamWithIDDoesNotExistError(JsonableError):
|
|
|
|
code = ErrorCode.STREAM_DOES_NOT_EXIST
|
|
|
|
data_fields = ['stream_id']
|
|
|
|
|
|
|
|
def __init__(self, stream_id: int) -> None:
|
|
|
|
self.stream_id = stream_id
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def msg_format() -> str:
|
|
|
|
return _("Stream with ID '{stream_id}' does not exist")
|
|
|
|
|
2018-08-21 08:14:46 +02:00
|
|
|
class CannotDeactivateLastUserError(JsonableError):
|
|
|
|
code = ErrorCode.CANNOT_DEACTIVATE_LAST_USER
|
2020-05-16 21:06:43 +02:00
|
|
|
data_fields = ['is_last_owner', 'entity']
|
2018-08-21 08:14:46 +02:00
|
|
|
|
2020-05-16 21:06:43 +02:00
|
|
|
def __init__(self, is_last_owner: bool) -> None:
|
|
|
|
self.is_last_owner = is_last_owner
|
|
|
|
self.entity = _("organization owner") if is_last_owner else _("user")
|
2018-08-21 08:14:46 +02:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def msg_format() -> str:
|
|
|
|
return _("Cannot deactivate the only {entity}.")
|
|
|
|
|
2018-12-23 00:13:57 +01:00
|
|
|
class InvalidMarkdownIncludeStatement(JsonableError):
|
|
|
|
code = ErrorCode.INVALID_MARKDOWN_INCLUDE_STATEMENT
|
|
|
|
data_fields = ['include_statement']
|
|
|
|
|
|
|
|
def __init__(self, include_statement: str) -> None:
|
|
|
|
self.include_statement = include_statement
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def msg_format() -> str:
|
|
|
|
return _("Invalid markdown include statement: {include_statement}")
|
|
|
|
|
2019-08-01 18:48:41 +02:00
|
|
|
class RateLimited(Exception):
|
2017-11-05 11:15:10 +01:00
|
|
|
def __init__(self, msg: str="") -> None:
|
2017-10-27 08:28:23 +02:00
|
|
|
super().__init__(msg)
|
2018-06-21 15:09:14 +02:00
|
|
|
|
2018-11-15 05:31:34 +01:00
|
|
|
class InvalidJSONError(JsonableError):
|
|
|
|
code = ErrorCode.INVALID_JSON
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def msg_format() -> str:
|
|
|
|
return _("Malformed JSON")
|
|
|
|
|
2020-07-15 22:18:32 +02:00
|
|
|
class OrganizationMemberRequired(JsonableError):
|
|
|
|
code: ErrorCode = ErrorCode.UNAUTHORIZED_PRINCIPAL
|
|
|
|
|
|
|
|
MEMBER_REQUIRED_MESSAGE = _("Must be an organization member")
|
|
|
|
|
|
|
|
def __init__(self) -> None:
|
|
|
|
super().__init__(self.MEMBER_REQUIRED_MESSAGE)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def msg_format() -> str:
|
|
|
|
return OrganizationMemberRequired.MEMBER_REQUIRED_MESSAGE
|
|
|
|
|
2019-11-16 15:53:56 +01:00
|
|
|
class OrganizationAdministratorRequired(JsonableError):
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
code: ErrorCode = ErrorCode.UNAUTHORIZED_PRINCIPAL
|
2019-11-16 15:53:56 +01:00
|
|
|
|
|
|
|
ADMIN_REQUIRED_MESSAGE = _("Must be an organization administrator")
|
|
|
|
|
|
|
|
def __init__(self) -> None:
|
|
|
|
super().__init__(self.ADMIN_REQUIRED_MESSAGE)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def msg_format() -> str:
|
|
|
|
return OrganizationAdministratorRequired.ADMIN_REQUIRED_MESSAGE
|
|
|
|
|
2020-06-11 00:26:49 +02:00
|
|
|
class OrganizationOwnerRequired(JsonableError):
|
|
|
|
code: ErrorCode = ErrorCode.UNAUTHORIZED_PRINCIPAL
|
|
|
|
|
|
|
|
OWNER_REQUIRED_MESSAGE = _("Must be an organization owner")
|
|
|
|
|
|
|
|
def __init__(self) -> None:
|
|
|
|
super().__init__(self.OWNER_REQUIRED_MESSAGE)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def msg_format() -> str:
|
|
|
|
return OrganizationOwnerRequired.OWNER_REQUIRED_MESSAGE
|
|
|
|
|
2020-06-25 16:58:20 +02:00
|
|
|
class MarkdownRenderingException(Exception):
|
2018-06-21 15:09:14 +02:00
|
|
|
pass
|
2019-01-05 20:18:18 +01:00
|
|
|
|
|
|
|
class InvalidAPIKeyError(JsonableError):
|
|
|
|
code = ErrorCode.INVALID_API_KEY
|
|
|
|
http_status_code = 401
|
|
|
|
|
|
|
|
def __init__(self) -> None:
|
|
|
|
pass
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def msg_format() -> str:
|
|
|
|
return _("Invalid API key")
|
2019-06-06 05:55:09 +02:00
|
|
|
|
2019-12-16 08:12:39 +01:00
|
|
|
class InvalidAPIKeyFormatError(InvalidAPIKeyError):
|
|
|
|
@staticmethod
|
|
|
|
def msg_format() -> str:
|
|
|
|
return _("Malformed API key")
|
|
|
|
|
2019-06-06 05:55:09 +02:00
|
|
|
class UnexpectedWebhookEventType(JsonableError):
|
|
|
|
code = ErrorCode.UNEXPECTED_WEBHOOK_EVENT_TYPE
|
|
|
|
data_fields = ['webhook_name', 'event_type']
|
|
|
|
|
|
|
|
def __init__(self, webhook_name: str, event_type: Optional[str]) -> None:
|
|
|
|
self.webhook_name = webhook_name
|
|
|
|
self.event_type = event_type
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def msg_format() -> str:
|
|
|
|
return _("The '{event_type}' event isn't currently supported by the {webhook_name} webhook")
|