skydiver / ewelink-api

eWeLink API for JavaScript
https://www.npmjs.com/package/ewelink-api
MIT License
264 stars 108 forks source link

Uncaught exception when DNS resolution fails #186

Open johnjmikucki opened 1 year ago

johnjmikucki commented 1 year ago
Aug 27 04:30:17 nodered[91277]: openWebSocket.js: Reconnecting...
Aug 27 04:30:22 nodered[91277]: 27 Aug 08:30:22 - [red] Uncaught Exception:
Aug 27 04:30:22 nodered[91277]: 27 Aug 08:30:22 - [error] FetchError: request to https://us-api.coolkit.cc:8080/api/user/login failed, reason>
Aug 27 04:30:22 nodered[91277]:     at ClientRequest.<anonymous> (/data/node_modules/ewelink-api/node_modules/node-fetch/lib/index.js:1491:11)
Aug 27 04:30:22 nodered[91277]:     at ClientRequest.emit (node:events:527:28)
Aug 27 04:30:22 nodered[91277]:     at TLSSocket.socketErrorListener (node:_http_client:454:9)
Aug 27 04:30:22 nodered[91277]:     at TLSSocket.emit (node:events:527:28)
Aug 27 04:30:22 nodered[91277]:     at emitErrorNT (node:internal/streams/destroy:157:8)
Aug 27 04:30:22 nodered[91277]:     at emitErrorCloseNT (node:internal/streams/destroy:122:3)
Aug 27 04:30:22 nodered[91277]:     at processTicksAndRejections (node:internal/process/task_queues:83:21)
Aug 27 04:30:22 podman[115215]: 2022-08-27 04:30:22.957355225 -0400 EDT m=+0.355075954 container remove de0c4c4c8274f9d7628ab76c8789d0a3b054a>
Aug 27 04:30:22 systemd[1]: container-nodered.service: Main process exited, code=exited, status=1/FAILURE

Opening this as a new issue on the package b/c the issue with the proximally-failing code is closed. (See discussed-not-committed) patch suggested by @LiYuanBR (https://github.com/LiYuanBr) at https://github.com/skydiver/ewelink-api/issues/65#issuecomment-1226180143. Their reconnection attempt in the catch block can ITSELF fail (in my case b/c DNS server failed), and when it does any exceptions are unhandled.

I've exactly zero javascript experience, so I'm loathe to propose an implementation, but think the wssLoginPayload() call needs to be wrapped in its own try-catch block which simply logs the exception and retries. I'm happy to help further diagnose / test proposed patches. If my fooling around with it produces something potentially useful, I'll post it here.

PatalJunior commented 1 year ago

@johnjmikucki Check #187 , and download my fork, as I don't know if they will merge it and get it updated. I implemented the fix in #65 as @LiYuanBr suggested. Hope it helps. Here is my fixed version: https://github.com/PatalJunior/ewelink-api/tree/origin/master