Skip to content

Include information about chained exceptions in failure tracebacks #4039

Closed

Description

@Ka55i0peia

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 exc

A little test case

*** Settings ***
Library     SimpleLib.py


*** Test Cases ***
Failing Test Case With Robot Exception Trace
    Failing Keyword

Prints:

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

  1. pekkaklarck commented on Jul 15, 2021

    @pekkaklarck
    Member

    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 well

  2. added this to the v5.0 milestone on Jul 15, 2021
  3. pekkaklarck commented on Nov 5, 2021

    @pekkaklarck
    Member

    I've been thinking how to format the chained message. Most likely the best approach is using same format that Python uses:

    >>> try:
    ...     1/0
    ... except Exception as e:
    ...     raise ValueError('Bang!') from e
    ... 
    Traceback (most recent call last):
      File "<stdin>", line 2, in <module>
    ZeroDivisionError: division by zero
    
    The above exception was the direct cause of the following exception:
    
    Traceback (most recent call last):
      File "<stdin>", line 4, in <module>
    ValueError: Bang!

    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.

  4. changed the title [-]Incomplete Exception Trace in log.html (Exception Chaining)[/-] [+]Include information about chained exceptions in failure tracebacks[/+] on Nov 10, 2021
  5. pekkaklarck commented on Nov 10, 2021

    @pekkaklarck
    Member

    I was able to do this by using traceback.format_exception() instead of traceback.format_tb(). Removing traceback lines originating from our code when using it was a bit tricky, but finally managed to do that by modifying exception.__traceback__ in addition to passing the modified traceback to format_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.

  6. pekkaklarck commented on Sep 27, 2022

    @pekkaklarck
    Member

    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.

  7. added 2 commits that reference this issue on Jul 15, 2025
    b6405ef
    c94b4bf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions