Improve handling of incorrect return data of Function Nodes

This commit is contained in:
Jan Oberhauser
2019-12-23 14:54:27 -06:00
parent 2b343a4aa5
commit 1ed5a4589d
4 changed files with 33 additions and 3 deletions

View File

@@ -80,11 +80,36 @@ export class Function implements INodeType {
try {
// Execute the function code
items = await vm.run(`module.exports = async function() {${functionCode}}()`);
items = (await vm.run(`module.exports = async function() {${functionCode}}()`));
} catch (e) {
return Promise.reject(e);
}
// Do very basic validation of the data
if (items === undefined) {
throw new Error('No data got returned. Always return an Array of items!');
}
if (!Array.isArray(items)) {
throw new Error('Always an Array of items has to be returned!');
}
for (const item of items) {
if (item.json === undefined) {
throw new Error('All returned items have to contain property named "json"!');
}
if (item.json === undefined) {
throw new Error('All returned items have to contain a property named "json"!');
}
if (typeof item.json !== 'object') {
throw new Error('The json-property has to be an object!');
}
if (item.binary !== undefined) {
if (Array.isArray(item.binary) || typeof item.binary !== 'object') {
throw new Error('The binary-property has to be an object!');
}
}
}
return this.prepareOutputData(items);
}
}