mscdex / node-ftp

An FTP client module for node.js
MIT License
1.13k stars 243 forks source link

Error on input file - Unhandled error event #143

Open jayant-simpragma opened 8 years ago

jayant-simpragma commented 8 years ago

I get the following error occasionally when the file starts to download from the FTP server. This error stops the whole process and I usually have to trigger it again (and the file downloads without any problem). It doesn't reach the error handler as well.

node-ftp version: 0.3.10

events.js:154
throw er; // Unhandled 'error' event

Error on input file.
    at makeError (/app/node_modules/ftp/lib/connection.js:1067:13)
    at Parser.<anonymous> (/app/node_modules/ftp/lib/connection.js:113:25)
    at emitTwo (events.js:100:13)
    at Parser.emit (events.js:185:7)
    at Parser._write (/app/node_modules/ftp/lib/parser.js:59:10)
    at doWrite (_stream_writable.js:292:12)
    at writeOrBuffer (_stream_writable.js:278:5)
    at Parser.Writable.write (_stream_writable.js:207:11)
    at TLSSocket.ondata (/app/node_modules/ftp/lib/connection.js:273:20)
    at emitOne (events.js:90:13)

I would like to know the cause of this occasional error and the way to catch/handle it in the code.

jairamaswamy commented 8 years ago

Can you provide the sample code here?

varun-verma commented 8 years ago

I am getting the same error. Here is my code:

var Client = require('ftp');
var fs = require('fs');

var fileName = 'file.pdf';
var fileLocation = '/myFiles';

var ftpClient = new Client();

var options = {
    host: 'host',
    user: 'user',
    password: 'password',
    connTimeout: 15000,
    secure: true,
    secureOptions: {
        rejectUnauthorized: false
    }
};

ftpClient.connect(options);
console.log('Connecting to ftp server ...');

ftpClient.on('ready', function() {
    console.log('Successfully connected to FTP server');
    console.log('----------------------------------------------------------------------------------------');
    getFile();
});

ftpClient.on('error', function(error) {
    console.log('Unable to connect to FTP server :', error);
});

ftpClient.on('end', function() {
    console.log('Connection closed');
});

function getFile() {
    ftpClient.get(fileLocation + '/' + fileName, function(err, stream) {
        if (err) {
            console.log('Error while getting the file :', err)
        } else {
            stream.pipe(fs.createWriteStream('output.pdf'));
            stream.once('close', function() {
                console.log('Stream ended');
            });
        }
    });
}