mscdex / node-ftp

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

better error handling? #135

Closed kenichi-shibata closed 8 years ago

kenichi-shibata commented 8 years ago

I am getting this error cb(undefined, RE_WD.exec(text)[1]); I have no idea what it means.

kenichi-shibata commented 8 years ago

TypeError: Cannot read property '1' of null

kenichi-shibata commented 8 years ago

This happens when I try to use async.eachLimit to do ftp connection then end it to make another one

mscdex commented 8 years ago

What does text contain if you log it?

And/Or what if you enable logging by passing debug: console.log in the connection config.

kenichi-shibata commented 8 years ago

It works if i have only one file but if i use async to upload multiple files this error always appears i tried using c.destroy instead of end. To avoid concurrent connection but its still the same

kenichi-shibata commented 8 years ago

I use the same code but there is only 1 file to be uploaded here is the response

[connection] < '220 (vsFTPd 2.0.5)\r\n'
[parser] < '220 (vsFTPd 2.0.5)\r\n'
[parser] Response: code=220, buffer='(vsFTPd 2.0.5)'
[connection] > 'USER myusername'
[connection] < '331 Please specify the password.\r\n'
[parser] < '331 Please specify the password.\r\n'
[parser] Response: code=331, buffer='Please specify the password.'
[connection] > 'PASS mypassword'
[connection] < '230 Login successful.\r\n'
[parser] < '230 Login successful.\r\n'
[parser] Response: code=230, buffer='Login successful.'
[connection] > 'FEAT'
[connection] < '211-Features:\r\n'
[connection] < ' EPRT\r\n'
[connection] < ' EPSV\r\n MDTM\r\n PASV\r\n REST STREAM\r\n SIZE\r\n TVFS\r\n UTF8\r\n211 End\r\n'
[parser] < '211-Features:\r\n EPRT\r\n EPSV\r\n MDTM\r\n PASV\r\n REST STREAM\r\n SIZE\r\n TVFS\r\n UTF8\r\n211 End\r\n'
[parser] Response: code=211, buffer='Features:\r\n EPRT\r\n EPSV\r\n MDTM\r\n PASV\r\n REST STREAM\r\n SIZE\r\n TVFS\r\n UTF8\r\nEnd'
[connection] > 'TYPE I'
[connection] < '200 Switching to Binary mode.\r\n'
[parser] < '200 Switching to Binary mode.\r\n'
[parser] Response: code=200, buffer='Switching to Binary mode.'
[connection] > 'PWD'
[connection] < '257 "/"\r\n'
[parser] < '257 "/"\r\n'
[parser] Response: code=257, buffer='"/"'
[connection] > 'CWD /'
[connection] < '250 Directory successfully changed.\r\n'
[parser] < '250 Directory successfully changed.\r\n'
[parser] Response: code=250, buffer='Directory successfully changed.'
[connection] > 'CWD test'
[connection] < '250 Directory successfully changed.\r\n'
[parser] < '250 Directory successfully changed.\r\n'
[parser] Response: code=250, buffer='Directory successfully changed.'
[connection] > 'CWD directory'
[connection] < '250 Directory successfully changed.\r\n'
[parser] < '250 Directory successfully changed.\r\n'
[parser] Response: code=250, buffer='Directory successfully changed.'
[connection] > 'CWD forimage'
[connection] < '250 Directory successfully changed.\r\n'
[parser] < '250 Directory successfully changed.\r\n'
[parser] Response: code=250, buffer='Directory successfully changed.'
[connection] > 'CWD /'
[connection] < '250 Directory successfully changed.\r\n'
[parser] < '250 Directory successfully changed.\r\n'
[parser] Response: code=250, buffer='Directory successfully changed.'
[connection] > 'PASV'
[connection] < '227 Entering Passive Mode (63,140,45,148,168,132)\r\n'
[parser] < '227 Entering Passive Mode (63,140,45,148,168,132)\r\n'
[parser] Response: code=227, buffer='Entering Passive Mode (63,140,45,148,168,132)'
[connection] PASV socket connected
[connection] > 'STOR /test/directory/forimage/logo.jpg'
[connection] < '150 Ok to send data.\r\n'
[parser] < '150 Ok to send data.\r\n'
[parser] Response: code=150, buffer='Ok to send data.'
[connection] < '226 File receive OK.\r\n'
[parser] < '226 File receive OK.\r\n'
[parser] Response: code=226, buffer='File receive OK.'
====================================
successfully uploaded file to /test/directory/forimage/logo.jpg
====================================
[connection] < '500 OOPS: vsf_sysutil_recv_peek: no data\r\n500 OOPS: child died\r\n'

this works

kenichi-shibata commented 8 years ago

here is my code

module.exports = function ftpUpload(server,file,pathToRemote){
try{
  var LOCAL_FILE = file;
  var REMOTE_FILE = file;
  var REMOTE_DIRECTORY = pathToRemote;
  var LOCAL_DIRECTORY = './test';

  log.info('uploadingers ftp: ',file);

  var connectionProperties = {
      host    : server.HOST,
      user    : server.USER,
      // port    : serve.PORT,
      password: server.PASSWORD,
      debug:  console.log

      // ,
      // secure: true,
      // secureOptions: {
      //    ca: [ fs.readFileSync('resources/somekey.pem') ]
      // }
  };

  c.on('ready', function() {
    c.mkdir(REMOTE_DIRECTORY, true, function(err) {
    if (err) throw err;
    });
    c.put('test/'+LOCAL_FILE,REMOTE_DIRECTORY+REMOTE_FILE, function(err) {
      if (err) throw err;
      console.log('====================================');
      console.log('successfully uploaded file to '+REMOTE_DIRECTORY+REMOTE_FILE);
      console.log('====================================');
      c.end();
    });
  });

  c.connect(connectionProperties);
  }

  catch(err){
    log.error(err);
  }
};
mscdex commented 8 years ago

For debugging purposes since you have multiple connections in parallel, you might at least temporarily assign some sort of unique ID that is included in the debug output so we can see what exactly is happening for each connection. For example:

var conn_id = 0;
module.exports = function ftpUpload(server,file,pathToRemote){
  // ...
  var id = conn_id++;
  var connectionProperties = {
    // ...
    debug: function(msg) {
      console.log('[%d] %s', id, msg);
    }
  };
  // ...
};

Then re-post the logged output

kenichi-shibata commented 8 years ago

here is with 1 file

[0] [connection] < '220 (vsFTPd 2.0.5)\r\n'
[0] [parser] < '220 (vsFTPd 2.0.5)\r\n'
[0] [parser] Response: code=220, buffer='(vsFTPd 2.0.5)'
[0] [connection] > 'USER myusername'
[0] [connection] < '331 Please specify the password.\r\n'
[0] [parser] < '331 Please specify the password.\r\n'
[0] [parser] Response: code=331, buffer='Please specify the password.'
[0] [connection] > 'PASS mypassword'
[0] [connection] < '230 Login successful.\r\n'
[0] [parser] < '230 Login successful.\r\n'
[0] [parser] Response: code=230, buffer='Login successful.'
[0] [connection] > 'FEAT'
[0] [connection] < '211-Features:\r\n EPRT\r\n EPSV\r\n'
[0] [connection] < ' MDTM\r\n PASV\r\n REST STREAM\r\n SIZE\r\n TVFS\r\n UTF8\r\n211 End\r\n'
[0] [parser] < '211-Features:\r\n EPRT\r\n EPSV\r\n MDTM\r\n PASV\r\n REST STREAM\r\n SIZE\r\n TVFS\r\n UTF8\r\n211 End\r\n'
[0] [parser] Response: code=211, buffer='Features:\r\n EPRT\r\n EPSV\r\n MDTM\r\n PASV\r\n REST STREAM\r\n SIZE\r\n TVFS\r\n UTF8\r\nEnd'
[0] [connection] > 'TYPE I'
[0] [connection] < '200 Switching to Binary mode.\r\n'
[0] [parser] < '200 Switching to Binary mode.\r\n'
[0] [parser] Response: code=200, buffer='Switching to Binary mode.'
[0] [connection] > 'PWD'
[0] [connection] < '257 "/"\r\n'
[0] [parser] < '257 "/"\r\n'
[0] [parser] Response: code=257, buffer='"/"'
[0] [connection] > 'CWD /'
[0] [connection] < '250 Directory successfully changed.\r\n'
[0] [parser] < '250 Directory successfully changed.\r\n'
[0] [parser] Response: code=250, buffer='Directory successfully changed.'
[0] [connection] > 'CWD test'
[0] [connection] < '250 Directory successfully changed.\r\n'
[0] [parser] < '250 Directory successfully changed.\r\n'
[0] [parser] Response: code=250, buffer='Directory successfully changed.'
[0] [connection] > 'CWD directory'
[0] [connection] < '250 Directory successfully changed.\r\n'
[0] [parser] < '250 Directory successfully changed.\r\n'
[0] [parser] Response: code=250, buffer='Directory successfully changed.'
[0] [connection] > 'CWD forimage'
[0] [connection] < '250 Directory successfully changed.\r\n'
[0] [parser] < '250 Directory successfully changed.\r\n'
[0] [parser] Response: code=250, buffer='Directory successfully changed.'
[0] [connection] > 'CWD /'
[0] [connection] < '250 Directory successfully changed.\r\n'
[0] [parser] < '250 Directory successfully changed.\r\n'
[0] [parser] Response: code=250, buffer='Directory successfully changed.'
[0] [connection] > 'PASV'
[0] [connection] < '227 Entering Passive Mode (63,140,45,148,184,98)\r\n'
[0] [parser] < '227 Entering Passive Mode (63,140,45,148,184,98)\r\n'
[0] [parser] Response: code=227, buffer='Entering Passive Mode (63,140,45,148,184,98)'
[0] [connection] PASV socket connected
[0] [connection] > 'STOR /test/directory/forimage/UNIQLO_Global_Logo.jpg'
[0] [connection] < '150 Ok to send data.\r\n'
[0] [parser] < '150 Ok to send data.\r\n'
[0] [parser] Response: code=150, buffer='Ok to send data.'
[0] [connection] < '226 File receive OK.\r\n'
[0] [parser] < '226 File receive OK.\r\n'
[0] [parser] Response: code=226, buffer='File receive OK.'
====================================
successfully uploaded file to /test/directory/forimage/logo.jpg
====================================
[0] [connection] < '500 OOPS: vsf_sysutil_recv_peek: no data\r\n'
[0] [connection] < '500 OOPS: child died\r\n'
kenichi-shibata commented 8 years ago

here is with 2 files

[0] [connection] < '220 (vsFTPd 2.0.5)\r\n'
[1] [parser] < '220 (vsFTPd 2.0.5)\r\n'
[1] [parser] Response: code=220, buffer='(vsFTPd 2.0.5)'
[1] [connection] > 'USER myusername'
[1] [connection] < '220 (vsFTPd 2.0.5)\r\n'
[1] [parser] < '220 (vsFTPd 2.0.5)\r\n'
[1] [parser] Response: code=220, buffer='(vsFTPd 2.0.5)'
[1] [connection] > 'PASS mypassword'
[1] [connection] < '331 Please specify the password.\r\n'
[1] [parser] < '331 Please specify the password.\r\n'
[1] [parser] Response: code=331, buffer='Please specify the password.'
[1] [connection] > 'FEAT'
[1] [connection] < '230 Login successful.\r\n'
[1] [parser] < '230 Login successful.\r\n'
[1] [parser] Response: code=230, buffer='Login successful.'
[1] [connection] > 'TYPE I'
[1] [connection] < '211-Features:\r\n EPRT\r\n'
[1] [connection] < ' EPSV\r\n MDTM\r\n PASV\r\n REST STREAM\r\n SIZE\r\n TVFS\r\n UTF8\r\n211 End\r\n'
[1] [parser] < '211-Features:\r\n EPRT\r\n EPSV\r\n MDTM\r\n PASV\r\n REST STREAM\r\n SIZE\r\n TVFS\r\n UTF8\r\n211 End\r\n'
[1] [parser] Response: code=211, buffer='Features:\r\n EPRT\r\n EPSV\r\n MDTM\r\n PASV\r\n REST STREAM\r\n SIZE\r\n TVFS\r\n UTF8\r\nEnd'
[1] [connection] > 'PWD'
[1] [connection] < '200 Switching to Binary mode.\r\n'
[1] [parser] < '200 Switching to Binary mode.\r\n'
[1] [parser] Response: code=200, buffer='Switching to Binary mode.'
/Users/00831103/Workspace/project-2/image-pf-node/node_modules/ftp/lib/connection.js:650
    cb(undefined, RE_WD.exec(text)[1]);
                                  ^

TypeError: Cannot read property '1' of null
    at Object.cb (/Users/00831103/Workspace/project-2/image-pf-node/node_modules/ftp/lib/connection.js:650:35)
    at Parser.<anonymous> (/Users/00831103/Workspace/project-2/pf-node/node_modules/ftp/lib/connection.js:117:20)
    at emitTwo (events.js:87:13)
    at Parser.emit (events.js:172:7)
    at Parser._write (/Users/00831103/Workspace/project-2/image-pf-node/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 Socket.ondata (/Users/00831103/Workspace/project-2/image-pf-node/node_modules/ftp/lib/connection.js:273:20)
    at emitOne (events.js:77:13)
kenichi-shibata commented 8 years ago

code is

var conn_id = 0;

module.exports = function ftpUpload(server,file,pathToRemote){
try{
  var LOCAL_FILE = file;
  var REMOTE_FILE = file;
  var REMOTE_DIRECTORY = pathToRemote;
  var LOCAL_DIRECTORY = './test';

  log.info('uploadingers ftp: ',file);
  var id = conn_id++;
  var connectionProperties = {
      host    : server.HOST,
      user    : server.USER,
      // port    : serve.PORT,
      password: server.PASSWORD,
      debug:   function(msg) {
      console.log('[%d] %s', id, msg);
    }

      // ,
      // secure: true,
      // secureOptions: {
      //    ca: [ fs.readFileSync('resources/gds.pem') ]
      // }
  };

  c.on('ready', function() {
    c.mkdir(REMOTE_DIRECTORY, true, function(err) {
    if (err) throw err;
    });
    c.put('test/'+LOCAL_FILE,REMOTE_DIRECTORY+REMOTE_FILE, function(err) {
      if (err) throw err;
      console.log('====================================');
      console.log('successfully uploaded file to '+REMOTE_DIRECTORY+REMOTE_FILE);
      console.log('====================================');
      c.end();
    });
  });

  c.connect(connectionProperties);
  }

  catch(err){
    log.error(err);
  }
};
mscdex commented 8 years ago

Where does c come from?

kenichi-shibata commented 8 years ago

on the top

var bunyan = require('bunyan');
var fs = require('fs');
var Client = require('ftp');
var c = new Client();

var log = bunyan.createLogger({
  name        : 'ftpUpload',
  level       : "debug",
  stream      : process.stdout,
});
var conn_id = 0;

module.exports = function ftpUpload(server,file,pathToRemote){
try{
  var LOCAL_FILE = file;
  var REMOTE_FILE = file;
  var REMOTE_DIRECTORY = pathToRemote;
  var LOCAL_DIRECTORY = './test';

  log.info('uploadingers ftp: ',file);
  var id = conn_id++;
  var connectionProperties = {
      host    : server.HOST,
      user    : server.USER,
      // port    : serve.PORT,
      password: server.PASSWORD,
      debug:   function(msg) {
      console.log('[%d] %s', id, msg);
    }

      // ,
      // secure: true,
      // secureOptions: {
      //    ca: [ fs.readFileSync('resources/key.pem') ]
      // }
  };

  c.on('ready', function() {
    c.mkdir(REMOTE_DIRECTORY, true, function(err) {
    if (err) throw err;
    });
    c.put('test/'+LOCAL_FILE,REMOTE_DIRECTORY+REMOTE_FILE, function(err) {
      if (err) throw err;
      console.log('====================================');
      console.log('successfully uploaded file to '+REMOTE_DIRECTORY+REMOTE_FILE);
      console.log('====================================');
      c.end();
    });
  });

  c.connect(connectionProperties);
  }

  catch(err){
    log.error(err);
  }
};
mscdex commented 8 years ago

Ah ok, so perhaps reusing/reconnecting a connection object is broken at the moment. Try moving var c = new Client(); inside your ftpUpload() function.

kenichi-shibata commented 8 years ago

Ok will try this.

kenichi-shibata commented 8 years ago

I got it You are awesome @mscdex !

freya0608 commented 5 years ago

the same to you ,but not work

freya0608 commented 5 years ago

when i node a single dir

let  c = new Client();
c.on('ready', function() {
    c.mkdir('/image_site/event/2019/07/activity/tvScreenXian/config/121723',{ recursive: true }, function(err) {
        if (err) throw err;
        c.end();
        console.log('okkkkk')
    });
});
c.connect({

});

it work

freya0608 commented 5 years ago

but in my code it not work

    let  c = new Client();
c.on('ready', function() {
    c.mkdir('/image_site/event/2019/07/activity/tvScreenXian/config/121723',{ recursive: true }, function(err) {
        if (err) throw err;
        c.end();
        console.log('okkkkk')
    });
});

    cb(undefined, RE_WD.exec(text)[1]);
                                  ^

TypeError: Cannot read property '1' of null
    at Object.cb (/Users/chengcheng/Documents/code/myEditor/node_modules/_ftp@0.3.10@ftp/lib/connection.js:650:35)
    at Parser.<anonymous> (/Users/chengcheng/Documents/code/myEditor/node_modules/_ftp@0.3.10@ftp/lib/connection.js:117:20)
    at Parser.emit (events.js:198:13)
    at Parser._write (/Users/chengcheng/Documents/code/myEditor/node_modules/_ftp@0.3.10@ftp/lib/parser.js:59:10)
    at doWrite (_stream_writable.js:415:12)
    at writeOrBuffer (_stream_writable.js:399:5)
    at Parser.Writable.write (_stream_writable.js:299:11)
    at Socket.ondata (/Users/chengcheng/Documents/code/myEditor/node_modules/_ftp@0.3.10@ftp/lib/connection.js:273:20)
    at Socket.emit (events.js:198:13)
    at addChunk (_stream_readable.js:288:12)```