Skip to content

Instantly share code, notes, and snippets.

@davidimoore
Forked from rpdelaney/traceback_exc.py
Created November 1, 2023 22:12
Show Gist options
  • Save davidimoore/cae9c9a0fdd7120ca5f79e7f0019269a to your computer and use it in GitHub Desktop.
Save davidimoore/cae9c9a0fdd7120ca5f79e7f0019269a to your computer and use it in GitHub Desktop.
Serialize python exceptions into dictionaries
import traceback
from typing import Dict, Union, List
def traceback_exc(exc: Exception) -> Dict[str, Union[str, List[str]]]:
tb = traceback.TracebackException.from_exception(
exc, capture_locals=True
)
return {
"title": type(exc).__name__,
"message": str(exc),
"traceback": [line.split("\n") for line in tb.format()],
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment