- 开始使用
- 构建
- 部署并运行
- 示例
- 参考
处理错误
将失败的节点路由到错误路径,记录错误详细信息,重试临时失败,并在出现不可恢复的错误时完全结束工作流。
您构建的内容
此类工作流可将故障节点路由到单独的路径,使用有用的上下文记录故障,在合理的情况下重试临时故障,并在错误无法恢复时干净结束。其结果是适合任何现有工作流的模式。
开始之前
- 至少包含一个可能失败的节点的现有工作流,并具有“错误处理”选项卡,例如“HTTP 请求”或“提取节点”。
使用的节点
步骤
编码智能体设置后,您可以跳过手动步骤。询问:
Add error handling to the HTTP Request node in this Maestro Automate workflow: connect its error path to a Script node that logs the error message, retry temporary failures, and end the run with a Terminate node when the error can't be recovered.
Add error handling to the HTTP Request node in this Maestro Automate workflow: connect its error path to a Script node that logs the error message, retry temporary failures, and end the run with a Terminate node when the error can't be recovered.
然后在画布上打开工作流,根据以下步骤进行检查并进行测试。“设置编码智能体”描述了相关设置。
1. 连接节点的错误句柄
默认情况下,故障节点会停止整个工作流。要改为处理故障:
- 在画布上打开工作流,然后选择可能失败的节点。
- 打开“错误处理”选项卡,然后启用“启用错误处理” 。主输出句柄下方显示“错误”句柄。
- 选择“错误”句柄上的“ +” ,搜索“脚本” ,然后选择它。
这将创建错误路径。当节点发生故障时,系统将遵循此路径运行,而不会停止工作流。没有“错误句柄”的节点不支持错误处理,若节点发生故障,工作流会停止,并显示在“故障”选项卡中。
2. 记录错误
在错误路径上的“脚本”节点中,读取$vars.<node>.error处的错误对象:
const error = $vars.httpRequest1.error;
console.log('Step failed:', error.message);
console.log('Code:', error.code, '| HTTP status:', error.status);
return { logged: true };
const error = $vars.httpRequest1.error;
console.log('Step failed:', error.message);
console.log('Code:', error.code, '| HTTP status:', error.status);
return { logged: true };
仅在错误路径上填充错误对象。“错误处理”页面列出了完整的错误字段集。
3. 重试暂时性失败(可选)
如果故障可能是暂时的,例如网络中断或速率限制,在放弃之前重试会有所帮助。“HTTP 请求”节点具有内置的重试功能:在其“高级”选项卡上设置“重试计数”可使其在触发错误句柄之前多次重新尝试请求。只有最终失败会路由到错误路径。
仅重试可以多次安全运行的操作。重试带有副作用(例如写入数据或发送消息)的节点会创建重复项。
4. 无法恢复时终止
在错误路径上的“脚本”节点后,添加“终止”节点。要返回故障描述,请在节点上选择“添加输出变量” ,并为其提供一个包含错误的值,例如$vars.httpRequest1.error.message 。
工作流在“终止”节点结束,该运行列在“监控运行”中。
5. 测试和调试
要有意触发错误路径:
- 请暂时将“HTTP 请求”节点中的 URL 设置为无效值(例如
https://this-will-fail.example.com)。 - Select Run Debug (▶) in the bottom toolbar of the canvas and verify that the execution trace follows the error path.
- 还原正确的 URL。
在追踪中检查脚本节点的控制台输出,以确认已记录错误详细信息。
结果
工作流将故障路由到错误路径,记录错误详细信息,可选择重试临时失败,然后以描述性故障状态干净结束。“HTTP 请求”节点中的 URL 无效,确认错误路径有效。
扩展此工作流
- Send an alert on failure: on the error path, before the Terminate node, add an HTTP Request or connector node to notify a Slack channel or create a ServiceNow incident.
- 继续而不是终止:如果步骤不关键,则让错误路径设置回退值并重新加入主路径,而不是终止。
- 广泛应用此模式: “错误处理”页面描述了更多模式,例如记录并继续、回退值和升级。