尽管有多个try / catch块,但仍未处理PromiseRejectionWarning

时间:2019-01-04 23:20:07

标签: javascript node.js async-await try-catch es6-promise

在使用mssql的Node模块时,在测试错误时遇到了奇怪的错误。下面的示例代码运行良好,连接到数据库,然后查询当前用户并在循环中等待一秒钟。如果在循环之前没有连接 ,则错误处理有效,循环开始并产生console.error

2019-01-04T23:08:43.537Z 'Failed to connect to RNOD-EMSSQL:1433 - getaddrinfo ENOTFOUND RNOD-EMSSQL'

但是,如果连接被终止(我只是在Windows中只是关闭了适配器),则在 循环开始后,错误不会被捕获并产生:

(node:347996) UnhandledPromiseRejectionWarning: ConnectionError: Failed to connect to RNOD-EMSSQL:1433 - getaddrinfo ENOTFOUND RNOD-EMSSQL
    at Connection.tedious.once.err (C:\Users\drodriguez\Desktop\POCs\mssql-issue\node_modules\mssql\lib\tedious.js:239:17)
    at Object.onceWrapper (events.js:273:13)
    at Connection.emit (events.js:182:13)
    at Connection.socketError (C:\Users\drodriguez\Desktop\POCs\mssql-issue\node_modules\tedious\lib\connection.js:1024:14)
    at C:\Users\drodriguez\Desktop\POCs\mssql-issue\node_modules\tedious\lib\connection.js:868:25
    at GetAddrInfoReqWrap.callback (C:\Users\drodriguez\Desktop\POCs\mssql-issue\node_modules\tedious\lib\connector.js:69:18)
    at GetAddrInfoReqWrap.onlookupall [as oncomplete] (dns.js:69:17)
(node:347996) 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: 2)
(node:347996) [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.

我不知所措,为什么会这样,为什么没有被承诺链上两个不同的try/catch块和一个.catch抓住。

const mssql = require('mssql');
const config = require('./config');

const test = async () => {
    // Reference to the connection
    let connection;

    // Flag to allow the do/while loop to run
    let run = true;

    try {
        // Establish a connection to the database
        connection = await mssql.connect(config.database);

        // Start the loop
        do {
            try {
                // Run a simple query to get the current user
                const results = await connection.request().query('SELECT CURRENT_USER AS [Name]');

                // Log it out to the console with the current date/time
                console.log(new Date(), results.recordset[0]);

                // Wait one second before restarting the loop
                await new Promise(resolve => setTimeout(resolve, 1000));
            } catch (error) {
                // If there is an error log it
                console.log(new Date(), error.message);

                // Break the loop
                run = false;
            }
        } while (run);
    } catch (error) {
        // If there is an error log it
        console.log(new Date(), error.message);
    } finally {
        // If a connection was established, close it out
        if (connection !== undefined) {
            connection.close();
        }
    }
}

// Run the test method
test()
.catch((error) => {
    // If there is an error log it
    console.log(new Date(), error.message);
})

编辑:从脚本中删除了new关键字

0 个答案:

没有答案