api: Add UnrecoverableNetworkError Exception & use in do_api_query.

This replaces one case where the base ZulipError was raised, and another
where the original exception was re-raised.

This method, and so all API calls, should now only fail via raising this
Exception or by a non-'success' result in the return value dict; this
should avoid leaking any implementation detail and clarify expectations
in user code.
This commit is contained in:
neiljp (Neil Pilgrim) 2019-03-23 21:31:36 -07:00 committed by Tim Abbott
parent accfa8ddaf
commit c2e5b14034

View file

@ -294,6 +294,9 @@ class ConfigNotFoundError(ZulipError):
class MissingURLError(ZulipError): class MissingURLError(ZulipError):
pass pass
class UnrecoverableNetworkError(ZulipError):
pass
class Client(object): class Client(object):
def __init__(self, email=None, api_key=None, config_file=None, def __init__(self, email=None, api_key=None, config_file=None,
verbose=False, retry_on_errors=True, verbose=False, retry_on_errors=True,
@ -565,7 +568,7 @@ class Client(object):
# non-timeout other SSLErrors # non-timeout other SSLErrors
if (isinstance(e, requests.exceptions.SSLError) and if (isinstance(e, requests.exceptions.SSLError) and
str(e) != "The read operation timed out"): str(e) != "The read operation timed out"):
raise raise UnrecoverableNetworkError('SSL Error')
if longpolling: if longpolling:
# When longpolling, we expect the timeout to fire, # When longpolling, we expect the timeout to fire,
# and the correct response is to just retry # and the correct response is to just retry
@ -580,7 +583,7 @@ class Client(object):
# go into retry logic, because the most likely scenario here is # go into retry logic, because the most likely scenario here is
# that somebody just hasn't started their server, or they passed # that somebody just hasn't started their server, or they passed
# in an invalid site. # in an invalid site.
raise ZulipError('cannot connect to server ' + self.base_url) raise UnrecoverableNetworkError('cannot connect to server ' + self.base_url)
if error_retry(""): if error_retry(""):
continue continue