Author: tomaz
Date: Sun Feb 19 04:32:56 2012
New Revision: 1290945

URL: http://svn.apache.org/viewvc?rev=1290945&view=rev
Log:
Modify OpenStack driver to also handle JSON error responses without the
'message' attribute.

Modified:
    libcloud/trunk/libcloud/compute/drivers/openstack.py
    libcloud/trunk/test/compute/test_openstack.py

Modified: libcloud/trunk/libcloud/compute/drivers/openstack.py
URL: 
http://svn.apache.org/viewvc/libcloud/trunk/libcloud/compute/drivers/openstack.py?rev=1290945&r1=1290944&r2=1290945&view=diff
==============================================================================
--- libcloud/trunk/libcloud/compute/drivers/openstack.py (original)
+++ libcloud/trunk/libcloud/compute/drivers/openstack.py Sun Feb 19 04:32:56 
2012
@@ -107,8 +107,13 @@ class OpenStackResponse(Response):
             text = "; ".join([err.text or '' for err in body.getiterator()
                               if err.text])
         elif self.has_content_type('application/json'):
-            text = ';'.join([fault_data['message'] for fault_data
-                             in body.values()])
+            values = body.values()
+
+            if len(values) > 0 and 'message' in values[0]:
+                text = ';'.join([fault_data['message'] for fault_data
+                                 in values])
+            else:
+                text = body
         else:
             # while we hope a response is always one of xml or json, we have
             # seen html or text in the past, its not clear we can really do

Modified: libcloud/trunk/test/compute/test_openstack.py
URL: 
http://svn.apache.org/viewvc/libcloud/trunk/test/compute/test_openstack.py?rev=1290945&r1=1290944&r2=1290945&view=diff
==============================================================================
--- libcloud/trunk/test/compute/test_openstack.py (original)
+++ libcloud/trunk/test/compute/test_openstack.py Sun Feb 19 04:32:56 2012
@@ -129,6 +129,16 @@ class OpenStack_1_0_Tests(unittest.TestC
         else:
             self.fail('test should have thrown')
 
+    def test_error_parsing_when_body_is_missing_message(self):
+        OpenStackMockHttp.type = 'NO_MESSAGE_IN_ERROR_BODY'
+        try:
+            self.driver.list_images()
+        except Exception:
+            e = sys.exc_info()[1]
+            self.assertEqual(True, isinstance(e, Exception))
+        else:
+            self.fail('test should have thrown')
+
     def test_list_locations(self):
         locations = self.driver.list_locations()
         self.assertEqual(len(locations), 1)
@@ -372,6 +382,10 @@ class OpenStackMockHttp(MockHttpTestCase
     def _v1_0_INTERNAL_SERVER_ERROR(self, method, url, body, headers):
         return (httplib.INTERNAL_SERVER_ERROR, "<h1>500: Internal Server 
Error</h1>", {}, httplib.responses[httplib.INTERNAL_SERVER_ERROR])
 
+    def _v1_0_slug_images_detail_NO_MESSAGE_IN_ERROR_BODY(self, method, url, 
body, headers):
+        body = self.fixtures.load('300_multiple_choices.json')
+        return (httplib.MULTIPLE_CHOICES, body, self.json_content_headers, 
httplib.responses[httplib.OK])
+
     def _v1_0_UNAUTHORIZED_MISSING_KEY(self, method, url, body, headers):
         headers = {'x-server-management-url': 
'https://servers.api.rackspacecloud.com/v1.0/slug',
                    'x-auth-token': 'FE011C19-CF86-4F87-BE5D-9229145D7A06',


Reply via email to