TransRead: handle more exceptions
authorArtem Bityutskiy <artem.bityutskiy@intel.com>
Fri, 8 Mar 2013 07:50:31 +0000 (09:50 +0200)
committerArtem Bityutskiy <artem.bityutskiy@intel.com>
Fri, 8 Mar 2013 07:50:31 +0000 (09:50 +0200)
Handle a couple of exceptions from httplib which may be caused by incorrect
URL. Just catch them, and re-raise with a more understandable error message.
The TransRead user will catch it and exit nicely, without a scary stackdupm
like this:

Traceback (most recent call last):
  File "./bmaptool", line 389, in <module>
    sys.exit(main())
  File "./bmaptool", line 373, in main
    args.func(args, log)
  File "./bmaptool", line 154, in copy_command
    copy_command_open_all(args, log)
  File "./bmaptool", line 109, in copy_command_open_all
    image_obj = TransRead.TransRead(args.image)
  File "/mnt/bigssd/dedekind/work/tizen/git/bmap-tools/bmaptools/TransRead.py", line 232, in __init__
    self._open_url(filepath)
  File "/mnt/bigssd/dedekind/work/tizen/git/bmap-tools/bmaptools/TransRead.py", line 210, in _open_url
    self._file_obj = opener.open(url)
  File "/usr/lib64/python2.7/urllib2.py", line 400, in open
    response = self._open(req, data)
  File "/usr/lib64/python2.7/urllib2.py", line 418, in _open
    '_open', req)
  File "/usr/lib64/python2.7/urllib2.py", line 378, in _call_chain
    result = func(*args)
  File "/usr/lib64/python2.7/urllib2.py", line 1215, in https_open
    return self.do_open(httplib.HTTPSConnection, req)
  File "/usr/lib64/python2.7/urllib2.py", line 1174, in do_open
    h.request(req.get_method(), req.get_selector(), req.data, headers)
  File "/usr/lib64/python2.7/httplib.py", line 958, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib64/python2.7/httplib.py", line 992, in _send_request
    self.endheaders(body)
  File "/usr/lib64/python2.7/httplib.py", line 954, in endheaders
    self._send_output(message_body)
  File "/usr/lib64/python2.7/httplib.py", line 814, in _send_output
    self.send(msg)
  File "/usr/lib64/python2.7/httplib.py", line 776, in send
    self.connect()
  File "/usr/lib64/python2.7/httplib.py", line 1160, in connect
    self._tunnel()
  File "/usr/lib64/python2.7/httplib.py", line 741, in _tunnel
    (version, code, message) = response._read_status()
  File "/usr/lib64/python2.7/httplib.py", line 371, in _read_status
    raise BadStatusLine(line)
httplib.BadStatusLine: ''

Change-Id: I48c0f63a6c33c816eb8cc833aeb400682c0f861f
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
bmaptools/TransRead.py

index 66c117b..eb16efe 100644 (file)
@@ -201,6 +201,7 @@ class TransRead:
         URL. """
 
         import urllib2
+        import httplib
 
         try:
             opener = urllib2.build_opener()
@@ -208,8 +209,11 @@ class TransRead:
             urllib2.install_opener(opener)
             self._file_obj = opener.open(url)
             self.is_url = True
-        except (IOError, ValueError) as err:
+        except (IOError, ValueError, httplib.InvalidURL) as err:
             raise Error("cannot open URL '%s': %s" % (url, err))
+        except httplib.BadStatusLine:
+            raise Error("cannot open URL '%s': server responds with an HTTP " \
+                        "status code that we don't understand" % url)
 
     def __init__(self, filepath):
         """ Class constructor. The 'filepath' argument is the full path to the