PerditionC / VBAChromeDevProtocol

VBA (Excel) based wrapper for Chrome Developer Protocol (CDP) - sorta a VBA version of Puppeteer/Selenium
https://PerditionC.github.io/VBAChromeDevProtocol/
MIT License
60 stars 11 forks source link

Error handling of disconnected browser #11

Open moaoci opened 2 years ago

moaoci commented 2 years ago

The error ( halt to the STOP instruction ) occurs when the browser is manually disconnected while the user macros are running. Might also happen if www site returns a time out.

Mine occurs in AutomateBrowser.querySelectorAll, (other functions might be also be affected.)

ErrHandler: Debug.Print "AutomateBrowser.querySelectorAll - " & Err.description Stop Resume End Function

The RESUME after the STOP will rapidly bring back to the STOP and on and on.

The documentation says «' Note: resulting Dictionary will be empty (0 items) if an error or no matching elements found»

Changing RESUME to RESUME NEXT seems to work, returning 0 items as stated.

After returning 0 items, is there a way to verify if browser is unavailable or if querySelectorAll simply did not find anything ? If so, the STOP could also be removed.

I am not at ease with the whole AutomateBrowser module. Other ErrHandler: simply stop before end function, no resume. Can someone verify ?

PerditionC commented 2 years ago

The error handling needs improving. Currently it is just setup for debugging issues, does not handle disconnection well. There are a few ways I can implement this, but I'm not sure what is the best. I can have the function return 0 items and set the global error variable to a value for error, e.g. disconnected. Or I can have the call throw the error - letting higher up calls see and handle the error. I don't want to just ignore the error (which is really what the resume and resume next would do), but I'm not sure the best way to communicate it to the caller. So far I'm thinking adding an error or disconnected function to AutomateBrowser class. Then add a check so before making the CDP call to the browser check if still connected and either fail or perhaps try reconnecting first then fail if not connected/other error. I'm open to suggestions - just throw error to caller or handle it and internally note error occurred?

tonton81 commented 1 year ago

i use on error goto xxxx in vba, since it polls the code randomly, a disconnected browser wont cause an exception to be thrown in vba. at the same time you can have your function return a response like "ERROR" or "SUCCESS" so your other code can process the payload if it was successful.

IMHO the error handling should be done by the user like i did above, because in either case the user would still need to identify whether the library was a success or not before processing the request..., and at the same time if there are errors without the goto call it's easy to debug what caused it.

so in my function a successful transaction function returns "SUCCESS", and if the link to chrome was broken "ERROR" is returned via goto. So upon receiving a "SUCCESS" (my function would for example dump the page to a hidden sheet), the sheet would be processed. if an ERROR was returned, code wouldnt execute the hidden sheet.