Open ScavengerSpb opened 2 years ago
Code to reproduce this issue:
const Consul = require('consul');
const leaderElection = require('exp-leader-election');
const config = {
debug: console.log,
key: 'test/leader',
consul: {
host: 'localhost',
port: '8500'
}
};
leaderElection(config).on('gainedLeadership', () => {
setTimeout(() => {
Consul(config.consul).kv.del(config.key, () => {});
}, 100);
});
Disclaimer: The issue occurred during debugging so it's unlikely to happen in wildlife.
Due to some reason the leader key was deleted so consul returned 404 error code for which consul library (0.40.0) has special handling. This resulted in
undefined
being pushed to callback as the call result ofclient.kv.get
method. Console output with debug messages printed to console:It seems when key to read is not found Consul library returns result as
undefined
to indicate call succeeded but key was not found. It's expected situation so error is not provided as well. Handling of the call result inconsul
package:Callback passed to
client.kv.get
does not check result fornull
/undefined
so this results in attempt to access property ofundefined
: