Correct and simplify URLError exception handling
authorJeremy Stanley <fungi@yuggoth.org>
Tue, 1 Jun 2021 15:31:39 +0000 (15:31 +0000)
committerJeremy Stanley <fungi@yuggoth.org>
Tue, 1 Jun 2021 15:31:39 +0000 (15:31 +0000)
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.

weather.py

index 8eed25e..2258c80 100644 (file)
@@ -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>")]