From 72f3adf6eead2d630ebe8018ca5eda2b12c897bf Mon Sep 17 00:00:00 2001 From: Dilip M Date: Thu, 19 Nov 2020 23:58:34 +0530 Subject: [PATCH] Following changes are done: The __init__.py file of Client was changed to thrown an error during instantiation if the credentials are incorrect. send.py was modified to handle the error thrown by the Client. Adding an entry in the log for the invalidcredentialException raised --- .gitignore | 3 +++ zulip/zulip/__init__.py | 7 +++++++ zulip/zulip/send.py | 7 +++++-- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 910ff9029..79e71246b 100644 --- a/.gitignore +++ b/.gitignore @@ -57,3 +57,6 @@ botserverrc # Pycharm \.DS_Store \.idea/ + +# VSCode +.vscode/ \ No newline at end of file diff --git a/zulip/zulip/__init__.py b/zulip/zulip/__init__.py index 196cfbe10..80b5a0e20 100644 --- a/zulip/zulip/__init__.py +++ b/zulip/zulip/__init__.py @@ -291,6 +291,10 @@ class MissingURLError(ZulipError): class UnrecoverableNetworkError(ZulipError): pass +class InvalidCredentialsError(ZulipError): + pass + + class Client: def __init__(self, email: Optional[str] = None, api_key: Optional[str] = None, config_file: Optional[str] = None, verbose: bool = False, retry_on_errors: bool = True, @@ -419,6 +423,9 @@ def __init__(self, email: Optional[str] = None, api_key: Optional[str] = None, c self.session = None # type: Optional[requests.Session] self.has_connected = False + + if self.get_profile()["result"] == "error": + raise InvalidCredentialsError("Invalid API Credentials") def ensure_session(self) -> None: diff --git a/zulip/zulip/send.py b/zulip/zulip/send.py index ea894607e..bb2fdf9ed 100755 --- a/zulip/zulip/send.py +++ b/zulip/zulip/send.py @@ -71,8 +71,11 @@ def main() -> int: if len(options.recipients) == 0 and not (options.stream and options.subject): parser.error('You must specify a stream/subject or at least one recipient.') - client = zulip.init_from_options(options) - + try: + client = zulip.init_from_options(options) + except zulip.InvalidCredentialsError: + log.exception("Invalid API credentials") + return 1 if not options.message: options.message = sys.stdin.read()