jsftp icon indicating copy to clipboard operation
jsftp copied to clipboard

unhandled error: ECONNREFUSED

Open joniba opened this issue 6 years ago • 12 comments

I've seen a bunch of issues on this and all were closed claiming this was fixed. I must be missing something. I'm on version 2.0.0. But this code throws an unhandled error:

var FTP = require('jsftp');
new FTP({ host: 'localhost', port: 22 });

This will crash a running node.js process. Why is this unhandled? Why doesn't it raise any of the debug events? Is there a workaround for this?

joniba avatar Jul 18 '17 14:07 joniba

Are you using username and password ?? @joniba

Check if that port is used else where.

omkartin avatar Jul 18 '17 15:07 omkartin

Try this @joniba : var JsFtp = require('jsftp'); var FTP = new JsFtp({ host: 'localhost', port: 21, user: 'xxx', pass: 'xxxx' });

That should not give you unhandled error.

omkartin avatar Jul 18 '17 15:07 omkartin

Thanks, that does indeed prevent the error. However, this has made me feel sufficiently unsafe that I wrapped jsftp with a nodejs domain. My ftp urls are configured by external users. I wouldn't want my application to explode just because a user forgot to put in a username and password!

joniba avatar Jul 18 '17 18:07 joniba

@joniba May be the jsftp library can be enhanced to error out or give err msg if no user name or password is provided rather than giving you the above error.

You can request @sergi to add those capabilities.

omkartin avatar Jul 18 '17 19:07 omkartin

This is not only about username or password. If you provide an invalid ftp url, or the ftp site is down, this library explodes. The call to jsftp.upload does not return an error as documented. Honestly, the most basic thing I would expect from any library that manages out-of-process communications is error handling connection issues.

joniba avatar Jul 20 '17 12:07 joniba

Hi @joniba,

Thanks for this report. You are right, this should not explode in such a blatant way. I'll be fixing this asap.

sergi avatar Aug 25 '17 11:08 sergi

Also, not providing username and password doesn't make the library crash, because anonymous user is used. It does crash when the URL/PORT combination doesn't exist.

/cc @joniba @omkartin

sergi avatar Aug 25 '17 11:08 sergi

Any news on this?

hpawe01 avatar Mar 19 '18 21:03 hpawe01

Hi,

I've got the same problem on constructor with version 2.1.2 when my FTP server timed out. Naively I tried to catch the error but when I looked at the code it seems it's an event.

@sergi Any news on this? How can I help you fix this?

Thanks

JbIPS avatar Mar 24 '18 10:03 JbIPS

For those looking for a workaround, adding an error listener to the ftp instance did the job:

const ftp = new Jsftp(opts);
ftp.on('error', (err) => console.error(err));

JbIPS avatar Mar 26 '18 12:03 JbIPS

Thanks @JbIPS for this ! But if i wrap this in a try...catch and if i throw a new error instead of logging it i still have an uncaughtException...

      try {
        const ftp = new Jsftp(opts)
        ftp.on('error', (err) => {
          throw new Error('Error when connecting to FTP')
        })
      } catch (ex) {
        console.log('ex :', ex)
        throw ex
      }

This is running inside a function called on a specific expressjs route. The uncaughtExpression is catch like this

process.on('uncaughtException', function (ex) {
  console.log('logging uncaughtException')
  sendError(ex)
})

But the Nodejs server still crash...

Any idea ? Thanks !

GautierT avatar Nov 23 '18 09:11 GautierT

Hello @GautierT,

You can't catch an event, they aren't run at the same time so your thrown error isn't caught.

If you want to use try catch, you would have to wrap the listener in a Promise and use await. That the only way it will be caught.

JbIPS avatar Nov 23 '18 18:11 JbIPS