I catch an error in two places, still, it seems to propagate further on to my socket server, killing it. Below is a incomplete summary of my code. Apologies for failing to providing a true minimal reproducable example I could not trigger it in simple terms.
Outer catch
import WebSocket from 'ws';
export const run = async (ws: WebSocket) => {
const executor = new Executor()
const execution = executor.execute()
try {
for await(const update of execution) {
ws.send(update.stringify())
}
} catch(error) {
console.log("Outer catch", error)
}
}
Executor (Inner catch)
class Executor {
async *execute() {
const promise = generator.next()
// Handle run error
promise.catch((error: Error) => {
console.log("Inner catch", error)
})
}
}
Result (order of errors)
Log: "Inner catch", <THE_SAME_ERROR>
Log: "Outer catch", <THE_SAME_ERROR>!!
Error: <THE_SAME_ERROR>!!!
[nodemon] app crashed
What are some reason this could be happening?
Edit
Im trowing the error on purpose, its as simple as
throw Error('Some message')
This error will be thrown in generator.next()
above, which returns a promise that I don’t await. Hence, Im registering the catch callback just after.
2
Answers
Not a complete answer to my problem and I don't understand why, but I could fix my issue by chaining
then
andcatch
. Compare these examples:Failing
Succeeding
Could you try to expand your try/catch scope to:
And probably share a bit of context of your error message