Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): Add optional Error-Output #7460

Merged
merged 18 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ properties:
type: number
waitBetweenTries:
type: number
continueOnFail:
type: boolean
example: false
onError:
type: string
example: 'stopWorkflow'
position:
type: array
items:
Expand Down
8 changes: 7 additions & 1 deletion packages/core/src/NodeExecuteFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2295,7 +2295,13 @@ export function getNodeParameter(
*
*/
export function continueOnFail(node: INode): boolean {
return get(node, 'continueOnFail', false);
const onError = get(node, 'onError', undefined);

if (onError === undefined) {
return get(node, 'continueOnFail', false);
}

return ['continueRegularOutput', 'continueErrorOutput'].includes(onError);
}

/**
Expand Down
110 changes: 109 additions & 1 deletion packages/core/src/WorkflowExecute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import {
IRunExecutionData,
IWorkflowExecuteAdditionalData,
WorkflowExecuteMode,
NodeHelpers,
NodeConnectionType,
} from 'n8n-workflow';
import get from 'lodash/get';
import * as NodeExecuteFunctions from './NodeExecuteFunctions';
Expand Down Expand Up @@ -1042,6 +1044,7 @@ export class WorkflowExecute {
node: executionNode.name,
workflowId: workflow.id,
});

const runNodeData = await workflow.runNode(
executionData,
this.runExecutionData,
Expand All @@ -1052,6 +1055,106 @@ export class WorkflowExecute {
);
nodeSuccessData = runNodeData.data;

if (nodeSuccessData && executionData.node.onError === 'continueErrorOutput') {
// If errorOutput is activated check all the output items for error data.
// If any is found, route them to the last output as that will be the
// error output.

const nodeType = workflow.nodeTypes.getByNameAndVersion(
executionData.node.type,
executionData.node.typeVersion,
);
const outputs = NodeHelpers.getNodeOutputs(
workflow,
executionData.node,
nodeType.description,
);
const outputTypes = NodeHelpers.getConnectionTypes(outputs);
const mainOutputTypes = outputTypes.filter(
(output) => output === NodeConnectionType.Main,
);

const errorItems: INodeExecutionData[] = [];
const successItems: INodeExecutionData[] = [];

// Create a WorkflowDataProxy instance that we can get the data of the
// item which did error
const executeFunctions = NodeExecuteFunctions.getExecuteFunctions(
workflow,
this.runExecutionData,
runIndex,
[],
executionData.data,
executionData.node,
this.additionalData,
executionData,
this.mode,
);
const dataProxy = executeFunctions.getWorkflowDataProxy(0);

// Loop over all outputs except the error output as it would not contain data by default
for (
let outputIndex = 0;
outputIndex < mainOutputTypes.length - 1;
outputIndex++
) {
successItems.length = 0;
const items = nodeSuccessData.length ? nodeSuccessData[0] : [];

while (items.length) {
const item = items.pop();
if (item === undefined) {
continue;
}

const errorData = item.error || item.json.error;

if (errorData) {
const pairedItemData =
item.pairedItem && typeof item.pairedItem === 'object'
? Array.isArray(item.pairedItem)
? item.pairedItem[0]
: item.pairedItem
: undefined;

if (executionData!.source === null || pairedItemData === undefined) {
// Source data is missing for some reason so we can not figure out the item
errorItems.push(item);
} else {
const pairedItemInputIndex = pairedItemData.input || 0;

const sourceData =
executionData!.source[NodeConnectionType.Main][pairedItemInputIndex];

const constPairedItem = dataProxy.$getPairedItem(
sourceData!.previousNode,
sourceData,
pairedItemData,
);

if (constPairedItem === null) {
errorItems.push(item);
} else {
errorItems.push({
...item,
json: {
...constPairedItem.json,
...item.json,
},
});
}
}
} else {
successItems.push(item);
}
}

nodeSuccessData[outputIndex] = successItems;
}

nodeSuccessData[mainOutputTypes.length - 1] = errorItems;
}

if (runNodeData.closeFunction) {
// Explanation why we do this can be found in n8n-workflow/Workflow.ts -> runNode

Expand Down Expand Up @@ -1181,7 +1284,12 @@ export class WorkflowExecute {
taskData.error = executionError;
taskData.executionStatus = 'error';

if (executionData.node.continueOnFail === true) {
if (
executionData.node.continueOnFail === true ||
['continueRegularOutput', 'continueErrorOutput'].includes(
executionData.node.onError || '',
)
) {
// Workflow should continue running even if node errors
if (executionData.data.hasOwnProperty('main') && executionData.data.main.length > 0) {
// Simply get the input data of the node if it has any and pass it through
Expand Down