Detect invalid URL.
authorCMDR furrycat <elite@furrycat.net>
Mon, 26 Sep 2016 15:51:12 +0000 (16:51 +0100)
committerCMDR furrycat <elite@furrycat.net>
Mon, 26 Sep 2016 15:53:45 +0000 (16:53 +0100)
bot.py

diff --git a/bot.py b/bot.py
index 51c2b55..150edb2 100755 (executable)
--- a/bot.py
+++ b/bot.py
@@ -55,24 +55,30 @@ greetings = [
 def open_url(url):
   if sys.version_info >= (3, 0):
     # Specify our own user agent as Cloudflare doesn't seem to like the urllib one
-    request = urllib.request.Request(url, headers={'User-Agent': 'catbot'})
     try:
+      request = urllib.request.Request(url, headers={'User-Agent': 'catbot'})
       return urllib.request.urlopen(request)
     except urllib.error.HTTPError as err:
       log.error("Error {0} opening {1}: {2}".format(err.code, url, err.reason))
       return None
+    except ValueError as err:
+      log.error('Invalid URL {}'.format(url))
+      return None
   else:
     sslctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
     # If we're on OSX with OpenSSL 0.9.x, manually specify preferred ciphers so CloudFlare can negotiate successfully
     if platform.system() == 'Darwin' and ssl.OPENSSL_VERSION_INFO[0] < 1:
       sslctx.set_ciphers("ECCdraft:HIGH:!aNULL")
     # Specify our own user agent as Cloudflare doesn't seem to like the urllib one
-    request = urllib2.Request(url, headers={'User-Agent': 'catbot'})
     try:
+      request = urllib2.Request(url, headers={'User-Agent': 'catbot'})
       return urllib2.urlopen(request, context=sslctx)
     except urllib2.HTTPError as err:
       log.error("Error {0} opening {1}: {2}".format(err.code, url, err.reason))
       return None
+    except ValueError as err:
+      log.error('Invalid URL {}'.format(url))
+      return None
 
 def wait_for_prompt(p):
   p.expect('EDI> ')