better string truncation for error messages
authorTero Marttila <terom@fixme.fi>
Mon, 16 Feb 2009 02:55:17 +0200
changeset 136 c69a176b3620
parent 135 19ff083c2870
child 137 a25d1bf758e6
better string truncation for error messages
error.py
--- a/error.py	Mon Feb 16 02:49:06 2009 +0200
+++ b/error.py	Mon Feb 16 02:55:17 2009 +0200
@@ -4,6 +4,17 @@
 
 import traceback, sys, cgi, urllib
 
+def truncate (msg, limit) :
+    """
+        Truncate the given message to <limit> chars
+    """
+
+    if len(msg) > limit :
+        return msg[:limit-3] + '...'
+
+    else :
+        return msg
+
 def build_error (exc_info=None, env=None) :
     """
         Dumps out a raw traceback of the given/current exception to stdout.
@@ -51,7 +62,7 @@
         version = None
     
     # the exception type
-    exception_str = traceback.format_exception_only(*exc_info[:2])[-1][:255]
+    exception_str = traceback.format_exception_only(*exc_info[:2])[-1]
 
     # the exception traceback
     traceback_lines = traceback.format_exception(*exc_info)
@@ -72,7 +83,7 @@
         submit_args['revision'] = version
     
     if exception_str :
-        submit_args['summary'] = exception_str
+        submit_args['summary'] = truncate(exception_str, 140)
 
     if traceback_lines :
         # this is big
@@ -108,6 +119,6 @@
         traceback           = cgi.escape(''.join(traceback_lines)),
         query_url           = query_url,
         submit_url          = submit_url,
-        submit_url_short    = submit_url[:120] + '...'
+        submit_url_short    = truncate(submit_url, 120)
     )).encode('utf-8'))