You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@libcloud.apache.org by rb...@apache.org on 2010/08/06 19:59:39 UTC

svn commit: r983075 - in /incubator/libcloud/trunk: libcloud/drivers/gogrid.py libcloud/types.py test/test_gogrid.py

Author: rbogorodskiy
Date: Fri Aug  6 17:59:39 2010
New Revision: 983075

URL: http://svn.apache.org/viewvc?rev=983075&view=rev
Log:
Start of exceptions reogranization.

The following this was done:

- Added LibCloudException as a base one so other
  exceptions could subclass from it
- Converted existing exceptions to inherit from
  LibCloudException
- Added new exception MalformedResponseException

LibCloudException by defaults supports providing information
about the driver which excpetions has been raised in, e.g.:

raise LibCloudExceptions("Things go wrong", FooBarDriver)

so client could figure out which driver triggered an error 
by checking driver.name or maybe using some other way.

However, driver argument is optional and defaults to None, made
this in order not to break an existing code.
M    test/test_gogrid.py
M    libcloud/types.py
M    libcloud/drivers/gogrid.py

Modified:
    incubator/libcloud/trunk/libcloud/drivers/gogrid.py
    incubator/libcloud/trunk/libcloud/types.py
    incubator/libcloud/trunk/test/test_gogrid.py

Modified: incubator/libcloud/trunk/libcloud/drivers/gogrid.py
URL: http://svn.apache.org/viewvc/incubator/libcloud/trunk/libcloud/drivers/gogrid.py?rev=983075&r1=983074&r2=983075&view=diff
==============================================================================
--- incubator/libcloud/trunk/libcloud/drivers/gogrid.py (original)
+++ incubator/libcloud/trunk/libcloud/drivers/gogrid.py Fri Aug  6 17:59:39 2010
@@ -16,7 +16,7 @@
 GoGrid driver
 """
 from libcloud.providers import Provider
-from libcloud.types import NodeState, InvalidCredsException
+from libcloud.types import NodeState, MalformedResponseException, InvalidCredsException
 from libcloud.base import Node, ConnectionUserAndKey, Response, NodeDriver
 from libcloud.base import NodeSize, NodeImage, NodeLocation
 import time
@@ -75,10 +75,13 @@ GOGRID_INSTANCE_TYPES = {'512MB': {'id':
 class GoGridResponse(Response):
     def success(self):
         if self.status == 403:
-            raise InvalidCredsException()
+            raise InvalidCredsException('Invalid credentials', GoGridNodeDriver)
         if not self.body:
             return None
-        return json.loads(self.body)['status'] == 'success'
+        try:
+            return json.loads(self.body)['status'] == 'success'
+        except ValueError:
+            raise MalformedResponseException('Malformed reply', GoGridNodeDriver)
 
     def parse_body(self):
         if not self.body:

Modified: incubator/libcloud/trunk/libcloud/types.py
URL: http://svn.apache.org/viewvc/incubator/libcloud/trunk/libcloud/types.py?rev=983075&r1=983074&r2=983075&view=diff
==============================================================================
--- incubator/libcloud/trunk/libcloud/types.py (original)
+++ incubator/libcloud/trunk/libcloud/types.py Fri Aug  6 17:59:39 2010
@@ -73,14 +73,27 @@ class NodeState(object):
     PENDING = 3
     UNKNOWN = 4
 
-class InvalidCredsException(Exception):
+class LibCloudException(Exception):
+    """The base class for other libcloud exceptions"""
+    def __init__(self, value, driver=None):
+        self.value = value
+        self.driver = driver
+
+class MalformedResponseException(LibCloudException):
+    """Exception for the cases when a provider returns a malformed
+    response, e.g. you request JSON and provider returns 
+    '<h3>something</h3>' due to some error on their side."""
+    pass
+
+class InvalidCredsException(LibCloudException):
     """Exception used when invalid credentials are used on a provider."""
-    def __init__(self, value='Invalid credentials with the provider'):
+    def __init__(self, value='Invalid credentials with the provider', driver=None):
         self.value = value
+        self.driver = driver
     def __str__(self):
         return repr(self.value)
 
-class DeploymentException(Exception):
+class DeploymentException(LibCloudException):
     """
     Exception used when a Deployment Task failed.
 

Modified: incubator/libcloud/trunk/test/test_gogrid.py
URL: http://svn.apache.org/viewvc/incubator/libcloud/trunk/test/test_gogrid.py?rev=983075&r1=983074&r2=983075&view=diff
==============================================================================
--- incubator/libcloud/trunk/test/test_gogrid.py (original)
+++ incubator/libcloud/trunk/test/test_gogrid.py Fri Aug  6 17:59:39 2010
@@ -20,6 +20,7 @@ try:
 except ImportError:
     import simplejson as json
 
+from libcloud.types import LibCloudException, InvalidCredsException
 from libcloud.drivers.gogrid import GoGridNodeDriver
 from libcloud.base import Node, NodeImage, NodeSize
 
@@ -32,6 +33,7 @@ class GoGridTests(unittest.TestCase, Tes
 
     def setUp(self):
         GoGridNodeDriver.connectionCls.conn_classes = (None, GoGridMockHttp)
+        GoGridMockHttp.type = None
         self.driver = GoGridNodeDriver("foo", "bar")
 
     def test_create_node(self):
@@ -66,6 +68,25 @@ class GoGridTests(unittest.TestCase, Tes
         self.assertEqual(image.name, 'CentOS 5.3 (32-bit) w/ None')
         self.assertEqual(image.id, '1531')
 
+    def test_malformed_reply(self):
+        GoGridMockHttp.type = 'FAIL'
+        try:
+            images = self.driver.list_images()
+        except LibCloudException, e:
+            self.assertEqual(True, isinstance(e, LibCloudException))
+        else:
+            self.fail("test should have thrown")
+
+    def test_invalid_creds(self):
+        GoGridMockHttp.type = 'FAIL'
+        try:
+            nodes = self.driver.list_nodes()
+        except InvalidCredsException, e:
+            self.assertTrue(e.driver is not None)
+            self.assertEqual(e.driver.name, self.driver.name)
+        else:
+            self.fail("test should have thrown")
+
 class GoGridMockHttp(MockHttp):
 
     fixtures = FileFixtures('gogrid')
@@ -74,10 +95,17 @@ class GoGridMockHttp(MockHttp):
         body = self.fixtures.load('image_list.json')
         return (httplib.OK, body, {}, httplib.responses[httplib.OK])
 
+    def _api_grid_image_list_FAIL(self, method, url, body, headers):
+        body = "<h3>some non valid json here</h3>"
+        return (httplib.SERVICE_UNAVAILABLE, body, {}, httplib.responses[httplib.SERVICE_UNAVAILABLE])
+
     def _api_grid_server_list(self, method, url, body, headers):
         body = self.fixtures.load('server_list.json')
         return (httplib.OK, body, {}, httplib.responses[httplib.OK])
 
+    def _api_grid_server_list_FAIL(self, method, url, body, headers):
+        return (httplib.FORBIDDEN, "123", {}, httplib.responses[httplib.FORBIDDEN])
+
     def _api_grid_ip_list(self, method, url, body, headers):
         body = self.fixtures.load('ip_list.json')
         return (httplib.OK, body, {}, httplib.responses[httplib.OK])