Handling Errors is overlooked time and again by inexperienced developers, but this has a significant impact on the stability, monitoring, and performance of the application. In this article, I will explain how to handle errors efficiently in your express application. There is no definite way to handle errors, but some are better than others. let’s dive into this. Handle Errors in Controller (Bad Approach): In this approach, we handle errors in the controllers. For every failed condition/exception, the error will be raised and returned by the controller itself in the response. For example: express = ( ); bodyParser = ( ); app = express(); port = ; app.use(bodyParser.json()); app.post( , (req, res) => { { title, author } = req.body; (!title || !author) { res.status( ).json({ : , : }); } { post = db.post.insert({ title, author }); res.json(post); } (error) { res.status( ).json({ : , : }); } }); app.listen(port, () => .log( ) ); const require 'express' const require 'body-parser' const const 3000 '/post' async const if return 400 status 'error' message 'Missing required fields: title or author' try const await catch return 500 status 'error' message 'Internal Server Error' console `app is listening at http://localhost: ` ${port} In this, we have a route /post that is adding a new Post in the database. The errors are handled explicitly at every condition and return a response. This kind of design has multiple caveats that can increase the system complexity. For example, if you want to track all the errors thrown by the system, you have to invoke that piece of code(tracker) everywhere, even multiple times in a single controller. 😑 Now that’s something, you had never want to do! This type of application architecture will hinder the scaling of the app and also it will be really hard to track the errors altogether. Note: How to Handle Errors in Your Application (Better Approach): In this approach, we use a common error handler for the complete app. Yes! all the errors are handled in the same place. To make the process more smooth, we will use npm library to generate errors with respective codes. @hapi/boom express = ( ); bodyParser = ( ); Boom = ( ); app = express(); port = ; app.use(bodyParser.json()); app.post( , (req, res, next) => { { title, author } = req.body; { (!title || !author) { Boom.badRequest() } post = db.post.insert({ title, author }); res.json(post); } (error) { next(error); } }); app.use( { { message = , isBoom, output } = error; (isBoom) { res.status(output.statusCode).json({ message, : , }); } res.status( ).json({ : , : , }); }); app.listen(port, () => .log( ) ); const require 'express' const require 'body-parser' const require '@hapi/boom' const const 3000 '/post' async const try if throw const await catch // error handler ( ) => error, req, res, _ const 'Oops! Something went wrong' if // if the error is explicitly thrown return success false // return generic error response for unexpected error return 500 success false message 'Oops! Something went wrong' console `app is listening at http://localhost: ` ${port} This might seem a bit complicated initially but it is way better than the preceding approach. In Express, we get in every Controller as the third parameter and we simply use that to pass any error generated to our error handler. next This way, all the errors will pass through this handler and you can easily track the errors generated and take actions to counter system failure. Also, this design will help you scale your application, you won’t need to return an error for every condition but simply pass any error raised to the error handler using next and the rest can be taken care of there. You can also integrate error monitoring tools like in the system to keep track of the critical errors that can lead to a system halt. Sentry Conclusion: The basic principle of designing systems should be to keep things as simple as possible which supports scaling too for the bigger picture. Checkout the boilerplate that I have created for Express, Mongoose. You are going to like it: https://github.com/dhruv/node I hope you have enjoyed this article and found it valuable. You can follow me on Twitter . Thanks for the support! Also published on: https://javascript.plainenglish.io/how-to-handle-errors-in-express-js-9fe2522ccc53
Share Your Thoughts