Adithya Sama
Adithya Sama

Reputation: 365

"UnhandledPromiseRejectionWarning" in nodejs even after adding catch block

I am writing an api server for a postgres database, and was testing some stress cases. I am using a pool from the module node-postgres, and ran into this problem.

I first exhausted all the postgers connections before starting the server. Then i try to run a query through the pool.

I tried adding try catch blocks around the promise. inside the promise (inside the catch block), nothing helped.

The following is some test code with which i reproduced the error.

var pg = require('./node_modules/pg')

var pool = new pg.Pool(
    {
        user: "test",
        password: "pass134",
        host: "localhost",
        database: "testdb",
        port: 5432,
        max: 32,
        min: 16,
        idleTimeoutMillis: 60000,
        connectionTimeoutMillis: 10000
    }
);

pool.on("error", function(err){

    console.log("Error1: " + String(err));

});

pool.query("SELECT COUNT(*) FROM acal_cust", [])
.then(function(err, results){

    console.log(err);
    console.log(results);

}).catch(function(err){

    console.log("Error2:" + String(err));

    pool.end();

});

This is what i get when i run the code.

Error2: error: sorry, too many clients already
(node:10422) UnhandledPromiseRejectionWarning: error: sorry, too many clients already
    at Connection.parseE (/ext/node_modules/pg/lib/connection.js:554:11)
    at Connection.parseMessage (/ext/node_modules/pg/lib/connection.js:381:17)
    at Socket.<anonymous> (/ext/node_modules/pg/lib/connection.js:117:22)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at addChunk (_stream_readable.js:263:12)
    at readableAddChunk (_stream_readable.js:250:11)
    at Socket.Readable.push (_stream_readable.js:208:10)
    at TCP.onread (net.js:597:20)
(node:10422) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:10422) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I would like to know how to handle the error properly. So that it's not thrown.

Upvotes: 1

Views: 2278

Answers (1)

arizafar
arizafar

Reputation: 3122

catch Undled rejections

process.on('unhandledRejection', error => {
  // Will print "unhandledRejection err is not defined"
  console.log('unhandledRejection', error.message);
});

catch block is there for promises not for callback version of async code. so if any error will be thrown it will be caught in if(err) {//do whatever} or

pool.query("SELECT COUNT(*) FROM acal_cust", []).then((results) => {

    console.log(results);

}).catch(function(err){

    console.log("Error:" + String(err));

});
and also close the pool when done with the query or any error.

Upvotes: 2

Related Questions