Hi,

when I tried to build a custom live cd I noticed some problems in the error reporting when there were unicode error messages (e.g. some dependency was missing).

Example:
...
anaconda-11.4.1.63-1.i386 von updates hat Abhängigkeitsauflöse-Probleme
  --> Fehlende Abhängigkeit: booty wird benötigt von Paket 
anaconda-11.4.1.63-1.i386 (updates)
Traceback (most recent call last):
  File "./tools/livecd-creator", line 140, in <module>
    sys.exit(main())
  File "./tools/livecd-creator", line 132, in main
    logging.error("Error creating Live CD : %s" % e)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in position 
42: ordinal not in range(128)

My patch ensures that unicode error messages can be printed correctly every time. To get 100% unicode support, one should go through all log calls and ensure that all strings are unicode so that localized error messages are always shown correctly.

With my patch, there should be no unicode exceptions anymore, even when the error message may be printed as 'Fehlende Abh\xe4ngigkeit' instead of 'Fehlende Abhängigkeit' (notice the '\xe4' in the first string).

fs
diff --git a/imgcreate/errors.py b/imgcreate/errors.py
index ba08563..071d4b8 100644
--- a/imgcreate/errors.py
+++ b/imgcreate/errors.py
@@ -20,6 +20,29 @@ class CreatorError(Exception):
     """An exception base class for all imgcreate errors."""
     def __init__(self, msg):
         Exception.__init__(self, msg)
+    
+    # Some error messages may contain unicode strings (especially if your 
system
+    # locale is different from 'C', e.g. 'de_DE'). Python's exception class 
does
+    # not handle this appropriately (at least until 2.5) because str(Exception)
+    # returns just self.message without ensuring that all characters can be
+    # represented using ASCII. So we try to return a str and fall back to repr
+    # if this does not work.
+    # 
+    # Please use unicode for your error logging strings so that we can really
+    # print nice error messages, e.g.:
+    #     log.error(u"Internal error: " % e)
+    # instead of
+    #     log.error("Internal error: " % e)
+    # With our custom __str__ and __unicode__ methods both will work but the 
+    # first log call print a more readable error message.
+    def __str__(self):
+        try:
+            return str(self.message)
+        except UnicodeEncodeError:
+            return repr(self.message)
+    
+    def __unicode__(self):
+        return unicode(self.message)
 
 class KickstartError(CreatorError):
     pass
diff --git a/tools/livecd-creator b/tools/livecd-creator
index 1aab882..39f7478 100755
--- a/tools/livecd-creator
+++ b/tools/livecd-creator
@@ -129,7 +129,7 @@ def main():
         creator.unmount()
         creator.package()
     except imgcreate.CreatorError, e:
-        logging.error("Error creating Live CD : %s" % e)
+        logging.error(u"Error creating Live CD : %s" % e)
         return 1
     finally:
         creator.cleanup()
--
Fedora-livecd-list mailing list
Fedora-livecd-list@redhat.com
https://www.redhat.com/mailman/listinfo/fedora-livecd-list

Reply via email to