From 0325d22179d04009a56a0b031366b87c56f581f6 Mon Sep 17 00:00:00 2001
From: Gabriel Reid <gabriel.reid@gmail.com>
Date: Tue, 5 Jun 2012 10:59:45 +0200
Subject: [PATCH] Improve error information for Cloudstack async

Use the error text if available when throwing an exception for an
error that occurs during CloudStack async requests
---
 libcloud/common/cloudstack.py  |    3 ++-
 test/common/test_cloudstack.py |    9 +++++++--
 2 files changed, 9 insertions(+), 3 deletions(-)

diff --git libcloud/common/cloudstack.py libcloud/common/cloudstack.py
index 6b69a62..77c319a 100644
--- libcloud/common/cloudstack.py
+++ libcloud/common/cloudstack.py
@@ -83,7 +83,8 @@ class CloudStackConnection(ConnectionUserAndKey, PollingConnection):
         status = response.get('jobstatus', self.ASYNC_PENDING)
 
         if status == self.ASYNC_FAILURE:
-            raise Exception(status)
+            raise Exception(
+                    response.get('jobresult',{}).get('errortext', status))
 
         return status == self.ASYNC_SUCCESS
 
diff --git test/common/test_cloudstack.py test/common/test_cloudstack.py
index ecac341..9eee132 100644
--- test/common/test_cloudstack.py
+++ test/common/test_cloudstack.py
@@ -21,6 +21,7 @@ from libcloud.common.types import MalformedResponseError
 
 from test import MockHttpTestCase
 
+
 async_delay = 0
 
 class CloudStackMockDriver(object):
@@ -63,7 +64,8 @@ class CloudStackCommonTest(unittest.TestCase):
         self.driver.path = '/async/fail'
         try:
             self.connection._async_request('fake')
-        except:
+        except Exception, e:
+            self.assertEquals(CloudStackMockHttp.ERROR_TEXT, str(e))
             return
         self.assertFalse(True)
 
@@ -109,6 +111,9 @@ class CloudStackCommonTest(unittest.TestCase):
             self.assertEqual(connection._make_signature(params), b(case[1]))
 
 class CloudStackMockHttp(MockHttpTestCase):
+
+    ERROR_TEXT = 'ERROR TEXT'
+
     def _response(self, status, result, response):
         return (status, json.dumps(result), result, response)
 
@@ -156,7 +161,7 @@ class CloudStackMockHttp(MockHttpTestCase):
             result = {
                 query['command'].lower() + 'response': {
                     'jobstatus': 2,
-                    'jobresult': {'fake': 'failresult'}
+                    'jobresult': {'errortext': self.ERROR_TEXT}
                 }
             }
         else:
-- 
1.7.1

