If we got bogus XML that can't be parsed, provide a better error message instead of a trace. Fixes #77

This commit is contained in:
Matt Martz 2014-06-25 11:06:42 -05:00
parent c0cd0d1666
commit 3558b22de1
1 changed files with 26 additions and 18 deletions

View File

@ -314,6 +314,7 @@ def getConfig():
if int(uh.code) != 200: if int(uh.code) != 200:
return None return None
uh.close() uh.close()
try:
try: try:
root = ET.fromstring(''.encode().join(configxml)) root = ET.fromstring(''.encode().join(configxml))
config = { config = {
@ -328,6 +329,9 @@ def getConfig():
'times': getAttributesByTagName(root, 'times'), 'times': getAttributesByTagName(root, 'times'),
'download': getAttributesByTagName(root, 'download'), 'download': getAttributesByTagName(root, 'download'),
'upload': getAttributesByTagName(root, 'upload')} 'upload': getAttributesByTagName(root, 'upload')}
except SyntaxError:
print_('Failed to parse speedtest.net configuration')
sys.exit(1)
del root del root
del configxml del configxml
return config return config
@ -347,12 +351,16 @@ def closestServers(client, all=False):
if int(uh.code) != 200: if int(uh.code) != 200:
return None return None
uh.close() uh.close()
try:
try: try:
root = ET.fromstring(''.encode().join(serversxml)) root = ET.fromstring(''.encode().join(serversxml))
elements = root.getiterator('server') elements = root.getiterator('server')
except AttributeError: except AttributeError:
root = DOM.parseString(''.join(serversxml)) root = DOM.parseString(''.join(serversxml))
elements = root.getElementsByTagName('server') elements = root.getElementsByTagName('server')
except SyntaxError:
print_('Failed to parse list of speedtest.net servers')
sys.exit(1)
servers = {} servers = {}
for server in elements: for server in elements:
try: try: