[Kimchi-devel] [PATCH 2/3] refactor exception: Create a common Exception to translate error messages

Aline Manera alinefm at linux.vnet.ibm.com
Fri Jan 31 18:20:52 UTC 2014


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

KimchiError will be the base for all other Exception types in Kimchi
It will translate the error message before raising it.
Then UI can use the message directly. That way we avoid duplicating
messages on UI and backend.

Signed-off-by: Aline Manera <alinefm at br.ibm.com>
---
 src/kimchi/exception.py |   29 ++++++++++++++++++++++-------
 1 file changed, 22 insertions(+), 7 deletions(-)

diff --git a/src/kimchi/exception.py b/src/kimchi/exception.py
index a37015b..f7dc200 100644
--- a/src/kimchi/exception.py
+++ b/src/kimchi/exception.py
@@ -20,30 +20,45 @@
 # License along with this library; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 
+import cherrypy
+import gettext
 
-class NotFoundError(Exception):
+from kimchi.template import get_lang, validate_language
+
+
+class KimchiError(Exception):
+    def __init__(self, msg, params):
+        lang = validate_language(get_lang())
+        paths = cherrypy.request.app.root.paths
+        translation = gettext.translation('kimchi', paths.mo_dir, [lang])
+        text = msg.encode('utf-8')
+        self.msg =  unicode(translation.gettext(text), 'utf-8') % params
+        Exception.__init__(self, self.msg)
+
+
+class NotFoundError(KimchiError):
     pass
 
 
-class OperationFailed(Exception):
+class OperationFailed(KimchiError):
     pass
 
 
-class MissingParameter(Exception):
+class MissingParameter(KimchiError):
     pass
 
 
-class InvalidParameter(Exception):
+class InvalidParameter(KimchiError):
     pass
 
 
-class InvalidOperation(Exception):
+class InvalidOperation(KimchiError):
     pass
 
 
-class IsoFormatError(Exception):
+class IsoFormatError(KimchiError):
     pass
 
 
-class TimeoutExpired(Exception):
+class TimeoutExpired(KimchiError):
     pass
-- 
1.7.10.4




More information about the Kimchi-devel mailing list