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
await client.connectby yourthrow(anywayawait throw...is not valid JS). So, yes, your throw is synchronous. It does happen during the execution ofconnect. Butclient.connectis not. Thethrowthat occurs during its operation does not occur during the execution ofclient.connect()(which is just a function that returns a promise. So unless the fabrication of that promise fails...).throwhere. The point still stands. Yes I'm using proper coding techniques and methods (not a complete novice here lol). If you look at my other examples, no matter how I handle theclient.connectI get an exception, andcatchno matter the context never catches the exception.client.connectis asynchronous. That means that all it does, is creating a promise. You would catch the exception if the creation of the promise were to fail (but of course, it can't). But not if, later, well afterclient.connecthas returned, the fullfilling of the promise fails. I know that withasync/awaitthis is hidden, and code appears to be the one ofconnect, not the one executed when the promise if fullfilled (or, worse, of a continuation of a continuation of a ..., of that promise, since eachawaityou have is that). Yet, it is just a promise.