在自动化工作流中,错误处理是确保流程稳定可靠的关键。n8n 的 Stop And Error 节点为您提供了强大的工具,能够在特定条件下主动中断工作流执行,并通过自定义错误消息传递关键信息。本教程将帮助初学者快速掌握这个核心节点的使用方法。
Stop And Error 节点是 n8n 中的一个流程控制节点,用于:
| 功能特性 | 说明 |
|---|---|
| 条件触发 | 根据业务逻辑主动抛出错误 |
| 灵活消息 | 支持动态消息和表达式注入 |
| 错误追踪 | 自动记录错误信息便于调试 |
| 工作流联动 | 与 Error Trigger 无缝配合 |
快速查找技巧:
在节点搜索框中输入 "stop" 即可快速定位到 Stop And Error 节点Stop And Error 节点提供两种错误类型:
适用场景:需要发送简单、易读的错误信息
配置方法:
示例:
邮箱验证失败!请确保输入了有效的邮箱地址。用户ID: {{ $json.id }}适用场景:需要传递结构化的错误信息到错误工作流
配置方法:
示例:
{
"errorCode": "INVALID_EMAIL",
"message": "邮箱格式不正确",
"userId": "{{ $json.id }}",
"timestamp": "{{ new Date().toISOString() }}",
"retryable": false
}Stop And Error 节点通常配合 If 节点使用:
为了让错误被捕获并处理:
以下是一个完整的可执行工作流,展示如何使用 Stop And Error 节点进行用户数据验证:
Start
↓
获取用户数据
↓
检查邮箱字段
├─ 如果为空 → Stop And Error(邮箱不能为空)
├─ 如果有效 → 检查年龄字段
├─ 如果小于0 → Stop And Error(年龄不能为负数)
├─ 如果有效 → 数据验证成功,继续处理将以下 JSON 代码复制到您的 n8n 中:
{
"name": "Data Validation with Stop And Error",
"nodes": [
{
"parameters": {},
"name": "Start",
"type": "n8n-nodes-base.start",
"typeVersion": 1,
"position": [250, 300]
},
{
"parameters": {
"functionCode": "// 模拟获取用户数据\nreturn [\n {\n \"json\": {\n \"id\": 1,\n \"name\": \"张三\",\n \"email\": \"zhangsan@example.com\",\n \"age\": 25\n }\n },\n {\n \"json\": {\n \"id\": 2,\n \"name\": \"李四\",\n \"email\": \"\",\n \"age\": -5\n }\n }\n];"
},
"name": "Get User Data",
"type": "n8n-nodes-base.function",
"typeVersion": 1,
"position": [450, 300]
},
{
"parameters": {
"conditions": {
"boolean": [
{
"value1": "{{ $json.email }}",
"condition": "isEmpty"
}
]
}
},
"name": "Check Email",
"type": "n8n-nodes-base.if",
"typeVersion": 1,
"position": [650, 300]
},
{
"parameters": {
"errorType": "errorMessage",
"errorMessage": "邮箱不能为空!用户ID: {{ $json.id }},用户名: {{ $json.name }}"
},
"name": "Stop - Invalid Email",
"type": "n8n-nodes-base.stopanderror",
"typeVersion": 1,
"position": [850, 200]
},
{
"parameters": {
"conditions": {
"boolean": [
{
"value1": "{{ $json.age }}",
"condition": "lessThan",
"value2": "0"
}
]
}
},
"name": "Check Age",
"type": "n8n-nodes-base.if",
"typeVersion": 1,
"position": [850, 400]
},
{
"parameters": {
"errorType": "errorMessage",
"errorMessage": "年龄不能为负数!用户ID: {{ $json.id }},年龄: {{ $json.age }}"
},
"name": "Stop - Invalid Age",
"type": "n8n-nodes-base.stopanderror",
"typeVersion": 1,
"position": [1050, 400]
},
{
"parameters": {
"functionCode": "// 数据验证成功,返回处理结果\nreturn [\n {\n \"json\": {\n \"status\": \"success\",\n \"message\": \"数据验证通过\",\n \"user_id\": $json.id,\n \"user_name\": $json.name,\n \"email\": $json.email,\n \"age\": $json.age,\n \"timestamp\": new Date().toISOString()\n }\n }\n];"
},
"name": "Data Valid - Process",
"type": "n8n-nodes-base.function",
"typeVersion": 1,
"position": [1050, 500]
}
],
"connections": {
"Start": {
"main": [
[
{
"node": "Get User Data",
"type": "main",
"index": 0
}
]
]
},
"Get User Data": {
"main": [
[
{
"node": "Check Email",
"type": "main",
"index": 0
}
]
]
},
"Check Email": {
"main": [
[
{
"node": "Check Age",
"type": "main",
"index": 0
}
],
[
{
"node": "Stop - Invalid Email",
"type": "main",
"index": 0
}
]
]
},
"Check Age": {
"main": [
[
{
"node": "Data Valid - Process",
"type": "main",
"index": 0
}
],
[
{
"node": "Stop - Invalid Age",
"type": "main",
"index": 0
}
]
]
}
}
}// 在错误工作流中,可以使用以下表达式获取错误详情
{{ $node["Error Trigger"].json.workflow.name }} // 原工作流名称
{{ $node["Error Trigger"].json.workflow.id }} // 原工作流ID
{{ $node["Error Trigger"].json.error.message }} // 错误消息
{{ $node["Error Trigger"].json.execution.id }} // 执行ID| 问题 | 解决方案 |
|---|---|
| Stop And Error 节点没有触发 | 检查条件判断是否正确连接,确保数据流指向该节点 |
| 错误消息中的表达式没有生效 | 确保使用正确的表达式语法:{{ $json.fieldName }} |
| 错误工作流没有被触发 | 检查错误工作流是否以 Error Trigger 开始,且已在主工作流设置中配置 |
| Error Object 无法解析 | 确保 JSON 格式有效,使用在线 JSON 验证工具检查语法 |
1. 动态错误消息
根据不同条件生成不同的错误信息:
{{ $json.fieldType === 'email' ? '邮箱格式错误' : '字段验证失败' }}2. 错误对象中包含上下文数据
{
"errorCode": "VALIDATION_FAILED",
"failedField": "{{ $json.fieldName }}",
"receivedValue": "{{ $json.fieldValue }}",
"expectedFormat": "{{ $json.expectedFormat }}",
"timestamp": "{{ new Date().toISOString() }}"
}3. 多层级条件验证
使用多个 If 节点和 Stop And Error 节点实现阶梯式验证
每个验证失败都有独立的错误消息,便于问题追踪通过本教程,您已经学会了:
✓ Stop And Error 节点的基本概念和用途
✓ 两种错误类型的配置方法(Error Message 和 Error Object)
✓ 与 If 节点配合进行条件验证
✓ 搭建完整的数据验证工作流
✓ 与错误工作流的集成
现在您可以创建更加健壮和可靠的 n8n 工作流了!继续探索 n8n 的强大功能,让您的自动化流程更加智能。
[1] 官方文档: https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.stopanderror/
[2] n8n系列教程: https://www.undsky.com/blog/?category=n8n%E6%95%99%E7%A8%8B#