def exc_str(exc=None, limit=None):
"""Enhanced str for exceptions. Should include original location
Parameters
----------
Exception to
"""
out = str(exc)
if limit is None:
# TODO: config logging.exceptions.traceback_levels = 1
limit = int(os.environ.get('NICEMAN_EXC_STR_TBLIMIT', '1'))
try:
exctype, value, tb = sys.exc_info()
if not exc:
exc = value
out = str(exc)
# verify that it seems to be the exception we were passed
#assert(isinstance(exc, exctype))
if exc:
assert(exc is value)
entries = traceback.extract_tb(tb)
if entries:
out += " [%s]" % (','.join(['%s:%s:%d' % (os.path.basename(x[0]), x[2], x[1]) for x in entries[-limit:]]))
except:
return out # To the best of our abilities
finally:
# As the bible teaches us:
# https://docs.python.org/2/library/sys.html#sys.exc_info
del tb
return out
评论列表
文章目录