Skip to content

错误处理(Error handling)#

在设计流程逻辑时,考虑潜在的错误并设置处理方法是一种良好的做法。通过错误工作流,你可以控制 n8n 在工作流执行失败时的响应方式。

🌐 When designing your flow logic, it's a good practice to consider potential errors, and set up methods to handle them gracefully. With an error workflow, you can control how n8n responds to a workflow execution failure.

调查错误

要调查执行失败的问题,你可以:

创建和设置错误工作流(Create and set an error workflow)#

对于每个工作流,你可以在 工作流设置 中设置错误工作流。如果执行失败,它将运行。这意味着,例如,当工作流执行出错时,你可以发送电子邮件或 Slack 警报。错误工作流必须以 错误触发器 开始。

🌐 For each workflow, you can set an error workflow in Workflow Settings. It runs if an execution fails. This means you can, for example, send email or Slack alerts when a workflow execution errors. The error workflow must start with the Error Trigger.

你可以将相同的错误工作流用于多个工作流。

🌐 You can use the same error workflow for multiple workflows.

  1. Create a new workflow, with the Error Trigger as the first node.
  2. Give the workflow a name, for example Error Handler.
  3. Select Save.
  4. In the workflow where you want to use this error workflow:
    1. Select Options Options menu icon > Settings.
    2. In Error workflow, select the workflow you just created. For example, if you used the name Error Handler, select Error handler.
    3. Select Save. Now, when this workflow errors, the related error workflow runs.

错误数据(Error data)#

The default error data received by the Error Trigger is:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
[
	{
		"execution": {
			"id": "231",
			"url": "https://n8n.example.com/execution/231",
			"retryOf": "34",
			"error": {
				"message": "Example Error Message",
				"stack": "Stacktrace"
			},
			"lastNodeExecuted": "Node With Error",
			"mode": "manual"
		},
		"workflow": {
			"id": "1",
			"name": "Example Workflow"
		}
	}
]

All information is always present, except:

  • execution.id: requires the execution to be saved in the database. Not present if the error is in the trigger node of the main workflow, as the workflow doesn't execute.
  • execution.url: requires the execution to be saved in the database. Not present if the error is in the trigger node of the main workflow, as the workflow doesn't execute.
  • execution.retryOf: only present when the execution is a retry of a failed execution.

If the error is caused by the trigger node of the main workflow, rather than a later stage, the data sent to the error workflow is different. There's less information in execution{} and more in trigger{}:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
  "trigger": {
    "error": {
      "context": {},
      "name": "WorkflowActivationError",
      "cause": {
        "message": "",
        "stack": ""
      },
      "timestamp": 1654609328787,
      "message": "",
      "node": {
        . . . 
      }
    },
    "mode": "trigger"
  },
  "workflow": {
    "id": "",
    "name": ""
  }
}

使用“停止并报错”功能导致工作流执行失败(Cause a workflow execution failure using Stop And Error)#

当你创建并设置错误工作流时,n8n 会在执行失败时运行它。通常,这是由于节点设置中的错误或工作流内存不足等原因导致的。

🌐 When you create and set an error workflow, n8n runs it when an execution fails. Usually, this is due to things like errors in node settings, or the workflow running out of memory.

你可以将 Stop And Error 节点添加到工作流中,以在你选择的情况下强制执行失败,并触发错误工作流。

🌐 You can add the Stop And Error node to your workflow to force executions to fail under your chosen circumstances, and trigger the error workflow.