I'm building a tool where as exceptions propagate upwards, new data about the context of the exception gets added to the exception. The issue is, by the time the exception gets to the top level, all of the extra context data is there, but only the very latest stack trace is shown. Is there an easy way to have an exception show the original stack trace in which it was thrown instead of the last stack trace, or should I do something like grab the original stack trace the first time that the exception propagates?
For example, the following code:
def a():
return UNBOUND
def b():
try:
a()
except Exception as e:
raise e
b()
yields the following exception:
Traceback (most recent call last):
File "test.py", line 8, in <module>
b()
File "test.py", line 7, in b
raise e
NameError: global name 'UNBOUND' is not defined
where, ideally, I'd like to somehow show the user this:
Traceback (most recent call last):
File "test.py", line 8, in <module>
File "test.py", line 2, in a
return UNBOUND
NameError: global name 'UNBOUND' is not defined
As that points the user to the line that the error originally occurred on.