node-consul icon indicating copy to clipboard operation
node-consul copied to clipboard

nodejs application crashes when callback not supplied

Open yury-kozlov opened this issue 6 years ago • 1 comments

consul: v0.37.0 node: v13.3.0

Consider this code:

setInterval(() => { console.log('alive'); }, 1000);
try {
    const consul = require('consul')({ promisify: false });
    consul.health.service({ service: 'some-service' }, /*callback not supplied*/);
} catch (error) {
   // this code is not reached
    console.log(error);
}

On Windows, the app will stop without any notification. On Linux application crashes, and I get this error in the output: TypeError: Cannot read property 'apply' of undefined at next (/home/y/node_modules/papi/lib/client.js:305:32) at Consul.body (/home/y/node_modules/consul/lib/utils.js:18:27) at next (/home/y/node_modules/papi/lib/client.js:311:10) at ClientRequest.<anonymous> (/home/y/node_modules/papi/lib/client.js:505:5) at ClientRequest.emit (events.js:219:5) at Socket.socketErrorListener (_http_client.js:420:9) at Socket.emit (events.js:219:5) at emitErrorNT (internal/streams/destroy.js:84:8) at processTicksAndRejections (internal/process/task_queues.js:84:21)

The expected behavior is some validation error that can be caught by try catch, instead of failing whole application. I tested this only on health.service, but I believe the issue may happen anywhere as long as you don't pass callback function.

yury-kozlov avatar Jan 06 '20 21:01 yury-kozlov

I've got this problem too. Doc already indicate the option promisify but I missed it. Ex:

const consul = new Consul({promisify: true})

noomz avatar Aug 28 '21 10:08 noomz

The post 1.x version is promise only, so this is no longer a problem.

silas avatar Dec 21 '22 15:12 silas