Skip to content

Instantly share code, notes, and snippets.

@alhoo
Created May 27, 2023 05:12
Show Gist options
  • Save alhoo/3b0d0c177bed1e91c879fb84749a8adc to your computer and use it in GitHub Desktop.
Save alhoo/3b0d0c177bed1e91c879fb84749a8adc to your computer and use it in GitHub Desktop.
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