How to stop http response

I use http in node to insert data into PotgreSQL table, in case of inserting data fails, such as the wrong column name, ... Node-RED side will make an error in postgres node

while the browser side will still run without ending...

I would like when an error occurs with postgres node, the browser side will display an error message such as insert data failed and end the response, is it possible?
I tried using catch node to do this but it failed.

My flow:

[{"id":"7624b06e.61b2c","type":"http in","z":"1c4ec4d8.181d5b","name":"","url":"/insert","method":"get","upload":false,"swaggerDoc":"","x":2040,"y":220,"wires":[["1118246a.d4c26c"]]},{"id":"80cbeb16.13c6a8","type":"postgres","z":"1c4ec4d8.181d5b","postgresdb":"795c7d9.88a6f84","name":"","output":true,"outputs":1,"x":2360,"y":220,"wires":[["c701dbbd.2a9b78","7128a876.90b3f8"]]},{"id":"1118246a.d4c26c","type":"template","z":"1c4ec4d8.181d5b","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"INSERT INTO public.\"tblTest\"(fileName) VALUES ('File1.csv');","output":"str","x":2200,"y":220,"wires":[["80cbeb16.13c6a8"]]},{"id":"c701dbbd.2a9b78","type":"http response","z":"1c4ec4d8.181d5b","name":"http response","statusCode":"","headers":{},"x":2560,"y":220,"wires":[]},{"id":"7128a876.90b3f8","type":"debug","z":"1c4ec4d8.181d5b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":2530,"y":260,"wires":[]},{"id":"124b177c.78c229","type":"catch","z":"1c4ec4d8.181d5b","name":"","scope":["80cbeb16.13c6a8"],"uncaught":false,"x":2030,"y":120,"wires":[["54a009fc.b5e728"]]},{"id":"51f4bfc6.714a1","type":"debug","z":"1c4ec4d8.181d5b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":2550,"y":120,"wires":[]},{"id":"54a009fc.b5e728","type":"change","z":"1c4ec4d8.181d5b","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"Failed to insert data.","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":2200,"y":120,"wires":[["51f4bfc6.714a1","c701dbbd.2a9b78"]]},{"id":"795c7d9.88a6f84","type":"postgresdb","z":"","hostname":"localhost","port":"5432","db":"mydb","ssl":false}]

If possible, please tell me.
Thanks!

Postgress is returning a msg string in json format. You will need to parse it to an object, then check for error properties. If they exist return the error you require in msg.payload and feed it in to response node, else return the original msg.

An example using the change node, one inject feeds a json string with no error property, the other inject has an error property. The change node converts the string to an object, then if error exists returns a preset error message.

[{"id":"2f271aa4.dd0926","type":"inject","z":"7760f563.ea0324","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"1\":1,\"2\":2}","payloadType":"str","x":210,"y":560,"wires":[["e7c07a2e.cb71d"]]},{"id":"cf5b3239.506f5","type":"inject","z":"7760f563.ea0324","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"1\":1,\"error\":\"input fail\",\"2\":2}","payloadType":"str","x":219.3333282470703,"y":492.3333435058594,"wires":[["e7c07a2e.cb71d"]]},{"id":"e7c07a2e.cb71d","type":"change","z":"7760f563.ea0324","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"($eval(payload).error = \"input fail\" ? \"this error\" : payload)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":459.3333282470703,"y":492.3333435058594,"wires":[["b7b75c67.b4beb"]]},{"id":"b7b75c67.b4beb","type":"debug","z":"7760f563.ea0324","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":650,"y":480,"wires":[]}]
1 Like

I tried it but the browser side still runs without ending.

[{"id":"398e4f84.af26","type":"http in","z":"1c4ec4d8.181d5b","name":"","url":"/insert","method":"get","upload":false,"swaggerDoc":"","x":680,"y":980,"wires":[["e4b8f2b3.94c7c"]]},{"id":"848a23cd.f1c0a","type":"postgres","z":"1c4ec4d8.181d5b","postgresdb":"795c7d9.88a6f84","name":"","output":true,"outputs":1,"x":1060,"y":980,"wires":[["74fd3e45.f2d77","b2f60da5.d6e3e"]]},{"id":"e4b8f2b3.94c7c","type":"template","z":"1c4ec4d8.181d5b","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"INSERT INTO public.\"tblTest\"(fileName) VALUES ('File1.csv');","output":"str","x":880,"y":980,"wires":[["848a23cd.f1c0a"]]},{"id":"b2f60da5.d6e3e","type":"http response","z":"1c4ec4d8.181d5b","name":"http response","statusCode":"","headers":{},"x":1460,"y":980,"wires":[]},{"id":"74fd3e45.f2d77","type":"debug","z":"1c4ec4d8.181d5b","name":"postgres","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1440,"y":1040,"wires":[]},{"id":"8a0f8e.0234d07","type":"catch","z":"1c4ec4d8.181d5b","name":"","scope":["848a23cd.f1c0a"],"uncaught":false,"x":670,"y":860,"wires":[["30fe29d3.14fe46"]]},{"id":"9b99c3e0.41f69","type":"debug","z":"1c4ec4d8.181d5b","name":"catch","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1430,"y":860,"wires":[]},{"id":"30fe29d3.14fe46","type":"change","z":"1c4ec4d8.181d5b","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"($eval(error.message).error = {\"name\":\"error\",\"length\":191,\"severity\":\"ERROR\",\"code\":\"42P01\",\"position\":\"13\",\"file\":\"d:\\\\pginstaller_12.auto\\\\postgres.windows-x64\\\\src\\\\backend\\\\parser\\\\parse_relation.c\",\"line\":\"1173\",\"routine\":\"parserOpenTable\"} ? \"this error\" : payload)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":920,"y":860,"wires":[["9b99c3e0.41f69","b2f60da5.d6e3e"]]},{"id":"8625c042.62ce5","type":"change","z":"1c4ec4d8.181d5b","name":"","rules":[{"t":"set","p":"headers","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1230,"y":920,"wires":[["b2f60da5.d6e3e"]]},{"id":"d8b019d4.d386a8","type":"change","z":"1c4ec4d8.181d5b","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"($eval(error.message).error = {\"name\":\"error\",\"length\":191,\"severity\":\"ERROR\",\"code\":\"42P01\",\"position\":\"13\",\"file\":\"d:\\\\pginstaller_12.auto\\\\postgres.windows-x64\\\\src\\\\backend\\\\parser\\\\parse_relation.c\",\"line\":\"1173\",\"routine\":\"parserOpenTable\"} ? \"this error\" : payload)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1240,"y":1100,"wires":[["b2f60da5.d6e3e"]]},{"id":"795c7d9.88a6f84","type":"postgresdb","z":"","hostname":"localhost","port":"5432","db":"DataBase","ssl":false}]

I think the msg object that flows into a http-response node must come from a http-in node,
but when an error occurs at postgres node the msg object is not forwarded to http-response node behind it,
so http-response was not completed so the browser side is still running and not ending?

That is due to the Postgres node, it returned the error string to msg, not msg.payload or msg.error. So all the required properties have been overwitten.

msg.res and msg.req need to be preserved for the response node.

I would need a debug output from a
success postgres and an unsuccessful, to say more.

1 Like

Below are the results of 2 cases:

{"type":"postgres error","request":"INSERT INTO public.\"tblTest\"(fileName) VALUES ('File1.csv');","error":{"name":"error","length":191,"severity":"ERROR","code":"42P01","position":"13","file":"d:\\pginstaller_12.auto\\postgres.windows-x64\\src\\backend\\parser\\parse_relation.c","line":"1173","routine":"parserOpenTable"}}
{"_msgid":"5d0cc7ad.1d0298","payload":[],"queryParameters":{},"req":{"_readableState":{"objectMode":false,"highWaterMark":16384,"buffer":{"head":null,"tail":null,"length":0},"length":0,"pipes":null,"pipesCount":0,"flowing":null,"ended":true,"endEmitted":false,"reading":false,"sync":true,"needReadable":false,"emittedReadable":false,"readableListening":false,"resumeScheduled":false,"emitClose":true,"autoDestroy":false,"destroyed":false,"defaultEncoding":"utf8","awaitDrainWriters":null,"multiAwaitDrain":false,"readingMore":true,"decoder":null,"encoding":null},"readable":true,"_events":{},"_eventsCount":1,"socket":"[internal]","connection":"[internal]","httpVersionMajor":1,"httpVersionMinor":1,"httpVersion":"1.1","complete":true,"headers":{"host":"localhost:1880","connection":"keep-alive","cache-control":"max-age=0","upgrade-insecure-requests":"1","user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36 Edg/85.0.564.70","accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9","sec-fetch-site":"none","sec-fetch-mode":"navigate","sec-fetch-user":"?1","sec-fetch-dest":"document","accept-encoding":"gzip, deflate, br","accept-language":"ja,en;q=0.9,en-GB;q=0.8,en-US;q=0.7"},"rawHeaders":["Host","localhost:1880","Connection","keep-alive","Cache-Control","max-age=0","Upgrade-Insecure-Requests","1","User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36 Edg/85.0.564.70","Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9","Sec-Fetch-Site","none","Sec-Fetch-Mode","navigate","Sec-Fetch-User","?1","Sec-Fetch-Dest","document","Accept-Encoding","gzip, deflate, br","Accept-Language","ja,en;q=0.9,en-GB;q=0.8,en-US;q=0.7"],"trailers":{},"rawTrailers":[],"aborted":false,"upgrade":false,"url":"/insert","method":"GET","statusCode":null,"statusMessage":null,"client":"[internal]","_consuming":false,"_dumped":false,"baseUrl":"","originalUrl":"/insert","_parsedUrl":{"protocol":null,"slashes":null,"auth":null,"host":null,"port":null,"hostname":null,"hash":null,"search":null,"query":null,"pathname":"/insert","path":"/insert","href":"/insert","_raw":"/insert"},"params":{},"query":{},"res":"[internal]","body":{},"_parsedOriginalUrl":{"protocol":null,"slashes":null,"auth":null,"host":null,"port":null,"hostname":null,"hash":null,"search":null,"query":null,"pathname":"/insert","path":"/insert","href":"/insert","_raw":"/insert"},"route":{"path":"/insert","stack":[{"name":"cookieParser","keys":[],"regexp":{"fast_star":false,"fast_slash":false},"method":"get"},{"name":"httpMiddleware","keys":[],"regexp":{"fast_star":false,"fast_slash":false},"method":"get"},{"name":"corsHandler","keys":[],"regexp":{"fast_star":false,"fast_slash":false},"method":"get"},{"name":"metricsHandler","keys":[],"regexp":{"fast_star":false,"fast_slash":false},"method":"get"},{"name":"<anonymous>","keys":[],"regexp":{"fast_star":false,"fast_slash":false},"method":"get"},{"name":"<anonymous>","keys":[],"regexp":{"fast_star":false,"fast_slash":false},"method":"get"}],"methods":{"get":true}},"cookies":{},"signedCookies":{}},"res":{}}

This means that before run postgres node, I need a backup of 2 properties msg.res and msg.req!?
...but when there is an error at postgres node then all the required properties have been overwitten :frowning:

edit/
yes
, msg.res and msg.req save them
and just before the response node add them back to the msg

1 Like

Thank you for your enthusiastic support!!!
I have done it. :sweat_smile:

Glad to help.
After re-reading the info notes , i think you only need to preserve msg.res.

1 Like

Thanks to your support, I have learned a lot more...