After searching on SO, I could not find an answer to this problem. All of the questions I saw, the error was still being caught. I'm having the inverse problem. No matter how I refactor the following code, I am STILL not able to catch the error.
I hope I'm just overlooking something or doing something wrong.
process.on("uncaughtException", uncaughtExceptionListener)
start().catch(console.error)
async function start() {
try {
await queue.connect()
} catch (err) {
return console.error('Some Error:', err.message)
}
}
// queue.connect in class
async function connect() {
try {
// The next line is where the error originates
await client.connect()
return console.log('Never makes it here')
} catch (err) {
// Never makes it here either
return console.error('[client] Connection failed!')
}
}
Output:
node:internal/process/promises:391
triggerUncaughtException(err, true /* fromPromise */);
^
Error: connect ECONNREFUSED 127.0.0.1:6090
I've rewritten connect()
as simple as possible in multiple different ways:
function connect() {
const p2 = new Promise((resolve, reject) => {
client.connect().then(resolve).catch(reject)
})
p2.then(n => {
// ...
}).catch(e => {
console.log('Never makes it here')
})
return p2
}
And even more simply:
async connect() {
try {
// The next line is where the error originates
await client.connect()
} catch (err) {
return console.log('Never makes it here')
}
}
Replacing await client.connect()
with throw new Error('client.connect() simulation')
Everything works as it should. What is client.connect()
doing, where the exception can not be caught?
Going as far as encapsulating the actual .connect
method, the exception still happens and node crashes. Example:
client.connect().then(() => {}).catch(err => console.error('We never make it here'))
Version: (Although still happens on latest node version)
$ node --version
v20.18.0
Client in question: bee-queue
bee-queue.connect function
Solved. The issue was not in my code. The issue was upstream in the bee-queue
library. The promise was not being caught and rejected to the downstream promise chain.
See this PR for more info: https://github.com/bee-queue/bee-queue/pull/878