• V
 

在出错后自动重试动作

问题

您希望在抛出错误后重试一个动作。

解决方案

使用 Catch 节点接收错误,并将其连接回需要重试该动作的节点。

示例

[{"id":"27e61f12.c1a15","type":"inject","z":"fc046f99.4be08","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":100,"y":320,"wires":[["d7d08440.31b678"]]},{"id":"d7d08440.31b678","type":"function","z":"fc046f99.4be08","name":"随机错误","func":"// 随机抛出一个错误而不是\n// 传递消息。\nif (Math.random() < 0.5) {\n   node.error(\"一个随机错误\", msg);\n} else {\n    return msg;\n}","outputs":1,"noerr":0,"x":320,"y":320,"wires":[["f22b1e9a.5d89b"]]},{"id":"f22b1e9a.5d89b","type":"debug","z":"fc046f99.4be08","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":510,"y":320,"wires":[]},{"id":"2166290d.98d736","type":"delay","z":"fc046f99.4be08","name":"","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":240,"y":380,"wires":[["d7d08440.31b678"]]},{"id":"139b836e.7950ed","type":"catch","z":"fc046f99.4be08","name":"","scope":["d7d08440.31b678"],"uncaught":false,"x":90,"y":380,"wires":[["2166290d.98d736","9c8ab214.0ecaa"]]},{"id":"9c8ab214.0ecaa","type":"debug","z":"fc046f99.4be08","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"error","targetType":"msg","x":240,"y":440,"wires":[]}]

讨论

有些错误是短暂的,执行一个动作只需重新尝试即可成功。 另外,在重试之前可能需要一些补救措施。

在示例流程中,Function 节点模拟了一个随机错误 - 它有50%的概率会抛出错误而不是传递消息。

Catch 节点接收错误,并将消息传递回 Function 节点以进行重试。它还包括一个 Delay 节点,因为在某些情况下,重试之前等待一个短暂的间隔是合适的。