Skip to content

Commit

Permalink
Improved error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
teastman committed Sep 29, 2017
1 parent eba2a55 commit 6e4ea95
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 21 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ Usage
This node allows you to download one or more files within a single connection.

The file paths may be defined in one of a number of ways.
- `msg.`: A path to a variable on the msg object that contains an array of Strings.
- `flow.`: A path to a variable on the flow context object that contains an array of Strings.
- `global.`: A path to a variable on the global context object that contains an array of Strings.
- `msg.`: A variable on the msg object that contains an array of Strings.
- `flow.`: A variable on the flow context object that contains an array of Strings.
- `global.`: A variable on the global context object that contains an array of Strings.
- `json`: A JSON array of Strings.
- `string`: A singular path String.

The destination path may be defined in one of a number of ways.
- `msg.`: A path to a variable on the msg object that contains a local path String.
- `flow.`: A path to a variable on the flow context object that contains a local path String
- `global.`: A path to a variable on the global context object that contains a local path String
- `msg.`: A variable on the msg object that contains a local path String.
- `flow.`: A variable on the flow context object that contains a local path String
- `global.`: A variable on the global context object that contains a local path String
- `string`: A singular local path String.

As an output an Array of local file path Strings is places in `msg.payload`
Expand Down
54 changes: 40 additions & 14 deletions ftp-download.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,19 @@ module.exports = function (RED) {
fileList = global.get(node.files);
break;
case 'json':
fileList = node.files;
fileList = JSON.parse(node.files);
break;
default:
fileList = [node.files];
}
if (!(fileList instanceof Array))
node.error("Files field must be an array.");
if (!Array.isArray(fileList)) {
node.error("Files field must be an array.", msg);
return;
}
}
catch (err) {
node.error("Could not load files variable, type: " + node.filesType + " location: " + node.files, msg);
return;
}

// Load the destination from the appropriate variable.
Expand All @@ -103,6 +106,7 @@ module.exports = function (RED) {
}
catch (err) {
node.error("Could not load destination variable, type: " + node.destinationType + " location: " + node.destination, msg);
return;
}

// Assert we have access to write to the destination
Expand All @@ -111,6 +115,7 @@ module.exports = function (RED) {
}
catch (err) {
node.error("Lacking permission to write files to " + destination, msg);
return;
}

conn.on('ready', () => {
Expand All @@ -123,32 +128,53 @@ module.exports = function (RED) {

promise
.then((filePaths)=> {
conn.end();
msg.payload = filePaths;
node.send(msg);
})
.catch((err) => {
conn.end();
msg.payload = err;
msg.payload = {
fileList: fileList,
destination: destination,
error: err
};
node.error("FTP download failed", msg);
})
});

function download(file) {
return (filePaths) => new Promise((resolve, reject) => {
conn.get(file, (err, stream) => {
if (err)
throw err;
let filePath = path.join(destination, path.basename(file));
filePaths.push(filePath);
stream.once('finish', () => resolve(filePaths));
stream.pipe(fs.createWriteStream(filePath));
});
try {
conn.get(file, (err, stream) => {
try {
if (err)
throw err;
let filePath = path.join(destination, path.basename(file));
filePaths.push(filePath);
stream.once('finish', () => resolve(filePaths));
stream.pipe(fs.createWriteStream(filePath));
}
catch (error) {
reject(error);
}
});
}
catch (error) {
reject(error);
}
});
}

conn.connect(node.serverConfig.options);
});

node.on('close', () => {
try {
conn.destroy();
}
catch (err) {
// Do nothing as the node is closed anyway.
}
});
}

RED.nodes.registerType('ftp-download', FtpDownloadNode);
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6e4ea95

Please sign in to comment.