Hi,
I have some code similar to this:
async function bootStrap () {
await redis.connect();
// ...other things
}
bootStrap()
.catch(err => {
console.log('-------------');
console.log(err);
console.log('-------------');
process.exit(1);
});
The problem is that, when the Redis server is not connected, this code produces:
[ioredis] Unhandled error event: Error: connect ECONNREFUSED 127.0.0.1:6379
at Object.exports._errnoException (util.js:1033:11)
at exports._exceptionWithHostPort (util.js:1056:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1099:14)
-------------
Error: Connection is closed.
at Redis.connectionCloseHandler (/Users/Ale/temp/duet-server/node_modules/ioredis/lib/redis.js:312:16)
at Object.onceWrapper (events.js:293:19)
at emitOne (events.js:96:13)
at Redis.emit (events.js:191:7)
at _combinedTickCallback (internal/process/next_tick.js:73:7)
at process._tickCallback (internal/process/next_tick.js:104:9)
-------------
Why does it have the Unhandled error event at the beginning? Shouldn't that be passed to the rejected promise on redis.connect()?
@luin @shaharmor
Thanks!
There are two mechanisms that ioredis used to track connection status: Promise and event. Promise that returned by Redis#connect() will reject as soon as the connection is failed (even a reconnection will be made). Meanwhile, "error" event will be emitted when the connection is failed (no matter whether you caught the Promise error).
It makes sense.. is there a way to make the two mutually exclusive? For example, if we're using promises ignore the "error" event, since it should be handled by the rejection anyways.
No, but you can simply listen to the error event explictly to avoid the "Unhandled" warning:
redis.on('error', err => {
// handle the err here or just ignore them
});
Yep, that I thought of, thx!
redis.on('error', err => {
window.location.href = "https://stackoverflow.com?q="+err;
});
Most helpful comment
redis.on('error', err => { window.location.href = "https://stackoverflow.com?q="+err; });