From: Jeremy Stanley Date: Tue, 1 Jun 2021 15:31:39 +0000 (+0000) Subject: Correct and simplify URLError exception handling X-Git-Tag: 2.4.2~3 X-Git-Url: https://yuggoth.org/gitweb?a=commitdiff_plain;h=847a98636e473189f36679c6b67e1878b3061a5e;p=weather.git 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. --- diff --git a/weather.py b/weather.py index 8eed25e..2258c80 100644 --- a/weather.py +++ b/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 "
" in data:
             data = data[data.find("
")+5:data.find("
")]