Source code for hbutils.reflection.exception

"""
Overview:
    Useful functions to deal with exception or backtrace objects.
"""
import io
import traceback

__all__ = [
    'print_traceback',
    'str_traceback',
]





[docs]def str_traceback(err: BaseException) -> str: """ Overview: Get full backtrace for exception object. Arguments: - err (:obj:`BaseException`): Exception object. Returns: - backtrace (:obj:`str`): Full string backtrace. Examples: >>> try: >>> raise RuntimeError('runtime error') >>> except RuntimeError as e: >>> s = str_traceback(e) >>> s Traceback (most recent call last): File "<stdin>", line 2, in <module> raise RuntimeError('runtime error') RuntimeError: runtime error """ with io.StringIO() as fs: print_traceback(err, file=fs) return fs.getvalue()