在 node-ftp 模块中捕获未捕获的异常错误

Catch Uncaught Exception error in node-ftp module

我正在使用 npm ftp 库:https://www.npmjs.com/package/ftp 我想在 ftp 服务器上上传一个文件,我想在连接期间捕获错误,例如登录错误。

我的函数:

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);
    }
};

但我总是得到 'ERROR Uncaught Exception' 即使我使用

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

完整堆栈跟踪:

{"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)"]}

如何处理错误?

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