Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhanced error handling with error(True) to raise exception on error [done] #299

Closed
kensoh opened this issue Aug 30, 2021 · 2 comments
Closed
Labels

Comments

@kensoh
Copy link
Member

kensoh commented Aug 30, 2021

user query from Telegram group chat on function hook error handling - https://t.me/rpa_chat/5886


Thanks for raising this! i open a new issue to explore this - #299

Will take some time, because I maintain the Python version in my personal free time. There are generally 2 ideas, 1 is a function hook. 1 is an option to raise error so that users can do a try-catch design.

For the time being, you can do -

if r.click() and r.type() and etc etc:
    print('SUCCESS')
   # do success actions

if not r.click() and not r.type() and ...:
    print('FAIL')
    # do fail actions

Above works because each function call will return True or False depending on whether it is successful.

You can also split into multiple lines if you want the Python script to be neater -

if r.click() and \
    r.type() and \
    ...:
    print('SUCCESS')
@kensoh kensoh added the feature label Aug 30, 2021
@kensoh kensoh changed the title Review enhancing error handling, eg function hook or raise error option - to explore Apr 19, 2022
@kensoh
Copy link
Member Author

kensoh commented Apr 26, 2022

Between the 2 ideas of having a function hook or raising exception for user-handling with try-catch, I would think the 2nd method is more accessible to users (more commonly known design pattern), and also gives more flexibility what to be done (calling some user-defined function, or do something else depending on the section of the script).

@kensoh kensoh changed the title Enhancing error handling, eg function hook or raise error option - to explore Apr 26, 2022
@kensoh kensoh changed the title Enhance error handling by adding error() option to raise exception on error - to explore Apr 26, 2022
@kensoh
Copy link
Member Author

kensoh commented Apr 27, 2022

Done in v1.47 and available with pip install rpa --upgrade

To handle errors during automation, use error(True) and Python try-except

  1. Example to error-handle a specific step by checking value of Exception e
r.error(True)

try:
    r.click('element 1')
    r.dclick('element 2')
    r.type('element 3', 'abc')

except Exception as e:
    print(e) # show specific error message
    # do something to handle that error
  1. Example to error-handle a series of steps without concern of failed step
r.error(True)

try:
    r.click('element 1')
    r.dclick('element 2')
    r.type('element 3', 'abc')

except:
    # the step that failed is not known
    # do something to deal with error

PS - default setting on initialising is error(False), so Python exceptions will not be raised on error

kensoh added a commit that referenced this issue Apr 27, 2022
@kensoh kensoh changed the title Enhance error handling by adding error() option to raise exception on error - to do Apr 27, 2022
@kensoh kensoh changed the title Enhance error handling by adding error() option to raise exception on error [done] Apr 27, 2022
@kensoh kensoh changed the title Enhance error handling with error() option to raise exception on error [done] Apr 27, 2022
@kensoh kensoh changed the title Enhance error handling with error() setting to raise exception on error [done] Apr 27, 2022
@kensoh kensoh changed the title Enhanced error handling with error() setting to raise exception on error [done] Apr 27, 2022
@kensoh kensoh closed this as completed Jul 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1 participant