Source code for hbutils.reflection.exception

"""
Overview:
    This module provides utility functions for handling exceptions and traceback objects.
    It includes functions to print and retrieve full backtrace information from exception objects,
    which is useful for debugging and error logging purposes.
"""
import io
import traceback

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






[docs] def str_traceback(err: BaseException) -> str: """ Get full backtrace for exception object as a string. :param err: Exception object to extract traceback from. :type err: BaseException :return: Full string representation of the backtrace. :rtype: str Example:: >>> try: ... raise RuntimeError('runtime error') ... except RuntimeError as e: ... s = str_traceback(e) ... print(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()