Skip to content

Expose special exceptions via robot.api #3685

Closed
Closed

Description

@pekkaklarck

By default exceptions raised by library keywords stop executing the current test (except for possible teardown) and move execution to the next test. We have, however, some special exceptions that change this logic:

  • Continuable failures allow execution to continue. The test will be marked failed in the end anyway.
  • Fatal errors stop the whole test execution.
  • Skip marks the test skipped, not failed.

The way users can mark exceptions continuable or fatal is by adding special attribute ROBOT_CONTINUE_ON_FAILURE or ROBOT_EXIT_ON_FAILURE, respectively, and the plan is to support the same approach with skip as well. There have been two reasons for using special attributes instead of exposing custom exceptions via the robot.api module:

  • Attributes work also with Java based exceptions when using Jython.
  • We wanted it to be possible to write library code without needing to have a dependency to the robot module.

Both of the above reasons are still valid, but it would nevertheless often be useful to be able to import custom exceptions directly without a need to create them. I propose the following:

  • Create custom exceptions for continuable failures, fatal errors and skip.
  • Expose them via the robot.api module.
  • Internally keep using the special attributes for detecting these special exceptions. This means that all old code will continue to work.

Activity

  1. added this to the v4.0 milestone on Sep 28, 2020
  2. pekkaklarck commented on Sep 28, 2020

    @pekkaklarck
    MemberAuthor

    The hardest part of implementing this issue is, as it often is, naming:

    1. Where exactly to put these exceptions? Should they go directly to robot.api or possibly a submodule like robot.api.error? Most of the API is exposed directly via robot.api (most of the stuff is implemented elsewhere) but we also have robot.api.logger and robot.api.deco. I consider these somewhat special cases and believe it would be best to put exceptions directly to robot.api.

    2. What exception names to use?

      1. FatalError sounds like a good name for fatal errors.

      2. Continuable failure could be simply ContinuableFailure but we could also consider something like SoftFailure to use terminology other tools (at least TestNG) uses. This terminology change would require updating the User Guide and would require its own issue.

      3. Skipping could be implemented with with SkipExecution or just Skip.

      Regardless the names we decide to use, I believe all exceptions should use another special attribute ROBOT_SUPPRESS_NAME to avoid showing the actual exception name in the final error message.

  3. aaltat commented on Sep 28, 2020

    @aaltat
    Contributor

    I vote for ContinuableFailure

  4. pekkaklarck commented on Sep 28, 2020

    @pekkaklarck
    MemberAuthor

    Other things to consider:

    1. If we add and expose exceptions for special cases, should we also add and expose keywords for normal failures? We could add Failure that extends AssertionError to match ContinuableFailure (or SoftFailure) and FatalError (or FatalFailure).

    2. If we add Failure to be used with normal failing validations, should we also add Error to be used with invalid usages and other non-validation related failures? Robot doesn't separate failures and errors but on library level there's a semantic difference.

    3. Should we expose existing PassExecution? It's used by the Pass Execution keyword and can mark the test passed without executing it fully. The idea has been that libraries shouldn't be allowed to do that, but they can already now import this exception from robot.errors which is not considered to be part of the public API and this functionality isn't documented either. I don't see any harm with this but I'm not sure is this worth the effort either.

    4. Should we expose existing ContinueForLoop, ExitForLoop and ReturnFromKeyword? I think that's not a good idea as these exceptions are used for internal communication related to Robot syntax.

  5. pekkaklarck commented on Oct 1, 2020

    @pekkaklarck
    MemberAuthor

    The more I think about it, the more I like the term "soft failure" over "continuable failure". I submitted #3692 about changing the terminology with an idea to do that already in RF 4.0. If there are objections, the decision can still be reverted.

  6. pekkaklarck commented on Oct 15, 2020

    @pekkaklarck
    MemberAuthor

    These exceptions were created in 02bf414:

    • Failure
    • ContinuableFailure
    • Error
    • FatalError
    • SkipExecution

    Failure and Error are basically aliases for AssertionError and RuntimeError, respectively, but they also act as base classes for ContinuableFailure and FatalError.

    Decided to use ContinuableFailure, not SoftFailure, because there weren't that much interest towards renaming the terminology (#3692). ContinuableFailure also better explains how the exception behaves.

    These exceptions are implemented in new robot.api.exceptions module but they are also exposed directly via the robot.api package.

  7. adiralashiva8 commented on Nov 4, 2020

    @adiralashiva8

    Need html message format support while raising exceptions

    Example:

     from robot.api import SkipExecution
     
     def skip_execution():
         raise SkipExecution('<b>Skipped from execution</b>', html=True)
    

    Current implementation doesn't support this.

  8. added a commit that references this issue on Dec 27, 2020
    9bc2376
  9. pekkaklarck commented on Feb 26, 2021

    @pekkaklarck
    MemberAuthor

    @adiralashiva8 You ought to be able to prefix the message with *HTML* like *HTML* <b>Skipped from execution</b> like you can with all error messages. I like the idea to add a separate html argument, though. If you submit an issue about it, we could add it in the future.

  10. pekkaklarck commented on Mar 3, 2021

    @pekkaklarck
    MemberAuthor

    #3866 was submitted about supporting HTML messages. I think that's a good idea and it's so easy to implement that I'll do that still be fore RF 4.0.

  11. pekkaklarck commented on Mar 3, 2021

    @pekkaklarck
    MemberAuthor

    HTML support was added by the commit above. A functional change caused by this change is that these exceptions now require a message. That means, for example, that

    raise SkipExecution
    

    doesn't work anymore and you need to use

    raise SkipExecution('Some message')
    

    instead. I consider that good but if someone has use cases for raising these exceptions without a message changing this can be still changed.

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