fierce-oil-47448
10/09/2024, 11:14 PMfrom sys import exc_info
from flytekit.exceptions.user import FlyteRecoverableException
from flytekit.exceptions.scopes import FlyteScopedUserException
try:
try:
raise RuntimeError("a")
except Exception as e:
raise FlyteRecoverableException("b") from e
except Exception:
ex = FlyteScopedUserException(*exc_info()) # this is how Flyte wraps up user exceptions
print(ex.verbose_message)
This produces:
Traceback (most recent call last):
File "<stdin>", line 5, in <module>
Message:
FlyteRecoverableException: USER:Recoverable: error=b, cause=a
User error.
See how there is only a single traceback.
Compare to:
from flytekit.exceptions.user import FlyteRecoverableException
import traceback
try:
try:
raise RuntimeError("a")
except Exception as e:
raise FlyteRecoverableException("b") from e
except Exception:
traceback.print_exc()
which correctly produces 2 stack traces:
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
RuntimeError: a
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<stdin>", line 5, in <module>
flytekit.exceptions.user.FlyteRecoverableException: USER:Recoverable: error=b, cause=a
Seems like a problem with FlyteScopedUserException
.thankful-minister-83577
fierce-oil-47448
10/09/2024, 11:19 PMfierce-oil-47448
10/09/2024, 11:20 PMthankful-minister-83577
thankful-minister-83577
fierce-oil-47448
10/09/2024, 11:36 PMfierce-oil-47448
10/09/2024, 11:36 PMdef get_traceback_str(e: Exception) -> str:
if isinstance(e, FlyteUserRuntimeException):
# If the exception is a user exception, we want to capture the traceback of the exception that was raised by the
# user code, not the Flyte internals.
tb = e.__cause__.__traceback__ if e.__cause__ else e.__traceback__
else:
tb = e.__traceback__
lines = traceback.format_tb(tb)
lines = [line.rstrip() for line in lines]
tb_str = "\n ".join(lines)
format_str = "Traceback (most recent call last):\n" "\n {traceback}\n" "\n" "Message:\n" "\n" " {message}"
value = e.value if isinstance(e, FlyteUserRuntimeException) else e
return format_str.format(traceback=tb_str, message=f"{type(value).__name__}: {value}")
This is the new method I see in the latest version. With the new version, I think this method can be improved to print the nested cause.fierce-oil-47448
10/09/2024, 11:53 PMthankful-minister-83577
fierce-oil-47448
10/10/2024, 12:36 AMfierce-oil-47448
10/15/2024, 5:19 AM