Skip to content

Native support for TRY/EXCEPT functionality #3075

Closed

Description

@pekkaklarck

The plan is to add support for native IF/ELSE syntax in RF 3.3 4.0. See the discussion in #3074 for background information why we think adding this kind of programmatic features is a good idea. Another similar and commonly needed feature is TRY/EXCEPT functionality to ease handling errors. Currently it is possible to use Run Keyword And Expect Error and Run Keyword And Ignore error but they are awkward similarly as Run Keyword If is an awkward replacement for IF/ELSE syntax.

Exact semantics how TRY/EXCEPT will work are not yet clear. They will be decided later before implementation starts. A precondition is a new parser, similarly as for IF/ELSE, and we also consider IF/ELSE more important and will start implementing it first.

Activity

  1. added this to the v3.3 milestone on Feb 4, 2019
  2. modified the milestones: v3.3, v4.0 on Feb 7, 2020
  3. Snooz82 commented on Apr 29, 2020

    @Snooz82
    Member

    Just an idea:

    At the moment we have already TRY/FINALLY with teststeps and Teardown.
    Teardown can be on every level.

    What is missing is only a [On-Error] setting.
    If we would have this [On-Error] we could also do stuff like capture screenshots there and closing connections an so on.

    I personally would not like to see a real TRY/EXCEPT like IF/ELSE, because the we start re-implementing Python.
    [On-Error] is also understandable for non programmers.

    *** Test Cases ***
    Test
        [Setup]     Open Browser.   http://example.com
        Do Keyword
        Some Other Test
        [On-Error]    Save error log and capture Screenshot
        [Teardown]    Close All Browsers
    

    Same could work on Suite and on Keyword Level.

    *** Settings *** 
    Suite Setup   Open Browser
    Error Handler    Save error log and capture Screenshot
    Suite Teardown    Close All Browsers
    
  4. pekkaklarck commented on Apr 30, 2020

    @pekkaklarck
    MemberAuthor

    I've been thinking about adding an on-error handler but nowadays I think TRY/EXCEPT would probably be better. Some of the problems I see with on-error handler:

    • Unless on-error can also ignore the error, it's not really different from teardown with Run Keyword If Test Failed.
    • If on-error should be able to ignore errors, it needed to get exception information as argument to be able to do decisions based on that.
    • One test (or keyword) could only have one on-error handler. If there were multiple possible errors to handle, the handler would get complicated.

    I got a feeling TRY/EXCEPT would be a lot more generic solution. In simple cases on-error might be easier to understand, but in more complex scenarios TRY/EXCEPT would be a lot more flexible.

    I also don't see problems in Robot's syntax moving towards a general very high level programming language. After replacing Run Keyword If with IF/ELSE and Run Keyword And Expect/Ignore Error with TRY/EXCEPT, we can look at replacing Exit/Continue For Loop with BREAK/CONTINUE, [Return] and Return From Keyword with RETURN, and so on.

  5. mkorpela commented on Sep 12, 2020

    @mkorpela
    Member

    How does this relate to expecting an error to happen? In otherwords many cases in tests the situation is so that an error must happen as expected, test should fail if not.

  6. pekkaklarck commented on Sep 12, 2020

    @pekkaklarck
    MemberAuthor

    We could support TRY/EXCEPT/ELSE like Python to support that use case.

  7. mkorpela commented on Sep 12, 2020

    @mkorpela
    Member

    One of the strongest selling points of Robot Framework is the simplicity for people coming from a non-IT-background. I think we should consider how easy a new core concept is to learn and how easy it is to teach.
    TRY/EXCEPT goes some steps towards more complex concepts.

  8. Snooz82 commented on Sep 12, 2020

    @Snooz82
    Member

    I do see usecases for Try/Except but these are mostly better done in python.

    I always hate it when i see testspecs with try catch in it. People try to implement execution logic into test design.

    @pekkaklarck you mentioned the "Run Keyword If Test Failed" this imho does not work on keyword level. But there are imho regular cases where you want to clean up or do stuff if something fails. And using TRY EXCEPT for this would imho be too technical. 🤷🏻‍♂️

  9. aikarjal commented on Sep 10, 2021

    @aikarjal

    +1 for this feature. There are valid use-cases for this in RPA. Right now the only way to handle errors is awkward Run Keyword And Expect Error

  10. kariharju commented on Sep 10, 2021

    @kariharju

    Definitely coming more and more of an issue, so nice to see this as critical v5 content.

  11. self-assigned this
    on Oct 19, 2021
  12. pekkaklarck commented on Dec 11, 2021

    @pekkaklarck
    MemberAuthor

    This feature has been developed in a branch and is getting ready to be merged. The basic functionality works like this:

    TRY
        Some keyword
    EXCEPT    Error message
        Error handler keyword
    END
    

    If Some keyword passes, the EXCEPT branch isn't run. If it fails with message Error message, the EXCEPT branch is executed. If it fails any other way, error is not handled. Any branch could naturally have more than one keyword.

    There can be more than one EXCEPT branches. In that case they are matched one by one and the first one matching is executed. One EXCEPT can also have multiple messages to match. If it has no messages to match, it matches any:

    TRY
        Some keyword
    EXCEPT    Error message
        Error handler 1
    EXCEPT    Alternative error
        Error handler 2
    EXCEPT    Another error    One more error    # Match either of these
        Error handler 3
    EXCEPT    # Match anything
        Error handler 3
    END
    

    By default matching the error requires an exact match. That can be changed by prefixing the message with GLOB:, REGEXP: or STARTS: to make the match a glob pattern match, regular expression match, or to match only the beginning of the error, respectively. If you are interested in the actual error message, you can assign it to a variable similarly as you can use except Exception as err in Python:

    TRY
        Some keyword
    EXCEPT    GLOB: Error *    AS    ${err}
        Log to console    Ignoring error: ${err}
    END
    

    Assigning to a variable works also if you don't specify the message at all i.e. when all messages match:

    TRY
        Some keyword
    EXCEPT    AS    ${message}
        Log to console    Error occurred: ${message}
    END
    

    It's possible to handle the case when there's no error using an ELSE branch like in Python:

    TRY
        Some keyword
    EXCEPT    Message
        Error handler keyword
    ELSE
        Log to console    No error!
    END
    

    Using ELSE without any EXCEPT branches is a syntax error.

    Finally, it's possible to execute code both when there's an error and when there isn't by using a FINALLY branch:

    TRY
        Some keyword
    EXCEPT    AS    ${err}
        Log to console    Error occurred: ${err}
    ELSE
        Log to console    No error occurred.
    FINALLY
        Log to console    Finally we are in FINALLY.
    END
    

    It is allowed to use FINALLY without EXCEPT branches. Such TRY/FINALLY structures can be used for cleaning up resources regardless are there failures or not:

    Open connection
    TRY
        Use connection
    FINALLY
        Close connection
    END
    

    Although this feature is mostly ready, there are still some design decisions that are still somewhat open. I'm going to write about them in separate comments.

  13. 35 remaining items

  14. pekkaklarck commented on Jan 30, 2022

    @pekkaklarck
    MemberAuthor

    I reopen this due to a bug in parsing EXCEPT with AS with data_only=False. This bug was initially reported as #4208 by @d-biehl and there's PR #4209 by him to fix the problem as well.

  15. pekkaklarck commented on Jan 31, 2022

    @pekkaklarck
    MemberAuthor

    The reported bug has now been fixed.

  16. pekkaklarck commented on Mar 6, 2022

    @pekkaklarck
    MemberAuthor

    We decided to add a support to limit how many times WHILE loops are executed to avoid endless loops. This limit is specified using limit=<value> like this:

    WHILE    ${condition}    limit=1 minute
        # ...
    END
    

    Now that we use option=value syntax with WHILE, it would be a good idea to use same syntax also with EXCEPT when controlling the pattern type. This is very late in RF 5.0 release cycle and we should already have the release out, but I believe it's better to change syntax still before the first release where TRY/EXCEPT is introduced than changing it later.

  17. pekkaklarck commented on Mar 6, 2022

    @pekkaklarck
    MemberAuthor

    My thinking is that the pattern type would be specified using pattern option. If should follow the actual pattern or patterns, but be before the AS part. If there are multiple patterns, the pattern configuration would apply to them all. In practice it would work like this:

    TRY
        Keyword
    EXCEPT    Error: *    pattern=glob
       Handler 1
    EXCEPT    (Another|Second) Error    Second [Pp]attern    pattern=regexp    AS    ${error}
        Handler 2
    END
    

    Things to decide:

    • Should we use pattern or something else like type?
    • Do we need to support matching only the start of the error message? We do that now, but just adding * at the end and using glob patterns is easy as well.
    • What exact value to use with regular expressions? Possible we could support both regex, and regexp.
    • What about case insensitivity? Should it be supported like pattern=case-insensitive or should we add another option that could be used like case_insensitive=True?
  18. pekkaklarck commented on Mar 9, 2022

    @pekkaklarck
    MemberAuthor

    Decisions related to the new syntax to specify the pattern type:

    • The configuration option is named type.
    • Matching from start is supported. The name of that type is start.
    • Regular expression matching type is regexp. There's no alias.
    • Case-insensitivity support is not added now. Can be done in future releases if needed.
  19. pekkaklarck commented on Mar 9, 2022

    @pekkaklarck
    MemberAuthor

    New pattern type syntax was implemented in bb4b01e. Documentation still needs to be updated but otherwise this ought to be done (again).

  20. added a commit that references this issue on Mar 9, 2022
  21. added a commit that references this issue on Mar 9, 2022
  22. pekkaklarck commented on Mar 9, 2022

    @pekkaklarck
    MemberAuthor

    Documentation and few more smallish fixes done in above commit. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions