0

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();
    }
  }
user846445
  • 221
  • 1
  • 3
  • 14
  • const PromiseFtp = require('promise-ftp') is your problem... bad library, you could try sftp-promises. – BGPHiJACK Jun 11 '21 at 11:04
  • @blanknamefornow: my ftp server supports only ftps. ftps dfifferent from sftp right? – user846445 Jun 11 '21 at 11:09
  • Either or, the s just means Secure; so it's known as Secure File Transfer Protocol. I'd hope too that no one was using regular FTP. But I'd give a different module a shot, sometimes the coders do a bad job on promisify the stuff.. it's horrid. – BGPHiJACK Jun 11 '21 at 11:11
  • Are you using Secure Protocol or not? promise-ftp is not that of the promise-sftp. Which may explain why no opening of the connection? – BGPHiJACK Jun 11 '21 at 11:15
  • @blanknamefornow: am using promise-ftp to make ftps connection. The above error am getting intermittently not all the time. out of 10 times, 2 times it works and remaining 8 times the above error occurs. – user846445 Jun 11 '21 at 11:17
  • is events.js:291 your file? Or the modules, would maybe be an author error if it's happening randomly, code looks fine fast glimpse. Thus was why I said maybe switch modules incase it's outdated or has these unexpected issues. – BGPHiJACK Jun 11 '21 at 11:27
  • @blanknamefornow: no events.js is not my file. its the modules. i tried with node-ftp same issue. I am not sure from where it comes. – user846445 Jun 11 '21 at 11:29
  • Are you maybe opening FTP too much and it's timing you out? Active development and all. Might need to think "server settings" but needs tracing to be sure, I just know mine does that natively fresh out of setup so goal is to connect and leave it connected for minutes to hours to not meet this timeout? – BGPHiJACK Jun 11 '21 at 11:37
  • `stream.once('error', reject("File problem"))` needs to be `stream.once('error', reject)` - you're always immediately rejecting your promise with a string. Also you should not `throw` from that first `error` event handler - probably you don't need that at all. – Bergi Jun 11 '21 at 21:59
  • @Bergi: can you be little clearer. Sorry am not getting you.So you are suggesting to change stream.once('error', reject("File Problem") to stream.once('error', reject). And if we should not throw error from first error. What would be the handler in that case? – user846445 Jun 12 '21 at 01:45
  • Yes, that's what I'm suggesting. And you should omit the whole `stream.on('error', function(err) {…})`. The handler would be the `reject` function, which causes the awaited promise to reject and your `catch` block to run. – Bergi Jun 12 '21 at 01:52
  • @bergi done as per your suggestion. But still getting the error. I am not sure from where its firing events.js:291 throw er; // Unhandled 'error' event ^ Error: Cannot open data connection. at makeError (\ftps\reconciliation-batch\node_modules\@icetee\ftp\lib\connection.js:1128:13) – user846445 Jun 12 '21 at 11:23

0 Answers0