From d3a312cc6b018e6ba1108eea489bd6ac0186c1a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Ovejero?= Date: Thu, 7 Oct 2021 22:31:38 +0200 Subject: [PATCH] :sparkles: Add Stop and Error node (#2232) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Throw Error Node * :hammer: Refactor Throw Error node * :truck: Rename node * :zap: Allow multiple nodes in the workflow * :truck: Rename node * :zap: Fix codex file name Co-authored-by: Günther Erb Co-authored-by: Jan Oberhauser --- .../nodes-base/nodes/StopAndError.node.json | 25 ++++ .../nodes-base/nodes/StopAndError.node.ts | 110 ++++++++++++++++++ packages/nodes-base/package.json | 1 + 3 files changed, 136 insertions(+) create mode 100644 packages/nodes-base/nodes/StopAndError.node.json create mode 100644 packages/nodes-base/nodes/StopAndError.node.ts diff --git a/packages/nodes-base/nodes/StopAndError.node.json b/packages/nodes-base/nodes/StopAndError.node.json new file mode 100644 index 0000000000..e50d829485 --- /dev/null +++ b/packages/nodes-base/nodes/StopAndError.node.json @@ -0,0 +1,25 @@ +{ + "node": "n8n-nodes-base.stopAndError", + "nodeVersion": "1.0", + "codexVersion": "1.0", + "categories": [ + "Core Nodes" + ], + "resources": { + "primaryDocumentation": [ + { + "url": "https://docs.n8n.io/nodes/n8n-nodes-base.stopAndError/" + } + ] + }, + "alias": [ + "error", + "throw", + "exception" + ], + "subcategories": { + "Core Nodes": [ + "Flow" + ] + } +} diff --git a/packages/nodes-base/nodes/StopAndError.node.ts b/packages/nodes-base/nodes/StopAndError.node.ts new file mode 100644 index 0000000000..b8d8ffb109 --- /dev/null +++ b/packages/nodes-base/nodes/StopAndError.node.ts @@ -0,0 +1,110 @@ +import { + IExecuteFunctions, +} from 'n8n-core'; + +import { + INodeExecutionData, + INodeType, + INodeTypeDescription, + NodeOperationError, +} from 'n8n-workflow'; + +const errorObjectPlaceholder = `{ + "code": "404", + "description": "The resource could not be fetched" +}`; + +export class StopAndError implements INodeType { + description: INodeTypeDescription = { + displayName: 'Stop and Error', + name: 'stopAndError', + icon: 'fa:exclamation-triangle', + group: ['input'], + version: 1, + description: 'Throw an error in the workflow', + defaults: { + name: 'Stop And Error', + color: '#ff0000', + }, + inputs: ['main'], + outputs: [], + properties: [ + { + displayName: 'Error Type', + name: 'errorType', + type: 'options', + options: [ + { + name: 'Error Message', + value: 'errorMessage', + }, + { + name: 'Error Object', + value: 'errorObject', + }, + ], + default: 'errorMessage', + description: 'Type of error to throw', + }, + { + displayName: 'Error Message', + name: 'errorMessage', + type: 'string', + placeholder: 'An error occurred!', + typeOptions: { + alwaysOpenEditWindow: true, + }, + default: '', + required: true, + displayOptions: { + show: { + errorType: [ + 'errorMessage', + ], + }, + }, + }, + { + displayName: 'Error Object', + name: 'errorObject', + type: 'json', + description: 'Object containing error properties', + default: '', + typeOptions: { + alwaysOpenEditWindow: true, + }, + placeholder: errorObjectPlaceholder, + required: true, + displayOptions: { + show: { + errorType: [ + 'errorObject', + ], + }, + }, + }, + ], + }; + + execute(this: IExecuteFunctions): Promise { + const errorType = this.getNodeParameter('errorType', 0) as 'errorMessage' | 'errorObject'; + const { id: workflowId, name: workflowName } = this.getWorkflow(); + + let toThrow: string | { name: string; message: string; [otherKey: string]: unknown }; + + if (errorType === 'errorMessage') { + toThrow = this.getNodeParameter('errorMessage', 0) as string; + } else { + const json = this.getNodeParameter('errorObject', 0) as string; + const errorObject = JSON.parse(json); + + toThrow = { + name: 'User-thrown error', + message: `Workflow ID ${workflowId} "${workflowName}" has failed`, + ...errorObject, + }; + } + + throw new NodeOperationError(this.getNode(), toThrow); + } +} diff --git a/packages/nodes-base/package.json b/packages/nodes-base/package.json index 341eff0860..9c1352f3fb 100644 --- a/packages/nodes-base/package.json +++ b/packages/nodes-base/package.json @@ -584,6 +584,7 @@ "dist/nodes/Salesmate/Salesmate.node.js", "dist/nodes/Segment/Segment.node.js", "dist/nodes/Sendy/Sendy.node.js", + "dist/nodes/StopAndError.node.js", "dist/nodes/SurveyMonkey/SurveyMonkeyTrigger.node.js", "dist/nodes/Taiga/Taiga.node.js", "dist/nodes/Taiga/TaigaTrigger.node.js",