Skip to content

处理工作流中的错误#

¥Dealing with errors in workflows

有时,你构建了一个完善的工作流,但在尝试执行时却失败了。工作流执行失败的原因多种多样,从节点配置错误或第三方服务故障等简单问题,到更难以解释的错误,不一而足。

¥Sometimes you build a nice workflow, but it fails when you try to execute it. Workflow executions may fail for a variety of reasons, ranging from straightforward problems with incorrectly configuring a node or a failure in a third-party service to more mysterious errors.

但请不要惊慌。在本课中,你将学习如何排查错误,以便尽快启动并运行你的工作流。

¥But don't panic. In this lesson, you'll learn how you can troubleshoot errors so you can get your workflow up and running as soon as possible.

检查失败的工作流#

¥Checking failed workflows

n8n 会跟踪你的工作流的执行情况。

¥n8n tracks executions of your workflows.

当你的某个工作流失败时,你可以查看“执行日志”以了解出错原因。“执行日志”显示已保存工作流的最新执行时间、状态、模式和运行时间列表。

¥When one of your workflows fails, you can check the Executions log to see what went wrong. The Executions log shows you a list of the latest execution time, status, mode, and running time of your saved workflows.

通过在左侧面板中选择 执行 打开“执行日志”。

¥Open the Executions log by selecting Executions in the left-side panel.

要调查列表中特定的失败执行,请选择名称,或将鼠标悬停在相应执行的行上时出现的“查看”按钮。

¥To investigate a specific failed execution from the list, select the name or the View button that appears when you hover over the row of the respective execution.

Executions log
Executions log

此操作将以只读模式打开工作流,你可以在其中查看每个节点的执行情况。此表示法可以帮助你确定工作流在哪个环节遇到问题。

¥This will open the workflow in read-only mode, where you can see the execution of each node. This representation can help you identify at what point the workflow ran into issues.

要在查看执行结果和编辑器之间切换,请选择页面顶部的“编辑器 | 执行结果”按钮。

¥To toggle between viewing the execution and the editor, select the Editor | Executions button at the top of the page.

Workflow execution view
Workflow execution view

捕获出错的工作流#

¥Catching erroring workflows

要捕获失败的工作流,请使用 错误触发节点 创建一个单独的 错误工作流。仅当主工作流执行失败时,此工作流才会执行。

¥To catch failed workflows, create a separate Error Workflow with the Error Trigger node. This workflow will only execute if the main workflow execution fails.

在错误工作流中添加有意义的其他节点,例如通过电子邮件或 Slack 发送有关失败工作流及其错误的通知。

¥Use additional nodes in your Error Workflow that make sense, like sending notifications about the failed workflow and its errors using email or Slack.

要接收失败工作流的错误消息,请将 工作流设置 中的“错误工作流”设置为使用“错误触发”节点的错误工作流。

¥To receive error messages for a failed workflow, set the Error Workflow in the Workflow Settings to an Error Workflow that uses an Error Trigger node.

常规工作流和错误工作流之间的唯一区别在于后者包含一个错误触发节点。请确保在将此节点设置为其他工作流的指定错误工作流之前创建此节点。

¥The only difference between a regular workflow and an Error Workflow is that the latter contains an Error Trigger node. Make sure to create this node before you set this as another workflow's designated Error Workflow.

Error workflows

  • 如果工作流使用“错误触发”节点,则无需激活该工作流。

¥If a workflow uses the Error Trigger node, you don't have to activate the workflow.

  • 如果工作流包含错误触发节点,默认情况下,工作流会使用自身作为错误工作流。

¥If a workflow contains the Error Trigger node, by default, the workflow uses itself as the error workflow.

  • 手动运行工作流时,无法测试错误工作流。“错误触发器”仅在自动工作流出错时运行。

¥You can't test error workflows when running workflows manually. The Error trigger only runs when an automatic workflow errors.

  • 你可以为多个工作流设置相同的错误工作流。

¥You can set the same Error Workflow for multiple workflows.

练习#

¥Exercise

在前面的章节中,你已经构建了几个小型工作流。现在,选择其中一个要监控的订单,并为其创建一个错误工作流:

¥In the previous chapters, you've built several small workflows. Now, pick one of them that you want to monitor and create an Error Workflow for it:

  1. 创建一个新的错误工作流。

¥Create a new Error Workflow. 2. 添加错误触发节点。

¥Add the Error Trigger node. 3. 将你选择的通信平台节点(例如 SlackDiscordTelegram,甚至是 Gmail 或更通用的 发送电子邮件)连接到“错误触发”节点。

¥Connect a node for the communication platform of your choice to the Error Trigger node, like Slack, Discord, Telegram, or even Gmail or a more generic Send Email. 4. 在你要监控的工作流中,打开 工作流设置,然后选择你刚刚创建的新错误工作流。请注意,此工作流需要自动运行才能触发错误工作流。

¥In the workflow you want to monitor, open the Workflow Settings and select the new Error Workflow you just created. Note that this workflow needs to run automatically to trigger the error workflow.

显示解决方案

¥??? note "Show me the solution"

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
The workflow for this exercise looks like this:

<figure><img src="/_images/courses/level-two/chapter-four/exercise_errors_errortriggernode_workflow.png" alt="" style="width:100%"><figcaption align = "center"><i>Error workflow</i></figcaption></figure>

To check the configuration of the nodes, you can copy the JSON workflow code below and paste it into your Editor UI:

```json
{
	"nodes": [
		{
			"parameters": {},
			"name": "Error Trigger",
			"type": "n8n-nodes-base.errorTrigger",
			"typeVersion": 1,
			"position": [
				720,
				-380
			]
		},
		{
			"parameters": {
				"channel": "channelname",
				"text": "=This workflow {{$node[\"Error Trigger\"].json[\"workflow\"][\"name\"]}}failed.\nHave a look at it here: {{$node[\"Error Trigger\"].json[\"execution\"][\"url\"]}}",
				"attachments": [],
				"otherOptions": {}
			},
			"name": "Slack",
			"type": "n8n-nodes-base.slack",
			"position": [
				900,
				-380
			],
			"typeVersion": 1,
			"credentials": {
				"slackApi": {
					"id": "17",
					"name": "slack_credentials"
				}
			}
		}
	],
	"connections": {
		"Error Trigger": {
			"main": [
				[
					{
						"node": "Slack",
						"type": "main",
						"index": 0
					}
				]
			]
		}
	}
}
```

在工作流中抛出异常#

¥Throwing exceptions in workflows

另一种排查工作流故障的方法是在你的工作流中包含 停止和错误节点。此节点抛出错误。你可以指定错误类型:

¥Another way of troubleshooting workflows is to include a Stop and Error node in your workflow. This node throws an error. You can specify the error type:

  • 错误信息:返回关于错误的自定义消息

¥Error Message: returns a custom message about the error

  • 错误对象:返回错误类型

¥Error Object: returns the type of error

“停止和错误”节点只能作为工作流中的最后一个节点。

¥You can only use the Stop and Error node as the last node in a workflow.

When to throw errors

使用“停止和错误”节点抛出异常有助于验证节点中的数据(或关于数据的假设)并返回自定义错误消息。

¥Throwing exceptions with the Stop and Error node is useful for verifying the data (or assumptions about the data) from a node and returning custom error messages.

如果你正在处理来自第三方服务的数据,你可能会遇到以下问题:

¥If you are working with data from a third-party service, you may come across problems such as:

  • JSON 输出格式错误

¥Wrongly formatted JSON output

  • 数据类型错误(例如,数值数据对应非数值)

¥Data with the wrong type (for example, numeric data that has a non-numeric value)

  • 缺少值

¥Missing values

  • 来自远程服务器的错误

¥Errors from remote servers

虽然这类无效数据可能不会立即导致工作流失败,但可能会在后续引发问题,并且难以追踪错误根源。这就是为什么在发现问题时最好立即抛出错误的原因。

¥Though this kind of invalid data might not cause the workflow to fail right away, it could cause problems later on, and then it can become difficult to track the source error. This is why it's better to throw an error at the time you know there might be a problem.

Stop and Error node with error message
Stop and Error node with error message