From 1ff97a018638386a3e89a005370666d33a28fae8 Mon Sep 17 00:00:00 2001 From: Michael Kret <88898367+michael-radency@users.noreply.github.com> Date: Fri, 16 Aug 2024 18:36:15 +0300 Subject: [PATCH] fix: Better errors in Switch, If and Filter nodes (#10457) Co-authored-by: Shireen Missi --- .../nodes/Filter/V2/FilterV2.node.ts | 27 ++++++++++++++----- packages/nodes-base/nodes/If/V2/IfV2.node.ts | 27 ++++++++++++++----- .../nodes/Switch/V3/SwitchV3.node.ts | 15 +++++++++-- 3 files changed, 53 insertions(+), 16 deletions(-) diff --git a/packages/nodes-base/nodes/Filter/V2/FilterV2.node.ts b/packages/nodes-base/nodes/Filter/V2/FilterV2.node.ts index 6c7e50c197672..16e95ef634d7c 100644 --- a/packages/nodes-base/nodes/Filter/V2/FilterV2.node.ts +++ b/packages/nodes-base/nodes/Filter/V2/FilterV2.node.ts @@ -1,10 +1,12 @@ import set from 'lodash/set'; -import type { - IExecuteFunctions, - INodeExecutionData, - INodeType, - INodeTypeBaseDescription, - INodeTypeDescription, +import { + ApplicationError, + NodeOperationError, + type IExecuteFunctions, + type INodeExecutionData, + type INodeType, + type INodeTypeBaseDescription, + type INodeTypeDescription, } from 'n8n-workflow'; import { ENABLE_LESS_STRICT_TYPE_VALIDATION } from '../../../utils/constants'; @@ -101,7 +103,18 @@ export class FilterV2 implements INodeType { if (this.continueOnFail(error)) { discardedItems.push(item); } else { - throw error; + if (error instanceof NodeOperationError) { + throw error; + } + + if (error instanceof ApplicationError) { + set(error, 'context.itemIndex', itemIndex); + throw error; + } + + throw new NodeOperationError(this.getNode(), error, { + itemIndex, + }); } } }); diff --git a/packages/nodes-base/nodes/If/V2/IfV2.node.ts b/packages/nodes-base/nodes/If/V2/IfV2.node.ts index 2e90747bed6d1..31fb258e76ebe 100644 --- a/packages/nodes-base/nodes/If/V2/IfV2.node.ts +++ b/packages/nodes-base/nodes/If/V2/IfV2.node.ts @@ -1,10 +1,12 @@ import set from 'lodash/set'; -import type { - IExecuteFunctions, - INodeExecutionData, - INodeType, - INodeTypeBaseDescription, - INodeTypeDescription, +import { + ApplicationError, + NodeOperationError, + type IExecuteFunctions, + type INodeExecutionData, + type INodeType, + type INodeTypeBaseDescription, + type INodeTypeDescription, } from 'n8n-workflow'; import { ENABLE_LESS_STRICT_TYPE_VALIDATION } from '../../../utils/constants'; @@ -101,7 +103,18 @@ export class IfV2 implements INodeType { if (this.continueOnFail(error)) { falseItems.push(item); } else { - throw error; + if (error instanceof NodeOperationError) { + throw error; + } + + if (error instanceof ApplicationError) { + set(error, 'context.itemIndex', itemIndex); + throw error; + } + + throw new NodeOperationError(this.getNode(), error, { + itemIndex, + }); } } }); diff --git a/packages/nodes-base/nodes/Switch/V3/SwitchV3.node.ts b/packages/nodes-base/nodes/Switch/V3/SwitchV3.node.ts index 42939ddec654b..2c54fb860efe3 100644 --- a/packages/nodes-base/nodes/Switch/V3/SwitchV3.node.ts +++ b/packages/nodes-base/nodes/Switch/V3/SwitchV3.node.ts @@ -9,7 +9,7 @@ import type { INodeTypeBaseDescription, INodeTypeDescription, } from 'n8n-workflow'; -import { NodeConnectionType, NodeOperationError } from 'n8n-workflow'; +import { ApplicationError, NodeConnectionType, NodeOperationError } from 'n8n-workflow'; import set from 'lodash/set'; import { capitalize } from '@utils/utilities'; import { ENABLE_LESS_STRICT_TYPE_VALIDATION } from '../../../utils/constants'; @@ -382,7 +382,18 @@ export class SwitchV3 implements INodeType { returnData[0].push({ json: { error: error.message } }); continue; } - throw new NodeOperationError(this.getNode(), error); + if (error instanceof NodeOperationError) { + throw error; + } + + if (error instanceof ApplicationError) { + set(error, 'context.itemIndex', itemIndex); + throw error; + } + + throw new NodeOperationError(this.getNode(), error, { + itemIndex, + }); } }