How to improve error handling in express

Asad khan
4 min readOct 31, 2020
Image by Luis Gomes from pexels

In this blog we will discuss how to improve error handling in express so I am assuming that you have some basic knowledge of express and wants to improve your error handling in express so lets get started

In this blog we have restful API with two routes through which a user can create a Account and can get his/her account. Code given below

This is how our user routes will look like but the thing is that we are repeating ourselves in each route catch block as we are sending a 500 status code if something went wrong and lets suppose we are using a logger like winston to log errors.Every route is going to have same catch block where we will log errors and will send a 500 status code to user so how can we improve our code quality? The solution to this is to define a middleware which will take 4 arguments(err,req,res,next) in which we will pass a error object as first argument.Now lets define our errorMiddleware which will look like

Now our middleware is defined but keep one thing in mind and that is our errorMiddleware must return a response or will call next middleware if not then our request will hang and our user will not get any response so now lets modify our routes so they will pass err object to errorMiddleware by calling next(err) which will call next middleware in the request-response pipeline.Lets refactor our routes which will look like

As we refactored our routes we are suppose to pass errorMiddleware as argument so that it will registered as middleware in our app request-response pipeline.

We achieved what we wanted but once again we are repeating ourselves by wrapping our code with try and catch for every route isn’t it against DRY Rule.

As we can see in the screenshot given below

As we can see we are definitely breaking DRY rule because we are repeating try and catch block for every route so how can we improve our code quality? The solution to this is to define a async middleware.lets define async middleware which will look like

First lets understand how express passes req and res objects to controller when controller is invoked express passes req and res object to controller of our route.The reason i defined a function which will take route controller as argument and will return a modified controller where we wrap our controller with try and catch blocks so when modified controller is invoked express will pass req and res objects to our modified controller and we will pass req and res objects to our controller so that we can access it in our controller. After defining our asyncMiddleware we will modify our routes which look like

we will wrap our route controllers with asyncMiddleware and will remove try,catch blocks from our route controllers so this is it but what if we don’t want to write our custom asyncMiddleware and don’t want to wrap every route controller with asyncMidlleware. We can use a npm package called express-async-errors.It will wrap our route controllers at runtime so now lets install it throught npm install express-async-errors so now lets refractor our routes and import express-async-errors it in our index file.

So Now our Code is looking way more cleaner than it was.

--

--