robertklep / nefit-easy-commands

High-level command implementation for Nefit Easy™ clients.
MIT License
13 stars 4 forks source link

"Catch" doesn't catch all exceptions #9

Open christofkac opened 4 years ago

christofkac commented 4 years ago

I'm using your library successfully, really thanks for the work. I use it in an ioBroker (homeautomation server) environment which is JavaScript based.

My problem is, that from time to time I get an uncaugt exception which crashes the JavaScript engine in ioBroker. I based my code on your example:

function ReadValues() { // Instantiate client const client = NefitEasyClient({ serialNumber : "xx", accessKey : "yy", password : "zz", }); // Connect client and retrieve status and pressure. client.connect().then( () => { return Promise.all([ client.status(), client.pressure() ]); }).then(response => { const status = response[0]; console.log(JSON.stringify(status)); const pressure = response[1]; console.log("Temperature is set to " + status['temp setpoint'].toFixed(1) + " °C, current is " + status['in house temp'].toFixed(1) + " °C.\n" + "Outside temperature is "+status['outdoor temp'].toFixed(1)+" °C.\n" + "User Mode is "+status['user mode']+".\n" + "System pressure is " + pressure.pressure + " " + pressure.unit); }).catch((e) => { console.error(e); }).finally(() => {
client.end(); }); }

My output:

2020-01-02 09:12:01.314 - info: javascript.0 (28942) script.js.common.Nefit: {"user mode":"clock","clock program":"auto","in house status":"ok","in house temp":19.6,"hot water active":true,"boiler indicator":"central heating","control":"room","temp override duration":0,"current switchpoint":24,"ps active":false,"powersave mode":false,"fp active":false,"fireplace mode":false,"temp override":false,"holiday mode":false,"boiler block":null,"boiler lock":null,"boiler maintenance":null,"temp setpoint":21,"temp override temp setpoint":17,"temp manual setpoint":21,"hed enabled":null,"hed device at home":null,"outdoor temp":3,"outdoor source type":"virtual"} 2020-01-02 09:12:01.314 - info: javascript.0 (28942) script.js.common.Nefit: Temperature is set to 21.0 °C, current is 19.6 °C. Outside temperature is 3.0 °C. User Mode is clock. System pressure is 1.8 bar 2020-01-02 09:12:01.342 - error: javascript.0 (28942) uncaught exception: read ECONNRESET 2020-01-02 09:12:01.343 - error: javascript.0 (28942) Error: read ECONNRESET 2020-01-02 09:12:01.963 - error: host.ioBroker Caught by controller[0]: { Error: read ECONNRESET

2020-01-02 09:12:01.964 - error: host.ioBroker Caught by controller[0]: at TLSWrap.onread (net.js:622:25) 2020-01-02 09:12:01.964 - error: host.ioBroker Caught by controller[0]: errno: 'ECONNRESET', code: 'ECONNRESET', syscall: 'read' }

So although the readout was successful, the exception ECONNRESET is thrown and unfortunately not caught be the "catch" statement.

Any hint? Thanks Christof

robertklep commented 4 years ago

If client.end() throws an exception, it won't get caught.

You could try this:

…
}).finally(() => {
  client.end();
}).catch(e => {})

But I'm not entirely sure where the error is coming from, so there might be another cause for it.

christofkac commented 4 years ago

Thanks, I'll give it a try and come back once this happens again. I added a logging into the new catch() block to get noticed.