In some cases socket.error constructor contains two arguments - errno and message.
Make sure we also handle that case. Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/47914e4a Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/47914e4a Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/47914e4a Branch: refs/heads/trunk Commit: 47914e4a6fc2208abfc4a432edfc29ef76b88ba9 Parents: b9e7a62 Author: Tomaz Muraus <[email protected]> Authored: Thu Jan 14 19:14:45 2016 +0100 Committer: Tomaz Muraus <[email protected]> Committed: Thu Jan 14 19:14:45 2016 +0100 ---------------------------------------------------------------------- libcloud/httplib_ssl.py | 22 ++++++++++++++++------ libcloud/test/test_httplib_ssl.py | 11 ++++++++++- 2 files changed, 26 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/libcloud/blob/47914e4a/libcloud/httplib_ssl.py ---------------------------------------------------------------------- diff --git a/libcloud/httplib_ssl.py b/libcloud/httplib_ssl.py index 73d2dee..5f11d05 100644 --- a/libcloud/httplib_ssl.py +++ b/libcloud/httplib_ssl.py @@ -299,8 +299,8 @@ class LibcloudHTTPSConnection(httplib.HTTPSConnection, LibcloudBaseConnection): ca_certs=self.ca_cert, ssl_version=libcloud.security.SSL_VERSION) except socket.error: - e = sys.exc_info()[1] - exc_msg = str(e) + exc = sys.exc_info()[1] + exc_msg = str(exc) # Re-throw an exception with a more friendly error message if 'connection reset by peer' in exc_msg.lower(): @@ -309,11 +309,21 @@ class LibcloudHTTPSConnection(httplib.HTTPSConnection, LibcloudBaseConnection): msg = (UNSUPPORTED_TLS_VERSION_ERROR_MSG % (exc_msg, ssl_version)) - new_e = socket.error(msg) - new_e.original_exc = e - raise new_e + # Note: In some cases arguments are (errno, message) and in + # other it's just (message,) + exc_args = getattr(exc, 'args', []) + + if len(exc_args) == 2: + new_exc_args = [exc.args[0], msg] + else: + new_exc_args = [msg] + + new_exc = socket.error(*new_exc_args) + new_exc.original_exc = exc + raise new_exc + + raise exc - raise e cert = self.sock.getpeercert() try: match_hostname(cert, self.host) http://git-wip-us.apache.org/repos/asf/libcloud/blob/47914e4a/libcloud/test/test_httplib_ssl.py ---------------------------------------------------------------------- diff --git a/libcloud/test/test_httplib_ssl.py b/libcloud/test/test_httplib_ssl.py index a2f8bf5..25f6450 100644 --- a/libcloud/test/test_httplib_ssl.py +++ b/libcloud/test/test_httplib_ssl.py @@ -128,9 +128,18 @@ class TestHttpLibSSLTests(unittest.TestCase): mock_wrap_socket.side_effect = socket.error('Connection reset by peer') expected_msg = 'Failed to establish SSL / TLS connection' - self.assertRaisesRegexp(Exception, expected_msg, + self.assertRaisesRegexp(socket.error, expected_msg, self.httplib_object.connect) + # Same error but including errno + with self.assertRaises(socket.error) as cm: + mock_wrap_socket.side_effect = socket.error(104, 'Connection reset by peer') + self.httplib_object.connect() + + e = cm.exception + self.assertEqual(e.errno, 104) + self.assertTrue(expected_msg in str(e)) + if __name__ == '__main__': sys.exit(unittest.main())
