Correct and simplify URLError exception handling
Julien Palard pointed out that the way URLError exceptions were being manually cobbled into the stderr stream wasn't quite working (thanks!), but it was also unnecessarily complicated for reasons I don't recall now. Rip most of it out and just go with a basic catch/error/re-raise there instead.
This commit is contained in:
16
weather.py
16
weather.py
@@ -221,18 +221,10 @@ def get_uri(
|
||||
data = urlopen(uri).read().decode("utf-8")
|
||||
except URLError:
|
||||
if ignore_fail: return ""
|
||||
else:
|
||||
import os, sys, traceback
|
||||
message = "%s error: failed to retrieve\n %s\n %s" % (
|
||||
os.path.basename( sys.argv[0] ),
|
||||
uri,
|
||||
traceback.format_exception_only(
|
||||
sys.exc_type,
|
||||
sys.exc_value
|
||||
)[0]
|
||||
)
|
||||
sys.stderr.write(message)
|
||||
sys.exit(1)
|
||||
import os, sys
|
||||
sys.stderr.write("%s error: failed to retrieve\n %s\n\n" % (
|
||||
os.path.basename( sys.argv[0] ), uri))
|
||||
raise
|
||||
# Some data sources are HTML with the plain text wrapped in pre tags
|
||||
if "<pre>" in data:
|
||||
data = data[data.find("<pre>")+5:data.find("</pre>")]
|
||||
|
||||
Reference in New Issue
Block a user