Repository navigation
Description
Hey there,
a very useful feature when debugging python implemented keywords, is first to inspect the exception trace of a failing keyword. When running robot with log level debug (robot -L DEBUG testsuite.robot), right below the error message a debug message will be logged into log.html printing the python exception trace. That is quite nice and useful!
But this exception trace is printing only the last level exception.
Problem: In complex libraries when the original exception gets re-raise using raise NewException() from OriginalException (aka Exception Chaining), only the trace of NewException getting printed. For diving down to the exception origin the complete trace is required.
Edit
There seems to be a strong relation to #3828. (Anyway, issue occurs here too, which i don't expected when watching the changes.)
Here is a little sample. (Checked with RF 3.2.2, RF 4.0.3, but also [v4.1rc1](https://github.com/robotframework/robotframework/releases/tag/v4.1rc1))
Some little python library:
from robot.api.deco import library
@library(scope='GLOBAL', doc_format='reST', auto_keywords=True)
class SimpleLib:
@staticmethod
def failing_keyword() -> None:
my_dict = {}
try:
my_dict['not_existing']
except KeyError as exc:
raise RuntimeError("Some usefull message") from excA little test case
*** Settings ***
Library SimpleLib.py
*** Test Cases ***
Failing Test Case With Robot Exception Trace
Failing KeywordPrints:
13:30:49.862 FAIL Some usefull message
13:30:49.862 DEBUG Traceback (most recent call last):
File "/home/user/projects/hades/sw-test/selftest/log_exception/SimpleLib.py", line 18, in failing_keyword
raise RuntimeError("Some usefull message") from exc
Expecting (like logging parameter exc_info does, e.g. logger.debug("Trace:", exc_info=exc))
13:30:49.864 FAIL Some usefull message
13:30:49.865 DEBUG Trace:
Traceback (most recent call last):
File "/home/user/projects/hades/sw-test/selftest/log_exception/SimpleLib.py", line 16, in failing_keyword
my_dict['not_existing']
KeyError: 'not_existing'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/user/projects/hades/sw-test/selftest/log_exception/SimpleLib.py", line 23, in log_trace
SimpleLib.failing_keyword()
File "/home/user/projects/hades/sw-test/selftest/log_exception/SimpleLib.py", line 18, in failing_keyword
raise RuntimeError("Some usefull message") from exc
RuntimeError: Some usefull message
Activity
pekkaklarck commentedon Jul 15, 2021
Sounds good to me. Especially after we drop Python 2 support so we don't need to do conditional exception handling. Could be fairly easy to implement as well.
I assign this to RF 5 scope but cannot guarantee it will in the end be included. If you want to make sure it is, please provide a PR.
Formatting you get with
logging.debug("Trace:", exc_info=exc)is provided by the Python code used when implementing #3828. Most likely we could use same approach where traceback logged when a keyword fails is formatted as wellpekkaklarck commentedon Nov 5, 2021
I've been thinking how to format the chained message. Most likely the best approach is using same format that Python uses:
Unfortunately it seems we cannot use traceback.format_exception that produces this output because it doesn't have a way to filter out traceback lines originating from our own code. We currently have our own traceback handling code that takes care of filtering. Most likely the best approach is enhancing that code to handle chaining so that the result looks like what Python itself produces.
One difference compared to how Python formats traceback compared to what we produce is that Python adds the exception type and message at the end like
ValueError: Bang!above. Probably would be good to first change our traceback handling so that we add that message as well.[-]Incomplete Exception Trace in log.html (Exception Chaining)[/-][+]Include information about chained exceptions in failure tracebacks[/+]pekkaklarck commentedon Nov 10, 2021
I was able to do this by using
traceback.format_exception()instead oftraceback.format_tb(). Removing traceback lines originating from our code when using it was a bit tricky, but finally managed to do that by modifyingexception.__traceback__in addition to passing the modified traceback toformat_exception(). Commit above ought to contain all code changes and it also has a unit test, but I forgot to add acceptance tests for chaining. Will do that next and then we can consider this issue done.pekkaklarck commentedon Sep 27, 2022
As explained in comments of #4476. information about chained exceptions wasn't included if the top level exception didn't have any traceback. This will be fixed in RF 5.1 as part of #4476.
Fix traceback with chained excaptions.
Enhance constructing failure tracebacks.
Acceptance tests for handling chained exceptions. Fixes robotframewor…