jsftp
jsftp copied to clipboard
unhandled error: ECONNREFUSED
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?
Are you using username and password ?? @joniba
Check if that port is used else where.
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.
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 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.
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.
Hi @joniba,
Thanks for this report. You are right, this should not explode in such a blatant way. I'll be fixing this asap.
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
Any news on this?
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
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));
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 !
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.