Add more specific exception for backenddb Entity
authorLin Yang <lin.a.yang@intel.com>
Mon, 1 Jul 2013 08:18:52 +0000 (16:18 +0800)
committerGerrit Code Review <gerrit2@otctools.jf.intel.com>
Mon, 1 Jul 2013 09:14:05 +0000 (02:14 -0700)
Change-Id: Ie232a034c1fc753d3c07d83b4a612a51ac395890
Signed-off-by: Lin Yang <lin.a.yang@intel.com>
common/backenddb.py

index 3c7c4af..97b8653 100644 (file)
@@ -20,6 +20,10 @@ import yaml
 import json
 from copy import deepcopy
 
+class EntityError(Exception):
+    """Entity custom exception."""
+    pass
+
 class BackendDBError(Exception):
     """BackendDB custom exception."""
     pass
@@ -76,6 +80,12 @@ class Entity(object):
             value = self._db.get(key)
             if self.jsoned:
                 value = json.loads(value)
+        elif self._db.type(key) == 'none':
+            raise EntityError("[Error] key %s don't exist in redis" % key)
+        else:
+            raise EntityError("[Error] don't support \"%s\" data in redis" \
+                                 % self._db.type(key))
+
         return value
 
     def __setitem__(self, key, value):
@@ -91,8 +101,11 @@ class Entity(object):
                 mvalue[i] = json.dumps(mvalue[i])
             for item in mvalue:
                 self._db.rpush(key, item)
-        else:
+        elif isinstance(value, str):
             self._db.set(key, value)
+        else:
+            raise EntityError("[Error] don't support \"%s\" data in redis" \
+                                 % type(value))
 
     def delete(self, key):
         """Remove key starting with the prefix from the db."""