How to properly handle exceptions to avoid process exiting?
galaczi opened this issue · 2 comments
According to the docs:
Note: If your items can potentially throw an exception, you must handle those errors from the returned Promise or they may be reported as an unhandled Promise rejection and potentially cause your process to exit immediately.
I tried this:
try {
await queue.add(async () => {
handleClosed()
})
} catch (error) {
console.log('Error thrown in queue.')
}
Still, if an error is thrown in handleClosed
, the whole process exits. What am I missing?
Edit: same result with this:
await queue.add(async () => {
try {
handleClosed()
} catch (error) {
console.log('Error thrown in queue.')
}
})
Edit 2: Maybe I am misunderstanding what the docs say. I shouldn't throw errors inside jobs at all (or should catch inside)?
Anyone knows how to avoid the main process exiting on error in any of the jobs? I found some old issues, people asking for it. Why would you want the whole process to exit on error in a single job, I don't get it.
I think you are missing an await
await queue.add(async () => {
try {
await handleClosed()
} catch (error) {
console.log('Error thrown in queue.')
}
})