handle unicode in CreatorError and UsageError
authorGui Chen <gui.chen@intel.com>
Thu, 18 Oct 2012 14:02:36 +0000 (22:02 +0800)
committerGui Chen <gui.chen@intel.com>
Thu, 18 Oct 2012 14:02:36 +0000 (22:02 +0800)
when code like 'raise CreatorError(str(e))' and e.msg is unicode,
this will be failed, this patch will fix such issue

Signed-off-by: Gui Chen <gui.chen@intel.com>
mic/utils/errors.py

index 5e7777b..8d720f9 100644 (file)
@@ -24,13 +24,21 @@ class CreatorError(Exception):
         self.msg = msg
 
     def __str__(self):
-        return self.keyword + str(self.msg)
+        if isinstance(self.msg, unicode):
+            self.msg = self.msg.encode('utf-8', 'ignore')
+        else:
+            self.msg = str(self.msg)
+        return self.keyword + self.msg
 
 class Usage(CreatorError):
     keyword = '<usage>'
 
     def __str__(self):
-        return self.keyword + str(self.msg) + ', please use "--help" for more info'
+        if isinstance(self.msg, unicode):
+            self.msg = self.msg.encode('utf-8', 'ignore')
+        else:
+            self.msg = str(self.msg)
+        return self.keyword + self.msg + ', please use "--help" for more info'
 
 class Abort(CreatorError):
     keyword = ''