[Kimchi-devel] [PATCH 4/8] refator exception: Update control to raise the exception message

Aline Manera alinefm at linux.vnet.ibm.com
Sun Feb 9 22:47:58 UTC 2014


From: Aline Manera <alinefm at br.ibm.com>

The error messages raised on control were being built in there instead of
using the exception message
And the errors from jsonschema validation also were displayed to the user
without a customization what can cause a misunderstanding as it contains
technical words.
Fix it in order to raise the translated messages to the user.

Signed-off-by: Aline Manera <alinefm at br.ibm.com>
---
 src/kimchi/control/base.py  |   90 ++++++++++++++++++++-----------------------
 src/kimchi/control/utils.py |    5 +--
 2 files changed, 44 insertions(+), 51 deletions(-)

diff --git a/src/kimchi/control/base.py b/src/kimchi/control/base.py
index f50ff6e..102b53c 100644
--- a/src/kimchi/control/base.py
+++ b/src/kimchi/control/base.py
@@ -70,18 +70,16 @@ class Resource(object):
                 fn(*model_args)
                 uri_params = tuple(self.model_args)
                 raise internal_redirect(self.uri_fmt % uri_params)
-            except MissingParameter, param:
-                error = "Missing parameter: '%s'" % param
-                raise cherrypy.HTTPError(400, error)
-            except InvalidParameter, param:
-                error = "Invalid parameter: '%s'" % param
-                raise cherrypy.HTTPError(400, error)
-            except InvalidOperation, msg:
-                raise cherrypy.HTTPError(400, "Invalid operation: '%s'" % msg)
-            except OperationFailed, msg:
-                raise cherrypy.HTTPError(500, "Operation Failed: '%s'" % msg)
-            except NotFoundError, msg:
-                raise cherrypy.HTTPError(404, "Not found: '%s'" % msg)
+            except MissingParameter, e:
+                raise cherrypy.HTTPError(400, e.message)
+            except InvalidParameter, e:
+                raise cherrypy.HTTPError(400, e.message)
+            except InvalidOperation, e:
+                raise cherrypy.HTTPError(400, e.message)
+            except OperationFailed, e:
+                raise cherrypy.HTTPError(500, e.message)
+            except NotFoundError, e:
+                raise cherrypy.HTTPError(404, e.message)
 
         wrapper.__name__ = action_name
         wrapper.exposed = True
@@ -102,10 +100,10 @@ class Resource(object):
         except AttributeError:
             error = "Delete is not allowed for %s" % get_class_name(self)
             raise cherrypy.HTTPError(405, error)
-        except OperationFailed, msg:
-            raise cherrypy.HTTPError(500, "Operation Failed: '%s'" % msg)
-        except InvalidOperation, msg:
-            raise cherrypy.HTTPError(400, "Invalid operation: '%s'" % msg)
+        except OperationFailed, e:
+            raise cherrypy.HTTPError(500, e.message)
+        except InvalidOperation, e:
+            raise cherrypy.HTTPError(400, e.message)
 
     @cherrypy.expose
     def index(self):
@@ -113,26 +111,26 @@ class Resource(object):
         if method == 'GET':
             try:
                 return self.get()
-            except NotFoundError, msg:
-                raise cherrypy.HTTPError(404, "Not found: '%s'" % msg)
-            except InvalidOperation, msg:
-                raise cherrypy.HTTPError(400, "Invalid operation: '%s'" % msg)
-            except OperationFailed, msg:
-                raise cherrypy.HTTPError(406, "Operation failed: '%s'" % msg)
+            except NotFoundError, e:
+                raise cherrypy.HTTPError(404, e.message)
+            except InvalidOperation, e:
+                raise cherrypy.HTTPError(400, e.message)
+            except OperationFailed, e:
+                raise cherrypy.HTTPError(406, e.message)
         elif method == 'DELETE':
             try:
                 return self.delete()
-            except NotFoundError, msg:
-                raise cherrypy.HTTPError(404, "Not found: '%s'" % msg)
+            except NotFoundError, e:
+                raise cherrypy.HTTPError(404, e.message)
         elif method == 'PUT':
             try:
                 return self.update()
-            except InvalidParameter, msg:
-                raise cherrypy.HTTPError(400, "Invalid parameter: '%s'" % msg)
-            except InvalidOperation, msg:
-                raise cherrypy.HTTPError(400, "Invalid operation: '%s'" % msg)
-            except NotFoundError, msg:
-                raise cherrypy.HTTPError(404, "Not found: '%s'" % msg)
+            except InvalidParameter, e:
+                raise cherrypy.HTTPError(400, e.message)
+            except InvalidOperation, e:
+                raise cherrypy.HTTPError(400, e.message)
+            except NotFoundError, e:
+                raise cherrypy.HTTPError(404, e.message)
 
     def update(self):
         try:
@@ -264,28 +262,24 @@ class Collection(object):
                 filter_params = cherrypy.request.params
                 validate_params(filter_params, self, 'get_list')
                 return self.get(filter_params)
-            except InvalidOperation, param:
-                error = "Invalid operation: '%s'" % param
-                raise cherrypy.HTTPError(400, error)
-            except NotFoundError, param:
-                raise cherrypy.HTTPError(404, "Not found: '%s'" % param)
+            except InvalidOperation, e:
+                raise cherrypy.HTTPError(400, e.message)
+            except NotFoundError, e:
+                raise cherrypy.HTTPError(404, e.message)
 
         elif method == 'POST':
             try:
                 return self.create(*args)
-            except MissingParameter, param:
-                error = "Missing parameter: '%s'" % param
-                raise cherrypy.HTTPError(400, error)
-            except InvalidParameter, param:
-                error = "Invalid parameter: '%s'" % param
-                raise cherrypy.HTTPError(400, error)
-            except OperationFailed, param:
-                raise cherrypy.HTTPError(500, "Operation Failed: '%s'" % param)
-            except InvalidOperation, param:
-                error = "Invalid operation: '%s'" % param
-                raise cherrypy.HTTPError(400, error)
-            except NotFoundError, param:
-                raise cherrypy.HTTPError(404, "Not found: '%s'" % param)
+            except MissingParameter, e:
+                raise cherrypy.HTTPError(400, e.message)
+            except InvalidParameter, e:
+                raise cherrypy.HTTPError(400, e.message)
+            except OperationFailed, e:
+                raise cherrypy.HTTPError(500, e.message)
+            except InvalidOperation, e:
+                raise cherrypy.HTTPError(400, e.message)
+            except NotFoundError, e:
+                raise cherrypy.HTTPError(404, e.message)
 
 
 class AsyncCollection(Collection):
diff --git a/src/kimchi/control/utils.py b/src/kimchi/control/utils.py
index 9c6878b..3e06100 100644
--- a/src/kimchi/control/utils.py
+++ b/src/kimchi/control/utils.py
@@ -101,9 +101,8 @@ def validate_params(params, instance, action):
 
     try:
         validator.validate(request)
-    except ValidationError:
-        raise InvalidParameter('; '.join(
-            e.message for e in validator.iter_errors(request)))
+    except ValidationError, e:
+        raise InvalidParameter(e.schema['error'], {'value': str(e.instance)})
 
 
 class UrlSubNode(object):
-- 
1.7.10.4




More information about the Kimchi-devel mailing list