Is there a clear way for handling nodejs ftp errors

277 Views Asked by At

I am using the following code to connect to ftp and download a file. But randomly it throws the following error and am not able to catch it in the catch block. I am not sure from where its throwing. Need your help here.

Output:

start ftsp client...

start ftsp client 1...

start ftsp client 2...

events.js:291

throw er; // Unhandled 'error' event ^

Error: Cannot open data connection.

Program

const PromiseFtp = require('promise-ftp')
 async  streamFileFromFtpToGcs(ftpsName, GcsBucket){
    const ftp = new PromiseFtp();
    try{
      console.log('start ftsp client 1...')
      await ftp.connect(ftpsConfig)
      console.log('start ftsp client 2...')
      const stream = await ftp.get('/03531/' + ftpsName)
      stream.on('error', function(err) {
        console.log('Error in')
        throw new Error('Error reading the file')
      })
      stream.on('data', function(err){
        console.log('Error data')
      })
      await new Promise((resolve, reject) =>{
        stream.once('close', resolve)
        stream.once('error', reject("File problem"))
        stream.pipe(GcsBucket.file('input/' + ftpsName).createWriteStream({
          destination: 'input/' + ftpsName
        }))
      })

    }catch(error) {
      console.log("error in ",error)
       throw new Error('Something wring with ftp', error)
    } finally{
      console.log("Ending")
      await ftp.end();
    }
  }
0

There are 0 best solutions below