Break flow after http request error

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

Code:

[{"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.

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

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.

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.

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

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..

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.

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