Fail fast for bad connections with the API and bots.
The API has aggressive retry logic for connecting to a server, which may make sense for situation where you have connection blips or server restarts. When you're first connecting to the API, however, connection failures are almost certainly a sign of misconfiguration, so now we fail fast. The bot lib takes advantage of this API change by catching the ZulipError exception and exiting gracefully.
This commit is contained in:
parent
a19278da65
commit
043d963a99
2 changed files with 26 additions and 2 deletions
|
@ -271,6 +271,9 @@ def get_default_config_filename():
|
|||
" mv ~/.humbugrc ~/.zuliprc\n")
|
||||
return config_file
|
||||
|
||||
class ZulipError(Exception):
|
||||
pass
|
||||
|
||||
class Client(object):
|
||||
def __init__(self, email=None, api_key=None, config_file=None,
|
||||
verbose=False, retry_on_errors=True,
|
||||
|
@ -382,6 +385,8 @@ class Client(object):
|
|||
|
||||
self.session = None # type: Union[None, requests.Session]
|
||||
|
||||
self.has_connected = False
|
||||
|
||||
def ensure_session(self):
|
||||
# type: () -> None
|
||||
|
||||
|
@ -507,6 +512,8 @@ class Client(object):
|
|||
timeout=request_timeout,
|
||||
**kwargs)
|
||||
|
||||
self.has_connected = True
|
||||
|
||||
# On 50x errors, try again after a short sleep
|
||||
if str(res.status_code).startswith('5'):
|
||||
if error_retry(" (server %s)" % (res.status_code,)):
|
||||
|
@ -528,6 +535,13 @@ class Client(object):
|
|||
return {'msg': "Connection error:\n%s" % traceback.format_exc(),
|
||||
"result": "connection-error"}
|
||||
except requests.exceptions.ConnectionError:
|
||||
if not self.has_connected:
|
||||
# If we have never successfully connected to the server, don't
|
||||
# go into retry logic, because the most likely scenario here is
|
||||
# that somebody just hasn't started their server, or they passed
|
||||
# in an invalid site.
|
||||
raise ZulipError('cannot connect to server ' + self.base_url)
|
||||
|
||||
if error_retry(""):
|
||||
continue
|
||||
end_error_retry(False)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue