Node.js error handling through each layer

by spin_round_22   Last Updated October 19, 2019 19:05 PM - source

What is the best way to handle errors in a Node.js application. This might actually apply to any language, but I still don't understand how to do it correctly. The questions are always who handles the errors, does it make sense to continually throw to the top, and who logs what?

Let's take a simple example of a user system where you can retrieve users from a database and "log in" if credentials match.

Database layer

// connects to a mongo db 
class UserDatabase(){
   async getUser(filter){
       // this can throw if connection is dead
       let user = await this.collections.users.findOne(filter);
       return user;

Application layer

// makes calls to the db class to "login" a user
class UserApp {
    async loginUser(email, password){
       let user = await this.db.getUser({email});
           return false;
       if(user.password === options.password){
           return true;

REST layer

// calls the API from REST calls
class UserRestApi {
    async loginUser(request, response){
        let result = await, request.body.password);
            response.status = 200;
        else {
            response.status = 401;

At this moment there is 0 error handling. We can fail almost everywhere with uncaught exceptions. But is there ever any point in catching at the lowest level? Suppose getUser() fails because the mongodb connection dies. I can catch it and log it there, but then do I throw it again, and again, and again, and log it again, and again, and again? Does that make sense? I would end up in this case with 2-3 repeated logs.

I'm just not sure how to approach this.

Related Questions

Handling Errors while using the Flux Pattern

Updated April 08, 2015 00:02 AM

try-catch in javascript... isn't it a good practice?

Updated January 16, 2018 19:05 PM