On Fri, 2010-12-10 at 20:58 -0800, Toshio Kuratomi wrote:
> This doesn't look right to me.  I think what you're trying to do is take an
> Exception object, find the simple repr of that object, and then use it as
> either part of a log message.

 Right.

> AFAICT, this function is taking an Exception object and attempting to return
> a string representation of it.  The problem it seems to be addressing is
> that Exception objects don't have a defined interface.

 Actually the main problem it's dealing with is that we mostly get "yum
exceptions" but they are in both unicode() and str(). These all have the
message in .value (or they do now, anyway) ... but the unicode/str thing
makes python barf (stupid python).
 We do get stuff like IOError too ... but that's mostly a bug.

>   So while some
> Exceptions might have a value arg, others might have msg, others message,
> others args[0], etc.  This function is trying to get something reasonable no
> matter what form the Exception object takes.
> 
> This function, though, has several problems:
> 
> * It can return either unicode or byte str.  That means that when you got to
>   use the return value from this you have the potential to raise
>   a UnicodeError.

 Yeh, it's mostly a to_unicode() but I put the str() in there just in
case ... because that would be the best thing to do, not in python.

> * It checks a very small subset of the common ways to express what the
>   Exception is as a string
> 
> I'd probably code a function like this instead:
> 
> def exception2unicode(e):
>     # add a function for each of the methods you want to try for converting
>     # from an exception object to a unicode string.  They will be tried in
>     # order
>     conversion_funcs = (str, unicode, lambda e: str(e.args[0]),  lambda e: 
> str(e.value))
>     msg = None
>     for func in conversion_funcs:
>         try:
>             msg = func(e)
>         except:
>             pass
>         else:
>             break
>     if msg:
>         return to_unicode(msg)
>     return u'<exception failed to convert to text>'

 If you want to test that with a few error messages (in non-us :), feel
free to submit a patch :).

_______________________________________________
Yum-devel mailing list
[email protected]
http://lists.baseurl.org/mailman/listinfo/yum-devel

Reply via email to