Sign in
Log inSign up

How to handle errors in a node web app?

Hugo Mota's photo
Hugo Mota
·Aug 28, 2016

I'm coming from a python background. In python, it's common to throw an exception when something goes wrong. If you know how to handle the problem, you can recover from it. If not, the execution is aborted.

In web, this means that if the request presents a failure, like a database query gone wrong for instance, the exception will bubble up to some higher up code that may return an error 500 to the users and log the error (in sentry, or directly to email, etc). The bottomline is: (almost) no error gets unreported.

In backend with node, however, I've noticed it's a pattern to pass the error as a callback argument. This effectively makes the programmer responsible to take action every time, which I consider error prone. Just playing with it a little got me frustrated to see messy code running without doing anything, not even reporting an error.

Of course we should handle every error, but we are human beings, right? What if I forget some? How can I make sure that this error I forgot will make it's way into my email box, so I can fix it?