How do you handle uncaught exceptions and promise rejections in Express js

0 votes

How do you handle uncaught exceptions and promise rejections in Express.js?

I'm using Express.js and facing issues with uncaught exceptions and unhandled promise rejections crashing my server. How can I handle these errors gracefully in an Express app? Also, what are the best practices for logging and monitoring these errors in a production environment?

Dec 6, 2024 in Angular by Nidhi
• 16,260 points
• 1,277 views

1 answer to this question.

0 votes

Handling uncaught exceptions and unhandled promise rejections within an Express.js application will be essential in developing highly robust and resilient systems. Here's how you should handle these situations:

Handling Uncaught Exceptions
Uncaught exceptions occur if an error is not caught within the application's code. This can crash the Node.js process if unhandled.


Example:

process.on('uncaughtException', (err) => {
console.error('Uncaught Exception:', err.message);
console.error(err.stack);
// Optionally, you can do cleanup here
process.exit(1); // Exit the process after logging the error
});

When to Use: Use this handler to log critical errors and shut down the server gracefully. Do not use it for normal error handling.

Handle Unhandled Promise Rejections

Unhandled promise rejections happen when a Promise is rejected and no.catch() or try.catch is used to handle the rejection.


Example:

process.on('unhandledRejection', (reason, promise) => {
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
// Optionally, you can perform cleanup tasks here
});

 Using Middleware to Handle Errors

Express.js has an inbuilt system for centralizing error handling using middleware.


Example

// Set up an error-handling middleware
app.use((err, req, res, next) => {
console.error('Error:', err.message);
res.status(err.status || 500).json({
error: {
message: err.message,
}
});
});


Best Practice: Place this middleware after all other routes to catch errors that occur in route handlers.

answered Dec 6, 2024 by Navya

Related Questions In Angular

0 votes
1 answer

How do you import a javascript package from a cdn/script tag in React?

Hello, Go to the index.html file and import ...READ MORE

answered Jun 4, 2020 in Angular by Niroj
• 82,800 points
• 26,766 views
0 votes
1 answer

How do you bind an Enum to a DropDownList control in ASP.NET?

Hello @kartik, I probably wouldn't bind the data as it's ...READ MORE

answered Jul 23, 2020 in Angular by Niroj
• 82,800 points
• 4,110 views
0 votes
1 answer

How do I include a JavaScript script file in Angular and call a function from that script?

Hello @kartik, Refer the scripts inside the angular-cli.json (angular.json when using ...READ MORE

answered Sep 8, 2020 in Angular by Niroj
• 82,800 points
• 15,686 views
0 votes
1 answer

How can you log and inspect request and response headers in Angular?

In Angular, you can log request and ...READ MORE

answered Feb 25, 2025 in Angular by Navya
• 1,082 views
0 votes
1 answer

Unable to start express server on AWS instance

It's not your code — you can't connect ...READ MORE

answered Oct 1, 2018 in AWS by Priyaj
• 58,020 points
• 4,564 views
0 votes
1 answer

Start script missing error when running npm start

It seems that there is an undefined ...READ MORE

answered Feb 10, 2022 in Java by Soham
• 9,730 points
• 6,462 views
0 votes
0 answers

Pre-rendering VS Server-side rendering for Angular SEO

i want to integrate an seo optimization ...READ MORE

Feb 14, 2022 in Others by Kichu
• 19,040 points
• 1,469 views
0 votes
1 answer

Pre-rendering VS Server-side rendering for Angular SEO

https://developers.google.com/web/updates/2019/02/rendering-on-the-web use this article it explains all about ...READ MORE

answered Feb 22, 2022 in Others by narikkadan
• 86,360 points
• 1,699 views
0 votes
0 answers

How do you deal with errors in Observables?

With the help of an example, can ...READ MORE

Mar 3, 2025 in Angular by Nidhi
• 16,260 points
• 861 views
0 votes
0 answers

How do you apply transform functions in PipeTransform Interface?

i want know with the help of ...READ MORE

Mar 5, 2025 in Angular by Nidhi
• 16,260 points
• 932 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP