Mike Orr committed 2bd0a9e

New ``format_exception()`` helper.

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)


 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.
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.