ottopaulsen / node-red-contrib-power-saver

A Node-RED node to saver money by turning off when the power is most expensive
Other
70 stars 17 forks source link

Node-Red crashes if Elvia returns invalid json #110

Closed shadowano closed 1 year ago

shadowano commented 1 year ago

This is a serious bug, causing Node-Red to crash everytime the node runs.

This is the log from Node-Rd in Home Assistant:

10 Nov 10:58:16 - [info] Updated flows 10 Nov 10:58:16 - [info] Starting modified flows 10 Nov 10:58:16 - [info] Started modified flows 10 Nov 10:58:25 - [red] Uncaught Exception: 10 Nov 10:58:25 - [error] FetchError: invalid json response body at https://elvia.azure-api.net/grid-tariff/digin/api/1.0/tariffquery?TariffKey=private_flatrate_house&StartTime=2022-11-09T23:00:00&EndTime=2022-11-10T23:00:00 reason: Unexpected end of JSON input at /config/node-red/node_modules/node-fetch/lib/index.js:273:32 at processTicksAndRejections (node:internal/process/task_queues:96:5) [10:58:26] WARNING: Node-RED crashed, halting add-on [10:58:26] INFO: Node-RED stopped, restarting... s6-rc: info: service legacy-services: stopping [10:58:26] INFO: Node-RED stopped, restarting... s6-svwait: fatal: supervisor died s6-rc: info: service legacy-services successfully stopped s6-rc: info: service legacy-cont-init: stopping s6-rc: info: service legacy-cont-init successfully stopped s6-rc: info: service fix-attrs: stopping s6-rc: info: service fix-attrs successfully stopped s6-rc: info: service s6rc-oneshot-runner: stopping s6-rc: info: service s6rc-oneshot-runner successfully stopped

this is the respons when using Elvias test API for the same url:

Response status 500 Internal Server Error Response latency 17830 ms

Yes, Elvia has a server issue but the node is not recovering from it. This causes Node-Red to go down.

ottopaulsen commented 1 year ago

What version are you using?

shadowano commented 1 year ago

I'm using

ottopaulsen commented 1 year ago

Hmm. I thought I had fixed that. Vould hou provide a flow with a minimum to reproduce it?

shadowano commented 1 year ago

Hmm. I thought I had fixed that. Vould hou provide a flow with a minimum to reproduce it?

Try to change elvia url to something that gives you a 500 internal error. Elvia reported the error as solved. Edit: https://elvia.portal.azure-api.net/Issues

shadowano commented 1 year ago

I just checked the Elvia code quickly (so I might have missed something), if server returns 500 then there is no Json returned. Wouldn't this line then fail? https://github.com/ottopaulsen/node-red-contrib-power-saver/blob/main/src/elvia/elvia-api.js#L41

ottopaulsen commented 1 year ago

You're right, it will fail.

ottopaulsen commented 1 year ago

I think this is at least better in version 4.0.0. Please try.

shadowano commented 1 year ago

Thanks, i see you are checking for 500 error explicitly, shouldn't there be an else check on line 40 to catch all errors, and prevent node-red crash? Or at least check if json is valid before exception is thrown?

Btw, great work on all the nodes you have created! Keep it up:)

ottopaulsen commented 1 year ago

A fix is ready for next release.

corvy commented 1 year ago

I think this issue could be closed :)