SuperFlyTV / xkeys

A Node.js module to interact with the X-keys panels.
MIT License
33 stars 12 forks source link

UnhandledPromiseRejectionWarning when watcher fails to open device #74

Closed Julusian closed 1 year ago

Julusian commented 2 years ago

Running the basic-log-all-events.js example, if I plug in an xkeys that node-hid is unable to open for some reason, it results in an UnhandledPromiseRejectionWarning.

It would be good to have these emitted as an 'error' event on the watcher, so that is is possible to catch them.

Full stack:

(node:482681) UnhandledPromiseRejectionWarning: TypeError: cannot open device with path /dev/hidraw1
    at new HID (/home/julus/Projects/xkeys/node_modules/node-hid/nodehid.js:49:17)
    at setupXkeysPanel (/home/julus/Projects/xkeys/packages/node/dist/methods.js:49:18)
    at XKeysWatcher.handleNewDevice (/home/julus/Projects/xkeys/packages/node/dist/watcher.js:201:32)
    at XKeysWatcher.updateConnectedDevices (/home/julus/Projects/xkeys/packages/node/dist/watcher.js:186:22)
    at Timeout._onTimeout (/home/julus/Projects/xkeys/packages/node/dist/watcher.js:141:22)
    at listOnTimeout (internal/timers.js:557:17)
    at processTimers (internal/timers.js:500:7)
(node:482681) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 6)
nytamin commented 1 year ago

It seems that the errors are already emitted as errors on XKeysWatcher.

In #67c2bfc I added the error listener to the documentation. When testing, the errors are emitted like so:

xkeys\packages\node>node examples\basic-log-all-events.js
Error in XKeysWatcher Error: cannot open device with path xyz
    at setupXkeysPanel (xkeys\packages\node\dist\methods.js:49:15)
    at XKeysWatcher.handleNewDevice (xkeys\packages\node\dist\watcher.js:196:32)
    at XKeysWatcher.updateConnectedDevices (xkeys\packages\node\dist\watcher.js:181:22)
    at Timeout._onTimeout (xkeys\packages\node\dist\watcher.js:136:22)
    at listOnTimeout (node:internal/timers:559:17)
    at processTimers (node:internal/timers:502:7)