Mike Orr committed 2bd0a9e

New ``format_exception()`` helper.

  • Participants
  • Parent commits c67b3a6

Comments (0)

Files changed (2)

 tip (development version)
-No changes yet.
+* webhelpers.misc:
+ - New ``format_exception`` helper to display an exception as Python would but
+   without the traceback.
 1.0b7 (2010-05-16)

File webhelpers/

 import itertools
+import traceback
 import types
 import warnings
         Exception.__init__(self, message)
         self.filename = filename
+def format_exception(exc=None):
+    """Format the exception type and value for display, without the traceback.
+    This is the function you always wished were in the ``traceback`` module but
+    isn't. It's *different* from ``traceback.format_exception``, which includes
+    the traceback, returns a list of lines, and has a trailing newline.
+    If you don't provide an exception object as an argument, it will call
+    ``sys.exc_info()`` to get the current exception.
+    """
+    if exc:
+        exc_type = type(exc)
+    else:
+        exc_type, exc = sys.exc_info()[:2]
+    lines = traceback.format_exception_only(exc_type, exc)
+    return "".join(lines).rstrip()
 def deprecate(message, pending=False, stacklevel=2):
     """Issue a deprecation warning.