So you have some Python code in a try-catch, and you want the typical Python stacktrace (aka traceback, aka backtrace) in a way you can manipulate?
Here are some ways to handle it:
import traceback
import logging
try:
stuff()
except Exception:
# Just print traceback
print "something went wrong, here is some info:"
traceback.print_exc()
# Get traceback as a string and do something with it
error = traceback.format_exc()
print error.upper()
# Log it through logging channel
logging.error('Ooops', exc_info=True)
corresponding output:
something went wrong, here is some info:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
NameError: name 'stuff' is not defined
TRACEBACK (MOST RECENT CALL LAST):
FILE "<STDIN>", LINE 2, IN <MODULE>
NAMEERROR: NAME 'STUFF' IS NOT DEFINED
ERROR:root:Ooops
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
NameError: name 'stuff' is not defined