I’m trying to handle exceptions thrown by NodeJS using TypeScript.
For example, if I get an ECONNREFUSED
exception, it’s of type SystemError
.
But as of now, because err
is of type any
, I can only do this
redisClient.on('error', (err) => {
console.log('Error: ', err);
});
But I would like to narrow down the error type, similar to this:
redisClient.on('error', (err) => {
if (err instanceof ErrnoException) {
if(err.code === 'ECONNREFUSED ') {
throw new Error('Connection refused');
}
}
throw err;
});
But unfortunately, SystemError
is not exported in Node.
I did find a discussion on GitHub from 4 years ago regarding NodeJS.ErrnoException
, but seems like it was removed.
Is it currently possible in NodeJS?