mssql - 流模式下的 uncaughtException

mssql - uncaughtException in Stream Mode

我遇到了与上述相同的问题here

当尝试使用流并查询 table 而未提及 schema.

类似于 select * from table 而不是 select * from schema_name.table。我收到以下错误: mssql uncaughtException RequestError: Invalid object name

我能够在收听 readStream.on('error') 时记录错误,但它仍然抛出 uncaughtException

软件版本

"mssql": "^7.2.1",
"nodejs":  "12.15.0"

预期行为:

应该能够捕捉到 uncaughtException

实际行为:

INFO: waaaaaa1
uncaughtException RequestError: Invalid object name 'employee'.
at handleError (.../node_modules/mssql/lib/tedious/request.js:388:15)
at Connection.emit (events.js:223:5)
at Connection.emit (.../node_modules/tedious/lib/connection.js:1071:18)
at Parser. (.../node_modules/tedious/lib/connection.js:1176:12)
at Parser.emit (events.js:223:5)
at Readable. (.../node_modules/tedious/lib/token/token-stream-parser.js:27:14)

配置:

    import mssql from 'mssql';
       try {
    mssql.connect({
      server,
      port: Number.parseInt(port),
      database,
      user,
      password,
      options: {
        encrypt: true, // for azure
        trustServerCertificate: trustServerCertificate ? true : false // change to true for local dev / self-signed certs
      }
    })
      .then(async client => {
        const readStream = client.request();
        readStream.stream = true;
        const passThroughStream = new stream.PassThrough();
        const writeStream = fs.createWriteStream(path.join(__dirname, './writeMe.json'));
      
        readStream.on('error', () => log.info('waaaaaa1'));
        passThroughStream.on('error', () => log.info('waaaaaa2'));
        writeStream.on('error', () => log.info('waaaaaa3'));

        readStream.pipe(passThroughStream).pipe(writeStream);
        readStream.query(query);
      })
      // the promise error handler
      .catch(() => {
        log.info('waaaaaa4');
      });

    // the main sql error handler:
    mssql.on('error', error => {
      log.error('SQL error1', error);
    });

    // the main sql error handler:
    client.on('error', error => {
      log.error('SQL error2', error);
    });


  }
  catch (err) {
    log.info('catchhhh meee', err);
  }
  finally {
    if(client){
      client.close();
    }
  }

尝试向 connect 添加一个捕获处理程序,并且对 mssql 对象更重要,如 docs

中所述
mssql.connect({
   // ...
    
 })
.then(() => {
    return readStream.query(query);
})

// the promise error handler
.catch(() => {
    
})

// the main sql error handler:
mssql.on('error', error => {
    console.error('SQL error')
})

最终使用 const readableStream = request.toReadableStream();

解决了问题

完整代码:

const request = client.request();
const readableStream = request.toReadableStream();
const writeStream = fs.createWriteStream(path.join(__dirname, './writeMe.json'));
const passThroughStream = new stream.PassThrough();
readableStream.on('end', () => passThroughStream.end());
readableStream.on('error', (e) => log.info('waaaaaa1'));
readableStream.pipe(passThroughStream).pipe(writeStream);
request.query(query);