node-ethernet-ip icon indicating copy to clipboard operation
node-ethernet-ip copied to clipboard

Read/Write Tags/Groups error friendly await

Open pcnate opened this issue 5 years ago • 1 comments

Read/Write Tags/Groups should be more await friendly by allowing const [ error, data ] = PLC.readTag( tag )

Current Behavior

You can currently await any of these functions but if an error happens, it throws an uncaught promise error which are pretty annoying to track down. The workaround is to write a wrapper for each function that returns a promise and catch errors yourself which makes the current level of support for await pointless.

Expected Behavior

You should be able to handle all errors like so:

const [ error, data ] = PLC.readTag( tag )
if ( error ) {
    // handle the error, log error, break you current block, etc
}

Another option could be to pass an error callback but... or an options object to control return structure

Possible Solution (Optional)

Each of these functions should contain a then and a catch and both should return something and not cause unhandled promise rejection errors. Returning [ false, data ] could be a breaking change however so a callback could be added to be executed on the catch block.

I currently use await-to-js to "awaitify" these functions.

Context

Trying to use async await much more than I used to and encountering unexpected issues or forgetting to use helper functions.

Steps to Reproduce (for bugs only)

  1. Use any await example from the README
  2. Try it on a tag that doesn't exist or throws some other error
  3. Program dies

Your Environment

  • Package version (Use npm list - e.g. 1.0.6):
  • Node Version (Use node --version - e.g. 9.8.0):
  • Operating System and version:
  • Controller Type (eg 1756-L83E/B):
  • Controller Firmware (eg 30.11):

pcnate avatar Mar 25 '19 15:03 pcnate

this might be worth implementing in v2.0

pcnate avatar Mar 25 '19 21:03 pcnate