Missing error handling in WebSockets code
utopian-io·@nafestw·
0.000 HBDMissing error handling in WebSockets code
The code for sending requests via WebSockets misses proper error handling, such that problems cannot be handled in user code. #### How to reproduce * Should be reproducible on any PC that runs Node.js * Disconnect from the Internet * Run the following code with Node.js ``` const steem = require("steem"); steem.api.setOptions({ url: "wss://steemd.privex.io" }); steem.api.getAccounts(["nafestw"], (err, resp) => { console.log("Callback is called. err = " + err); }); ``` #### Expected behavior The callback is called with `err` set to an object describing the error, which should be printed to the console. #### Actual behavior The following error occurs and the callback function is **not** called. ``` Unhandled rejection Error: getaddrinfo ENOTFOUND steemd.privex.io steemd.privex.io:443 at errnoException (dns.js:55:10) at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:97:26) ``` #### Suggested Fix Adding the missing `.catch` in `src/api/transports/ws.js` should fix the problem: ``` diff --git a/src/api/transports/ws.js b/src/api/transports/ws.js index 863ec4f..814d701 100644 --- a/src/api/transports/ws.js +++ b/src/api/transports/ws.js @@ -94,7 +94,7 @@ export default class WsTransport extends Transport { this._requests.set(_request.message.id, _request); this.ws.send(JSON.stringify(_request.message)); return deferral; - }); + }).catch(err => callback(err, null)); } onError(error) { ``` <br /><hr/><em>Posted on <a href="https://utopian.io/utopian-io/@nafestw/missing-error-handling-in-websockets-code">Utopian.io - Rewarding Open Source Contributors</a></em><hr/>