Handle unhandledRejection and uncaughtException in express node js

Some time it is difficult to handle all error or exceptions. Even then we need to capture these error or exception for debug purpose or to show proper message to End user.

Node js give us flexibility to capture to unhandled error or exception with the following way.

unhandledRejection

//For asyncrohous code unhandle error
process.on('unhandledRejection', err => {
    console.log(err.name, err.message);
});

uncaughtException

// for syncronous code unhandle error
process.on('uncaughtException', err => {
    console.log(err.name, err.message);
});

Keep above piece of code before initialization of express. means before const app = express();

Leave a Reply