mscdex / node-ftp

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

Catch connection error is not possible #279

Closed vielhuber closed 2 years ago

vielhuber commented 2 years ago

Hello!

From time to time in my script the following error is thrown:

node:events:505
      throw er; // Unhandled 'error' event
      ^

Error: OOPS: priv_sock_get_int
    at makeError (/var/www/test/node_modules/ftp/lib/connection.js:1067:13)
    at Parser.<anonymous> (/var/www/test/node_modules/ftp/lib/connection.js:115:28)
    at Parser.emit (node:events:527:28)
    at Parser._write (/var/www/test/node_modules/ftp/lib/parser.js:59:10)
    at writeOrBuffer (node:internal/streams/writable:389:12)
    at _write (node:internal/streams/writable:330:10)
    at Parser.Writable.write (node:internal/streams/writable:334:10)
    at Socket.ondata (/var/www/test/node_modules/ftp/lib/connection.js:273:20)
    at Socket.emit (node:events:527:28)
    at addChunk (node:internal/streams/readable:315:12)
Emitted 'error' event on  instance at:
    at Parser.<anonymous> (/var/www/test/node_modules/ftp/lib/connection.js:115:14)
    at Parser.emit (node:events:527:28)
    [... lines matching original stack trace ...]
    at addChunk (node:internal/streams/readable:315:12)
    at readableAddChunk (node:internal/streams/readable:289:9) {
  code: 500
}

Example code:

 var c = new Client();
  c.on('ready', function() {
    c.get('foo.txt', function(err, stream) {
      if (err) throw err;
      stream.once('close', function() { c.end(); });
      stream.pipe(fs.createWriteStream('foo.local-copy.txt'));
    });
  });
  c.connect();

If I wrap c.connect(); with try {} catch {} doesn't change anything.

How am I able to prevent that my script gets stuck any properly catch those errors?

vielhuber commented 2 years ago

Simple solution:

  c.on('error', function(err) {
      console.log(err);
  });