Ad

Catch Uncaught Exception Error In Node-ftp Module

I'm using the npm ftp library: https://www.npmjs.com/package/ftp I want to upload a file on a ftp server and I want to catch errors during the connection, e.g. a wrong login.

My function:

const upload = () => {

    try {
        const ftpClient = new ftp();
        fs.readFile(filePathToUpload, 'utf8', (err, data) => {
            const fileValue = data.concat(' Timestamp: ', new Date());

            ftpClient.on('ready', () => {
                ftpClient.put(fileValue, FILENAME_REMOTE, (err) => {
                    console.log('PUT FTP');
                    if (err) {
                        console.log(err);
                    }
                    ftpClient.end();
                });
            });

            try {
                console.log('connect upload');
                ftpClient.connect(ftpConfig);
            } catch (connectionError) {
                console.log('Connection Error: ', connectionError);
            }
        });
    } catch (err) {
        console.log('ERROR-FTP-Upload: ', err);
    }
};

But I always get 'ERROR Uncaught Exception' even if I use

process.on('uncaughtException', err => console.log(err));

Full Stack Trace:

{"errorType":"Error","errorMessage":"Login incorrect.","code":530,"stack":["Error: Login incorrect.","    at makeError (/var/task/node_modules/ftp/lib/connection.js:1067:13)","    at Parser.<anonymous> (/var/task/node_modules/ftp/lib/connection.js:113:25)","    at Parser.emit (events.js:189:13)","    at Parser.EventEmitter.emit (domain.js:441:20)","    at Parser._write (/var/task/node_modules/ftp/lib/parser.js:59:10)","    at doWrite (_stream_writable.js:410:12)","    at writeOrBuffer (_stream_writable.js:394:5)","    at Parser.Writable.write (_stream_writable.js:294:11)","    at Socket.ondata (/var/task/node_modules/ftp/lib/connection.js:273:20)","    at Socket.emit (events.js:189:13)"]}

How can I handle the error?

Ad

Answer

const ftpClient = new ftp();
ftpClient.on('error',console.dir);//add error listener.
// rest code...
Ad
source: stackoverflow.com
Ad