From 04d3621e1120907b9a4576b78d2439edd9cb0bdd Mon Sep 17 00:00:00 2001 From: Eeshan Garg Date: Mon, 29 Nov 2021 11:42:43 -0500 Subject: [PATCH 1/2] zulip: Reraise exceptions in do_api_query. There are cases where the call to an endpoint may result in an exception the traceback for which is converted into JSON and returned to the caller. In the case of such an unsuccessful response, we should just reraise the exception instead of parsing the response as though it was successful. --- zulip/zulip/__init__.py | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/zulip/zulip/__init__.py b/zulip/zulip/__init__.py index 91cc33998..6ad580dbf 100644 --- a/zulip/zulip/__init__.py +++ b/zulip/zulip/__init__.py @@ -681,10 +681,7 @@ def end_error_retry(succeeded: bool) -> None: continue else: end_error_retry(False) - return { - "msg": f"Connection error:\n{traceback.format_exc()}", - "result": "connection-error", - } + raise except requests.exceptions.ConnectionError: if not self.has_connected: # If we have never successfully connected to the server, don't @@ -696,16 +693,10 @@ def end_error_retry(succeeded: bool) -> None: if error_retry(""): continue end_error_retry(False) - return { - "msg": f"Connection error:\n{traceback.format_exc()}", - "result": "connection-error", - } + raise except Exception: # We'll split this out into more cases as we encounter new bugs. - return { - "msg": f"Unexpected error:\n{traceback.format_exc()}", - "result": "unexpected-error", - } + raise try: if requests_json_is_function: @@ -782,16 +773,28 @@ def do_register() -> Tuple[str, int]: if queue_id is None: (queue_id, last_event_id) = do_register() - res = self.get_events(queue_id=queue_id, last_event_id=last_event_id) + try: + res = self.get_events(queue_id=queue_id, last_event_id=last_event_id) + except ( + requests.exceptions.Timeout, + requests.exceptions.SSLError, + requests.exceptions.ConnectionError, + ): + if self.verbose: + print(f"Connection error fetching events:\n{traceback.format_exc()}") + # TODO: Make this use our backoff library + time.sleep(1) + continue + except Exception: + print(f"Unexpected error:\n{traceback.format_exc()}") + # TODO: Make this use our backoff library + time.sleep(1) + continue + if "error" in res["result"]: if res["result"] == "http-error": if self.verbose: print("HTTP error fetching events -- probably a server restart") - elif res["result"] == "connection-error": - if self.verbose: - print( - "Connection error fetching events -- probably server is temporarily down?" - ) else: if self.verbose: print("Server returned error:\n{}".format(res["msg"])) From f7afb0cfd0719879d7824f95ace8594ea939ee5e Mon Sep 17 00:00:00 2001 From: Eeshan Garg Date: Tue, 28 Dec 2021 19:32:04 -0500 Subject: [PATCH 2/2] zulip: Fix deprecation warnings for SafeConfigParser and readfp. SafeConfigParser has been renamed to ConfigParser and the method SafeConfigParser.readfp() is now named ConfigParser.read_file(). --- zulip/zulip/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/zulip/zulip/__init__.py b/zulip/zulip/__init__.py index 6ad580dbf..4b4829367 100644 --- a/zulip/zulip/__init__.py +++ b/zulip/zulip/__init__.py @@ -10,7 +10,7 @@ import traceback import types import urllib.parse -from configparser import SafeConfigParser +from configparser import ConfigParser from typing import ( IO, Any, @@ -425,9 +425,9 @@ def __init__( config_file = get_default_config_filename() if config_file is not None and os.path.exists(config_file): - config = SafeConfigParser() + config = ConfigParser() with open(config_file) as f: - config.readfp(f, config_file) + config.read_file(f, config_file) if api_key is None: api_key = config.get("api", "key") if email is None: