Newbie: Capture traceback message to string?

S

Sean Schertell

Hello!

I'm new to Python and this is my first post to the list.

I'm trying to simply capture exception text to a few strings which
can be passed to a PSP page to display a pretty error message. The
problem is that I just can't seem to figure out how to get the basic
components of the traceback message into strings. Here's what I want
to do:

---------------------
try:
bad_math = 1/0
except:
info = sys.exc_info()
main_error_str = # ???
full_traceback_str = # ???
tmpl = psp.PSP(req, filename='error.html')
tmpl.run({'main_error_str': main_error_str,
'full_traceback_str':full_traceback_str})

return apache.OK
---------------------

....So then my PSP page receives the errors as variables containing
strings.

I've been googling and experimenting for more hours than I care to
admit. A little help please?

Thanks!

Sean
 
T

Thomas Guettler

Sean said:
Hello!

I'm new to Python and this is my first post to the list.

I'm trying to simply capture exception text to a few strings which
can be passed to a PSP page to display a pretty error message. The
problem is that I just can't seem to figure out how to get the basic
components of the traceback message into strings. Here's what I want
to do:


# http://www.thomas-guettler.de/vortraege/python/beispiele.py.txt
def foo():
raise("Das ist eine Exception")
try:
foo()
except:
import traceback
import cStringIO
(exc_type, exc_value, tb) = sys.exc_info()
exc_file = cStringIO.StringIO()
traceback.print_exception(exc_type, exc_value, tb, file=exc_file)
exc_string=exc_file.getvalue()
print exc_string

Why don't you use cgitb? It is one reason why I like this language.

HTH,
Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top