# Break flow after http request error

**URL:** <https://discourse.nodered.org/t/break-flow-after-http-request-error/51833>\
**Category:** General\
**Created:** [4 October 2021 12:24 UTC](https://discourse.nodered.org/t/break-flow-after-http-request-error/51833 "2021-10-04T12:24:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![loukaspe](https://avatars.discourse-cdn.com/v4/letter/l/da6949/32.png) [@loukaspe](https://discourse.nodered.org/u/loukaspe)\
**Post date:** [4 October 2021 12:24 UTC](https://discourse.nodered.org/t/break-flow-after-http-request-error/51833/1 "2021-10-04T12:24:41Z")

</div>

I have a simple flow (code and sceenshot provided):

1. an HTTP Input
2. an HTTP request (for which I want to handle errors)
3. an HTTP Response (status 200)
4. an Catch node along with an HTTP Response (status 500).

What I've noticed is that when an error is thrown from the HTTP Request, although it is caught in the catch node, the flow also continues. Which leads to two HTTP Responses (one with status 200 and one with status 500).

The result is that the client receives a response (500) and when the second one is returned, an error message "_Error [ERR\_HTTP\_HEADERS\_SENT]: Cannot set headers after they are sent to the client_" is logged in Nodered.

**Is it possible to break/stop the flow after an error is returned**?

Screenshot

 ![Selection_005](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/9/d90aa51accdc68c485f1978f4e9547654d15bab4.png)

Code:

```auto
[{"id":"d0c457e9.3cdf08","type":"tab","label":"Test","disabled":false,"info":""},{"id":"ffd7f970.35e278","type":"http in","z":"d0c457e9.3cdf08","name":"/causeError","url":"/causeError","method":"get","upload":false,"swaggerDoc":"","x":90,"y":160,"wires":[["753bc460.8f534c"]]},{"id":"753bc460.8f534c","type":"http request","z":"d0c457e9.3cdf08","name":"Throw error from failed http request","method":"GET","ret":"txt","paytoqs":"ignore","url":"http://serverdown","tls":"","persist":false,"proxy":"","authType":"","x":340,"y":160,"wires":[["f48a058f.ee0658","97422ab6.d61898"]]},{"id":"f48a058f.ee0658","type":"http response","z":"d0c457e9.3cdf08","name":"Return 200","statusCode":"200","headers":{},"x":650,"y":160,"wires":[]},{"id":"5d87987c.743c68","type":"catch","z":"d0c457e9.3cdf08","name":"Error Handler","scope":["753bc460.8f534c"],"uncaught":false,"x":90,"y":260,"wires":[["1e798ca5.4f13b3","df60da2c.860218"]]},{"id":"df60da2c.860218","type":"http response","z":"d0c457e9.3cdf08","name":"Return 500","statusCode":"500","headers":{},"x":270,"y":260,"wires":[]},{"id":"97422ab6.d61898","type":"debug","z":"d0c457e9.3cdf08","name":"Message from Regular Flow","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":530,"y":80,"wires":[]},{"id":"1e798ca5.4f13b3","type":"debug","z":"d0c457e9.3cdf08","name":"Message from Error Handler Flow","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":240,"y":340,"wires":[]}]

```

PS: This is my first post, please provide feedback if the post needs anything else.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [4 October 2021 12:29 UTC](https://discourse.nodered.org/t/break-flow-after-http-request-error/51833/2 "2021-10-04T12:29:08Z")

</div>

> [@loukaspe](#):
>
> What I've noticed is that when an error is thrown from the HTTP Request, although it is caught in the catch node, the flow also continues. Which leads to two HTTP Responses (one with status 200 and one with status 500).

The node should not be triggering both the Catch node and sending a normal response.

I can recreate what you are seeing - we'll need to fix that for the next release.

In the meantime, you could put a Switch node after the output of the HTTP Request node and check the value of `msg.statusCode` is valid

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [4 October 2021 12:47 UTC](https://discourse.nodered.org/t/break-flow-after-http-request-error/51833/3 "2021-10-04T12:47:48Z")

</div>

> [@knolleary](#):
>
> The node should not be triggering both the Catch node and sending a normal response.

Actually, I take that back.

The HTTP Request node has always done that - its behaviour pre-dates the existence of the Catch node. We couldn't change the behaviour without breaking flows.

So the Switch node check on `msg.statusCode` is the way foward.

---

<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:** [4 October 2021 13:50 UTC](https://discourse.nodered.org/t/break-flow-after-http-request-error/51833/4 "2021-10-04T13:50:27Z")

</div>

> [@knolleary](#):
>
> The HTTP Request node has always done that

Does the node support Complete yet? If so then that could be used to send the good response, as the node should only generate one of Complete and Catch.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [4 October 2021 13:55 UTC](https://discourse.nodered.org/t/break-flow-after-http-request-error/51833/5 "2021-10-04T13:55:08Z")

</div>

> [@Colin](#):
>
> Does the node support Complete yet? If so then that could be used to send the good respons

A good response is sent out of the node output just like every other node does.

---

<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:** [4 October 2021 14:53 UTC](https://discourse.nodered.org/t/break-flow-after-http-request-error/51833/6 "2021-10-04T14:53:25Z")

</div>

I meant does the HTTP request node trigger a Complete node when there is no error, and a Catch when there is an error? I don't think that Complete had been implemented when I last tried it, which was some time ago..

---

<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:** [4 October 2021 16:03 UTC](https://discourse.nodered.org/t/break-flow-after-http-request-error/51833/7 "2021-10-04T16:03:32Z")

</div>

Having tried a request to a non-existent url (NR 2.0.6) it appears to be triggering both Catch and Complete. That isn't correct behaviour is it? I thought nodes should always trigger one and only one of those two.

---

<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:** [3 December 2021 16:04 UTC](https://discourse.nodered.org/t/break-flow-after-http-request-error/51833/8 "2021-12-03T16:04:16Z")

</div>

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