【问题标题】:"UnhandledPromiseRejectionWarning" in nodejs even after adding catch block即使在添加catch块之后,nodejs中的“UnhandledPromiseRejectionWarning”
【发布时间】:2019-06-17 20:54:40
【问题描述】:

我正在为 postgres 数据库编写一个 api 服务器,并且正在测试一些压力情况。我正在使用模块 node-postgres 中的一个池,并遇到了这个问题。

在启动服务器之前,我首先用尽了所有 postgers 连接。然后我尝试通过池运行查询。

我尝试在 promise 周围添加 try catch 块。在 promise 内(在 catch 块内),没有任何帮助。

以下是我重现错误的一些测试代码。

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();

});

这是我运行代码时得到的。

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.

我想知道如何正确处理错误。这样它就不会被抛出。

【问题讨论】:

    标签: javascript postgresql error-handling promise node-pg-pool


    【解决方案1】:

    捕获未处理的拒绝

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

    catch 块用于承诺而不是异步代码的回调版本。 所以如果有任何错误被抛出,它将被捕获在 if(err) {//do 不管} 或

    pool.query("SELECT COUNT(*) FROM acal_cust", []).then((results) => {
    
        console.log(results);
    
    }).catch(function(err){
    
        console.log("Error:" + String(err));
    
    });
    并在完成查询或任何错误时关闭池。

    【讨论】:

    • 拜托,对我裸露,因为我是新来的承诺。所以,当你说 catch 块是为了承诺时,这是什么意思。该错误专门与未处理的承诺有关,不是吗。而且 if(err) 部分(我假设你的意思是 .query 部分)甚至没有被执行。 catch 块确实被执行了,但是错误被抛出了。
    • 我不太了解 npm pg。如果是这样,那就意味着 pool.query 只是一个承诺函数,不接受回调。所以总是会执行 catch 块。在这种情况下,您需要使用 then 阻止成功响应。您面临的错误是关于运行多个连接,关闭一些正在运行的连接。 stackoverflow.com/questions/50947066/…
    • 连接,我故意用尽了。检查如何处理这种情况。错误很好。我只是在寻找一种安全的方法来处理它。我使用了 then 块,但是当出现错误时没有执行。而且catch块并不总是执行,只有在出现错误时才会执行。
    • then block 将仅在成功响应时执行,而 catch 仅在查询出错时才会执行,而不是捕获其他问题,要捕获连接问题,您必须使用 process.on('unhandledRejection '...) 或者 pg 中有类似 pool.on('error', ()=> {}) 的东西
    • 有一个 pool.on("error", () => {})。但这并没有发现错误。我已经更新了代码并导致了问题。
    猜你喜欢
    • 1970-01-01
    • 2017-05-05
    • 2019-07-03
    • 1970-01-01
    • 2013-01-26
    • 2021-11-27
    • 2018-08-25
    • 2015-06-21
    • 1970-01-01
    相关资源
    最近更新 更多