Home > other >  process.on('unhandledRejection') is not logging error
process.on('unhandledRejection') is not logging error

Time:04-23

In my server.js file i'm trying to log errors and not cause them to crash the app, this is the code im using

const server = app.listen(3000, () =>
    console.log('Server Up and running')
);

process.on('unhandledRejection', (err, promise) => {
    console.log(`Error: ${err}`);
    server.close(() => process.exit(1));
})

when i produce an error instead of it logging the error and closing the server it just logs the error how it normally would and causes the app to crash. If you need more info let me know. Sorry if this is a dumb question

CodePudding user response:

I suppose you know the difference between Unhandled Rejection and Uncaught Exception.

it just logs the error how it normally would

Because you're console logging the same error object!

Also don't use process.exit(). Because you might lose your logs. read more

Express docs explained how to do graceful shutdown, you can use similar approach.

  • Related