How to capture ftp-client connect attempt errors

Viewed 318

I use the npm package ftp-client to download files from an ftp server. I cannot determine how to capture possible errors when attempting to connect to the remote server. Documentation on the ftp-client package is very sparse.

Can anyone help?

My code is below. As it stands any connection error (e.g. invalid password) just crashes the application.

const ftpClient = require('ftp-client');
require('dotenv').config();

const dtf_login = process.env.dtf_login;
const dtf_psw = process.env.dtf_psw;
const dtf_host = process.env.dtf_host;

function doDirDownload(remoteDir, localDir, callback){
    var ftpOptions = {logging : 'none', overwrite : 'all'};

    var ftpConfig = {host : dtf_host, 
        port : 21, 
        user : dtf_login, 
        password : dtf_psw};

    c = new ftpClient(ftpConfig, ftpOptions);

    c.connect((err) => {
        if(err){
            callback(err);
        } else {
            c.download(remoteDir, localDir, 
            {overwrite: 'all'}, (result) => {
                callback(result);
            });         
        }
    });
}
0 Answers
Related