Created
May 27, 2023 05:12
-
-
Save alhoo/3b0d0c177bed1e91c879fb84749a8adc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def print_traceback(e): | |
""" | |
Prints a formatted traceback of the given exception, including filename, line number, function name, code line, | |
and local variables for each frame in the traceback stack. | |
Args: | |
e (Exception): The exception object for which the traceback should be printed. | |
Returns: | |
None | |
""" | |
import traceback | |
import json | |
tb = traceback.TracebackException.from_exception(e, capture_locals=True) | |
print( | |
json.dumps( | |
[ | |
{ | |
"filename": frame.filename, | |
"lineno": frame.lineno, | |
"name": frame.name, | |
"line": frame.line, | |
"locals": frame.locals, | |
} | |
for frame in tb.stack | |
], | |
indent=2, | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment