Skip to content

Instantly share code, notes, and snippets.

@rpdelaney
Last active May 13, 2024 08:34
Show Gist options
  • Save rpdelaney/40acab4284abae0220f833ba66f64227 to your computer and use it in GitHub Desktop.
Save rpdelaney/40acab4284abae0220f833ba66f64227 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
for part in tb.format()
for line in part.split("\n")
if line.strip()
],
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment