reportlab/lib/utils.py: add annotateException
authorrgbecker
Tue, 17 Nov 2009 14:41:29 +0000
changeset 3253 06194e4167b9
parent 3252 94fdd5f8f031
child 3254 dd9042134279
reportlab/lib/utils.py: add annotateException
src/reportlab/lib/utils.py
--- a/src/reportlab/lib/utils.py	Mon Nov 16 17:43:58 2009 +0000
+++ b/src/reportlab/lib/utils.py	Tue Nov 17 14:41:29 2009 +0000
@@ -1107,3 +1107,27 @@
                 return pfn
     if fail: raise ValueError('cannot locate %r with paths=%r' % (fn,paths))
     return fn
+
+def annotateException(msg,enc='utf8'):
+    '''add msg to the args of an existing exception'''
+    t,v,b=sys.exc_info()
+    e = -1
+    A = list(v.args)
+    for i,a in enumerate(A):
+        if isinstance(a,basestring):
+            e = i
+            break
+    if e>=0:
+        if isinstance(a,unicode):
+            if not isinstance(msg,unicode):
+                msg=msg.decode(enc)
+        else:
+            if isinstance(msg,unicode):
+                msg=msg.encode(enc)
+            else:
+                msg = str(msg)
+        A[e] += msg
+    else:
+        A.append(msg)
+    v.args = tuple(A)
+    raise t,v,b