How to get the exception stack traceback.format_exc()
as a List/Dictionary in order to then serialize it with json.dumps()?
Listing [Python.Docs]: traceback - Print or retrieve a stack traceback.
If my understanding is correct, you're after something like this:
>>> import os, traceback >>> >>> try: os.open() ... except: tb = traceback.format_exc() ... else: tb = None ... >>> print(type(tb), tb) <class 'str'> Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: open() missing required argument 'path' (pos 1) >>> [i for i in x.split("\n") if i] ['Traceback (most recent call last):', ' File "<stdin>", line 2, in <module>', "NameError: name 'os' is not defined"]
Once you have the exception traceback string, you can now manipulate it in any way you wish.