Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

process.on('unhandledRejection') is not logging error

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading