1

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.

Adithya Sama
  • 345
  • 3
  • 16

1 Answers1

2

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.
AZ_
  • 3,094
  • 1
  • 9
  • 19
  • please, bare with me, cos i am new to promises. So, you when say catch block is there for promises, what does that mean. The error is specifically about the unhandled promise isn't it. And also the if(err) part (Am assuming u meant the .query part) doesn't even get executed. the catch block does get executed, and yet the error is thrown. – Adithya Sama Jan 24 '19 at 09:24
  • I am not very much aware of npm pg. If so that's mean pool.query is simply a promisified function and does not accept a callback. so always catch block will be executed. In that case, you need to use then block for a successful response. The error you are facing is about running multiple connections, close some up n running ones. https://stackoverflow.com/questions/50947066/error-sorry-too-many-clients-already – AZ_ Jan 24 '19 at 09:36
  • The connections, i purposely exhausted. to check how to handle that situation. The error is fine. I was just looking for a safe way to handle it. I have used the then block, But that didn't get executed when there was error. And the catch block is not always executed, only when there was an error. – Adithya Sama Jan 24 '19 at 09:40
  • then block will executed only for successful responses and catch will get executed only when there's an error doing the query not to catch other issues, to catch connection issues you will have to use process.on('unhandledRejection'...) or if there's something in pg like pool.on('error', ()=> {}) – AZ_ Jan 24 '19 at 09:59
  • There is a pool.on("error", () => {}). But that doesn't catch the error. I have updated the code and result in the question. – Adithya Sama Jan 24 '19 at 10:05
  • please update the code with then block rather a callback. – AZ_ Jan 24 '19 at 10:13
  • I tried the then block, didn't make any difference. – Adithya Sama Jan 24 '19 at 10:18
  • I am not sure now, as it works for me, but try adding pool.end(); in catch block – AZ_ Jan 24 '19 at 10:39
  • I used pool.end(). Still not difference. But, is it working for you?, same code, no error thrown on console? – Adithya Sama Jan 24 '19 at 10:42
  • yes it does. is it the complete code? https://repl.it/@arizafar/IncomparablePungentJumpthreading – AZ_ Jan 24 '19 at 10:56
  • Maybe there is some nodejs setting, which is different in my setup, compared to the online one. I have to check. – Adithya Sama Jan 24 '19 at 11:03
  • I Couldn't find any environment changes. I even changed the error and threw the connection refused error same as in the online one. The catch block is executed yet the error is thrown onto the screen. – Adithya Sama Jan 29 '19 at 06:31