Two Error Handling Methods for Asynchronous Code in Express.js | by Sifeddine Lamraoui | Jul, 2022

Develop this essential talent proper from the beginning

Error dealing with is likely one of the most essential components of software program improvement, but it usually doesn’t get the eye it deserves, particularly for brand new builders. So we might be two completely different strategies of error dealing with when working with Specific.js, which is the most well-liked JavaScript server-side framework.

Since Specific.js takes care of error dealing with for synchronous code by default, we are going to solely cowl asynchronous code, for instance, if we name this route that makes use of a synchronous handler operate.

Specific’s default built-in error handler takes care of the error, and our app continues to run.

However, if we run the identical route with an async/await handler operate, right here’s what we get:

And our app crashes and exits.

And so, let’s get began.

Middleware features in Specific.js are features that get executed after the server receives the request and earlier than the response is distributed to the consumer. They’re triggered sequentially from prime to backside, they usually transfer in that order by calling one another with the subsequent() operate.

So if we create a middleware on the backside of our routes, it will possibly act as the purpose the place we deal with all of our errors by calling it at any time when an error happens.

Now, let’s create this middleware and put it on the backside of our routes.

And let’s change the final instance to incorporate a attempt/catch block after which name that middleware when an error happens.

Our app now seems to be like this:

Now, if we run that route, we obtain the message despatched by the error handler middleware, and our app continues to run.

You may as well customise the error object you ship to the error handler by modifying it earlier than sending it with the subsequent() operate. So, for instance, if we need to make the standing code, we reply with adjustments relying on the place the error got here from we are able to intercept the error first, like this:

When utilizing the earlier methodology to deal with our errors, there’s one inconvenience which is having to make use of a attempt/catch block on all of our routes. So on this methodology, we are able to keep away from that by making a wrapper operate that incorporates that attempt/catch block, and inside it calls the handler operate we give to it. Then we simply wrap all of our route handlers inside this operate, that means we attempt/catch solely as soon as and have a centralized place the place we deal with our errors.

So, let’s create our wrapper operate and title it handleCall. Right here’s the code:

As you’ll be able to see, the wrapper operate returns the handler that can get handed to the route, and inside it, we name our precise handler that incorporates our logic.

And so now our app seems to be like this:

As you’ll be able to see, we wrapped the handler operate inside handleCall and we didn’t have to make use of any attempt/catch block. We additionally despatched a personalized error object just like the earlier instance. Now, if we name that route, we obtain the identical response because the earlier methodology, and our app additionally continues to run.

That’s it. We explored two strategies the place we are able to deal with errors on Specific.js with asynchronous code, and you may select whichever methodology you are feeling extra snug with.

More Posts