Skip to content

Commit 7c3967f

Browse files
Eeshan Gargtimabbott
Eeshan Garg
authored andcommitted
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.
1 parent 05d591a commit 7c3967f

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

zulip/zulip/__init__.py

+21-18
Original file line numberDiff line numberDiff line change
@@ -681,10 +681,7 @@ def end_error_retry(succeeded: bool) -> None:
681681
continue
682682
else:
683683
end_error_retry(False)
684-
return {
685-
"msg": f"Connection error:\n{traceback.format_exc()}",
686-
"result": "connection-error",
687-
}
684+
raise
688685
except requests.exceptions.ConnectionError:
689686
if not self.has_connected:
690687
# If we have never successfully connected to the server, don't
@@ -696,16 +693,10 @@ def end_error_retry(succeeded: bool) -> None:
696693
if error_retry(""):
697694
continue
698695
end_error_retry(False)
699-
return {
700-
"msg": f"Connection error:\n{traceback.format_exc()}",
701-
"result": "connection-error",
702-
}
696+
raise
703697
except Exception:
704698
# We'll split this out into more cases as we encounter new bugs.
705-
return {
706-
"msg": f"Unexpected error:\n{traceback.format_exc()}",
707-
"result": "unexpected-error",
708-
}
699+
raise
709700

710701
try:
711702
if requests_json_is_function:
@@ -782,16 +773,28 @@ def do_register() -> Tuple[str, int]:
782773
if queue_id is None:
783774
(queue_id, last_event_id) = do_register()
784775

785-
res = self.get_events(queue_id=queue_id, last_event_id=last_event_id)
776+
try:
777+
res = self.get_events(queue_id=queue_id, last_event_id=last_event_id)
778+
except (
779+
requests.exceptions.Timeout,
780+
requests.exceptions.SSLError,
781+
requests.exceptions.ConnectionError,
782+
):
783+
if self.verbose:
784+
print(f"Connection error fetching events:\n{traceback.format_exc()}")
785+
# TODO: Make this use our backoff library
786+
time.sleep(1)
787+
continue
788+
except Exception:
789+
print(f"Unexpected error:\n{traceback.format_exc()}")
790+
# TODO: Make this use our backoff library
791+
time.sleep(1)
792+
continue
793+
786794
if "error" in res["result"]:
787795
if res["result"] == "http-error":
788796
if self.verbose:
789797
print("HTTP error fetching events -- probably a server restart")
790-
elif res["result"] == "connection-error":
791-
if self.verbose:
792-
print(
793-
"Connection error fetching events -- probably server is temporarily down?"
794-
)
795798
else:
796799
if self.verbose:
797800
print("Server returned error:\n{}".format(res["msg"]))

0 commit comments

Comments
 (0)