node-modbus-serial icon indicating copy to clipboard operation
node-modbus-serial copied to clipboard

TransactionTimedOutError cannot be caught when using async/await

Open Jalle19 opened this issue 3 years ago • 4 comments

Not sure why but it's impossible to catch TransactionTimedOutError errors, they always end up triggering an unhandled rejection warning no matter where you place catch clauses.

After hours of debugging I resorted to giving up.

Jalle19 avatar Apr 19 '21 11:04 Jalle19

HI, thanks for the issue :green_heart:

Never give up :-) Can you paste-here/link-to-gist a small demo program that simulates the missing error ? It may help other people looking at this issue to have an idea how to help ?

yaacov avatar Apr 19 '21 11:04 yaacov

It's hard to reproduce, I think the easiest way is to set a really low timeout. Currently I'm using 5 seconds, and the error happens only once a day or something like that.

https://github.com/Jalle19/eda-modbus-bridge/blob/master/app/modbus.mjs#L29 here's an example of a call that (I think) triggers the error. Any of the interactions with modbusClient can seemingly trigger it.

I'm not entirely sure if the async-mutex library I'm using to guard access to the serial port is the real culprit here, but it's certainly possible?

Jalle19 avatar Apr 19 '21 13:04 Jalle19

The most I've been able to do when it comes to logging the error is this:

Apr 18 09:56:53 homeassistant b5a380b3f13b[390]: (node:204) UnhandledPromiseRejectionWarning: #<TransactionTimedOutError>
Apr 18 09:56:53 homeassistant b5a380b3f13b[390]:     at emitUnhandledRejectionWarning (internal/process/promises.js:170:15)
Apr 18 09:56:53 homeassistant b5a380b3f13b[390]: (node:204) 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: 22)
Apr 18 09:56:53 homeassistant b5a380b3f13b[390]: (node:204) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code

Jalle19 avatar Apr 19 '21 13:04 Jalle19

Had the same issue but imho it has nothing to do with the library but with the nature of async / await as you can read here: https://itnext.io/error-handling-with-async-await-in-js-26c3f20bc06a

Basically you can fix that by having a try / catch clause with an await inside

pfried avatar Nov 03 '21 10:11 pfried

Turns out this was PEBKAC :shrug:

Jalle19 avatar Aug 14 '23 17:08 Jalle19