Don't sys.exit(1), instead either raise the exception, or a new SpeedtestCLIError. Fixes #352
This commit is contained in:
parent
7b38e264bc
commit
411f1609e8
32
speedtest.py
32
speedtest.py
|
@ -225,6 +225,10 @@ class SpeedtestException(Exception):
|
||||||
"""Base exception for this module"""
|
"""Base exception for this module"""
|
||||||
|
|
||||||
|
|
||||||
|
class SpeedtestCLIError(SpeedtestException):
|
||||||
|
"""Generic exception for raising errors during CLI operation"""
|
||||||
|
|
||||||
|
|
||||||
class SpeedtestHTTPError(SpeedtestException):
|
class SpeedtestHTTPError(SpeedtestException):
|
||||||
"""Base HTTP exception for this module"""
|
"""Base HTTP exception for this module"""
|
||||||
|
|
||||||
|
@ -303,6 +307,13 @@ class GzipDecodedResponse(GZIP_BASE):
|
||||||
self.io.close()
|
self.io.close()
|
||||||
|
|
||||||
|
|
||||||
|
def get_exception():
|
||||||
|
"""Helper function to work with py2.4-py3 for getting the current
|
||||||
|
exception in a try/except block
|
||||||
|
"""
|
||||||
|
return sys.exc_info()[1]
|
||||||
|
|
||||||
|
|
||||||
def bound_socket(*args, **kwargs):
|
def bound_socket(*args, **kwargs):
|
||||||
"""Bind socket to a specified source IP address"""
|
"""Bind socket to a specified source IP address"""
|
||||||
|
|
||||||
|
@ -398,7 +409,7 @@ def catch_request(request):
|
||||||
uh = urlopen(request)
|
uh = urlopen(request)
|
||||||
return uh, False
|
return uh, False
|
||||||
except HTTP_ERRORS:
|
except HTTP_ERRORS:
|
||||||
e = sys.exc_info()[1]
|
e = get_exception()
|
||||||
return None, e
|
return None, e
|
||||||
|
|
||||||
|
|
||||||
|
@ -982,7 +993,7 @@ class Speedtest(object):
|
||||||
r = h.getresponse()
|
r = h.getresponse()
|
||||||
total = (timeit.default_timer() - start)
|
total = (timeit.default_timer() - start)
|
||||||
except HTTP_ERRORS:
|
except HTTP_ERRORS:
|
||||||
e = sys.exc_info()[1]
|
e = get_exception()
|
||||||
printer('%r' % e, debug=True)
|
printer('%r' % e, debug=True)
|
||||||
cum.append(3600)
|
cum.append(3600)
|
||||||
continue
|
continue
|
||||||
|
@ -1312,14 +1323,14 @@ def shell():
|
||||||
speedtest = Speedtest()
|
speedtest = Speedtest()
|
||||||
except ConfigRetrievalError:
|
except ConfigRetrievalError:
|
||||||
printer('Cannot retrieve speedtest configuration')
|
printer('Cannot retrieve speedtest configuration')
|
||||||
sys.exit(1)
|
raise
|
||||||
|
|
||||||
if args.list:
|
if args.list:
|
||||||
try:
|
try:
|
||||||
speedtest.get_servers()
|
speedtest.get_servers()
|
||||||
except ServersRetrievalError:
|
except ServersRetrievalError:
|
||||||
print_('Cannot retrieve speedtest server list')
|
print_('Cannot retrieve speedtest server list')
|
||||||
sys.exit(1)
|
raise
|
||||||
|
|
||||||
for _, servers in sorted(speedtest.servers.items()):
|
for _, servers in sorted(speedtest.servers.items()):
|
||||||
for server in servers:
|
for server in servers:
|
||||||
|
@ -1328,7 +1339,7 @@ def shell():
|
||||||
try:
|
try:
|
||||||
print_(line)
|
print_(line)
|
||||||
except IOError:
|
except IOError:
|
||||||
e = sys.exc_info()[1]
|
e = get_exception()
|
||||||
if e.errno != errno.EPIPE:
|
if e.errno != errno.EPIPE:
|
||||||
raise
|
raise
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
@ -1346,14 +1357,13 @@ def shell():
|
||||||
try:
|
try:
|
||||||
speedtest.get_servers(servers)
|
speedtest.get_servers(servers)
|
||||||
except NoMatchedServers:
|
except NoMatchedServers:
|
||||||
print_('No matched servers: %s' % args.server)
|
raise SpeedtestCLIError('No matched servers: %s' % args.server)
|
||||||
sys.exit(1)
|
|
||||||
except ServersRetrievalError:
|
except ServersRetrievalError:
|
||||||
print_('Cannot retrieve speedtest server list')
|
print_('Cannot retrieve speedtest server list')
|
||||||
sys.exit(1)
|
raise
|
||||||
except InvalidServerIDType:
|
except InvalidServerIDType:
|
||||||
print_('%s is an invalid server type, must be int' % args.server)
|
raise SpeedtestCLIError('%s is an invalid server type, must '
|
||||||
sys.exit(1)
|
'be an int' % args.server)
|
||||||
|
|
||||||
printer('Selecting best server based on ping...', quiet)
|
printer('Selecting best server based on ping...', quiet)
|
||||||
speedtest.get_best_server()
|
speedtest.get_best_server()
|
||||||
|
@ -1403,7 +1413,7 @@ def main():
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print_('\nCancelling...')
|
print_('\nCancelling...')
|
||||||
except (SpeedtestException, SystemExit):
|
except (SpeedtestException, SystemExit):
|
||||||
e = sys.exc_info()[1]
|
e = get_exception()
|
||||||
if getattr(e, 'code', 1) != 0:
|
if getattr(e, 'code', 1) != 0:
|
||||||
raise SystemExit('ERROR: %s' % e)
|
raise SystemExit('ERROR: %s' % e)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue