nodejs application crashes when callback not supplied
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.
I've got this problem too. Doc already indicate the option promisify but I missed it. Ex:
const consul = new Consul({promisify: true})
The post 1.x version is promise only, so this is no longer a problem.