# How to retry a single message for x times should it encounter an error?

**URL:** <https://discourse.nodered.org/t/how-to-retry-a-single-message-for-x-times-should-it-encounter-an-error/70652>\
**Category:** General\
**Created:** [15 November 2022 13:50 UTC](https://discourse.nodered.org/t/how-to-retry-a-single-message-for-x-times-should-it-encounter-an-error/70652 "2022-11-15T13:50:28Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/josh/32/62208_2.png) [@Josh](https://discourse.nodered.org/u/Josh)\
**Post date:** [15 November 2022 13:50 UTC](https://discourse.nodered.org/t/how-to-retry-a-single-message-for-x-times-should-it-encounter-an-error/70652/1 "2022-11-15T13:50:28Z")

</div>

Hi Forum,  
I am working on a small project where I have to retry each message coming from an mqtt-in node for three times should the message encounter an error. I tried using catch node to catch the error but couldn't retry the error message for 3 times. Any help is appreciated !!!

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [15 November 2022 14:17 UTC](https://discourse.nodered.org/t/how-to-retry-a-single-message-for-x-times-should-it-encounter-an-error/70652/2 "2022-11-15T14:17:23Z")

</div>

What sort of error do you mean?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [15 November 2022 14:23 UTC](https://discourse.nodered.org/t/how-to-retry-a-single-message-for-x-times-should-it-encounter-an-error/70652/3 "2022-11-15T14:23:02Z")

</div>

if you can catch the error with a catch node then you can use `msg.error.source.count` and a switch node to allow three retries

e.g.

```auto
[{"id":"74013b85c3dbbf1f","type":"inject","z":"452103ea51141731","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":240,"y":3680,"wires":[["aa31269ca6e7b385"]]},{"id":"aa31269ca6e7b385","type":"function","z":"452103ea51141731","name":"function 17","func":"node.error(\"testerror\",msg)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":390,"y":3680,"wires":[["3100195536aa61cc"]]},{"id":"a74c2f591f18c947","type":"delay","z":"452103ea51141731","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":520,"y":3760,"wires":[["aa31269ca6e7b385"]]},{"id":"3100195536aa61cc","type":"debug","z":"452103ea51141731","name":"debug 107","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":570,"y":3680,"wires":[]},{"id":"f8f03081edea7cf5","type":"switch","z":"452103ea51141731","name":"","property":"error.source.count","propertyType":"msg","rules":[{"t":"lt","v":"3","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":390,"y":3800,"wires":[["0a4f503a989355f6","a74c2f591f18c947"]]},{"id":"544ad9ac0419193d","type":"catch","z":"452103ea51141731","name":"","scope":["aa31269ca6e7b385"],"uncaught":false,"x":270,"y":3800,"wires":[["f8f03081edea7cf5"]]},{"id":"0a4f503a989355f6","type":"debug","z":"452103ea51141731","name":"debug 108","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":530,"y":3800,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [15 November 2022 14:28 UTC](https://discourse.nodered.org/t/how-to-retry-a-single-message-for-x-times-should-it-encounter-an-error/70652/4 "2022-11-15T14:28:34Z")

</div>

I don't see how what you describe makes sense with an MQTT In node, which is why I ask what sort of error the MQTT node is generating.

---

<div class="post-metadata">

**Author:** ![Josh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/josh/32/62208_2.png) [@Josh](https://discourse.nodered.org/u/Josh)\
**Post date:** [15 November 2022 15:38 UTC](https://discourse.nodered.org/t/how-to-retry-a-single-message-for-x-times-should-it-encounter-an-error/70652/5 "2022-11-15T15:38:38Z")

</div>

Hi @E1cid Thanks for the solution.  
Now your retry mechanism seems to work as expected.

---

<div class="post-metadata">

**Author:** ![Josh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/josh/32/62208_2.png) [@Josh](https://discourse.nodered.org/u/Josh)\
**Post date:** [15 November 2022 15:41 UTC](https://discourse.nodered.org/t/how-to-retry-a-single-message-for-x-times-should-it-encounter-an-error/70652/6 "2022-11-15T15:41:45Z")

</div>

I have a postgres node which throws an error message but in the catch node the payload attribute has both error and payload data merged. Trying to seperate the payload with the help of flow context.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [15 November 2022 15:43 UTC](https://discourse.nodered.org/t/how-to-retry-a-single-message-for-x-times-should-it-encounter-an-error/70652/7 "2022-11-15T15:43:40Z")

</div>

Oh, I thought you said it was MQTT that was causing the problem. So the fact that the messages come from MQTT is completely irrelevant.

---

<div class="post-metadata">

**Author:** ![Josh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/josh/32/62208_2.png) [@Josh](https://discourse.nodered.org/u/Josh)\
**Post date:** [16 November 2022 09:45 UTC](https://discourse.nodered.org/t/how-to-retry-a-single-message-for-x-times-should-it-encounter-an-error/70652/8 "2022-11-16T09:45:47Z")

</div>

Yes it is. I was using the mqtt-in node to get trigger.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [16 November 2022 16:32 UTC](https://discourse.nodered.org/t/how-to-retry-a-single-message-for-x-times-should-it-encounter-an-error/70652/9 "2022-11-16T16:32:15Z")

</div>

Whether the message came from mqtt or somewhere else is irrelevant.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [30 November 2022 16:33 UTC](https://discourse.nodered.org/t/how-to-retry-a-single-message-for-x-times-should-it-encounter-an-error/70652/10 "2022-11-30T16:33:01Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
