[fix] UnhandledPromiseRejection crashes the koa server
Matthewloughton opened this issue · comments
Node 16: UnhandledPromiseRejection crashes the koa server
Node.js version: v16.17.0
Description: When an UnhandledPromiseRejection occurs, the koa server crashes. I noticed that it seems to be on Node 16.
How I found this was while using a library in which they inadvertently caused an UnhandledPromiseRejection. Fortunately I caught this issue before experiencing in prod!
I also sandbox tested with Express and it does the same.
Using node 14 does not exhibit this behaviour which leads me to believe it's related to Node 16
Code to reproduce
const app = new Koa();
app.use(ctx => {
const prom = new Promise((resolve) => {
throw 'Blah';
});
});
app.listen(1234, () => {
console.log('running on 1234')
});
Update:
Did some reading (I've been detached from node's versioning lately).
It seems like UnhandledPromiseRejection is strict by default now.
Run node with flag --unhandled-rejections=warn-with-error-code
if you don't want it to crash
You guys can close this if you want. Your call
Another update:
You can write something like below to "catch" these unhandledRejections.
process.on('unhandledRejection', error => {
console.log('handle the error');
});
Closing this issue. Hope my monologue helps someone!