简体   繁体   中英

mssql - uncaughtException in Stream Mode

I'm facing the same issue as mentioned here :

When trying to use stream and query a table without mentioning the schema .

Something like select * from table instead of select * from schema_name.table . I get the following error: mssql uncaughtException RequestError: Invalid object name

I was able to log the error while listening to readStream.on('error') but it still throws an uncaughtException .

Software versions

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

Expected behavior:

Should be able to catch the uncaughtException

Actual behavior:

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)

Configuration:

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

Try adding a catch handler to connect and more important to the mssql object as mentioned in the 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')
})

Finally resolved the issue using const readableStream = request.toReadableStream();

Full code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM