naofireblade / homebridge-weather-plus

A comprehensive weather plugin for homebridge.
MIT License
314 stars 63 forks source link

Need a try catch in the new WunderunderGroundAPI so homekit can proceed gracefully. #78

Closed knuckleheadsmiff closed 4 years ago

knuckleheadsmiff commented 4 years ago

Saw a crash today (sadly not detailed log info other then the line number that failed.) for the first time when trying to pass a JSON result. Causes a fatal error to HomeKit. Just need to add a try catch to make it handle itself better: Aroun line 45 should read something like:

                try {
            const jsonObj = JSON.parse(body);
                    debug(JSON.stringify(jsonObj, null, 2));

                    weather.report = that.parseReport(jsonObj);
                    callback(null, weather);
        } catch (e) {
                    debug("Error retrieving weather report and forecast");
                    debug("Error Message: " + e);
                    callback(e);
        }

So that if the JSON.parse fails the code can gracefully continue along. (I though the code I originally pushed had this but I got rid of my project and can't com[are. Regardless this should be added.

FYI: This was the log output:

[10/10/2019, 12:30:20 PM] SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at WundergroundAPI.<anonymous> (/usr/local/lib/node_modules/homebridge-weather-plus/api/weatherunderground.js:45:38)
    at Request.self.callback (/usr/local/lib/node_modules/homebridge-weather-plus/node_modules/request/request.js:185:22)
    at Request.emit (events.js:198:13)
    at Request.<anonymous> (/usr/local/lib/node_modules/homebridge-weather-plus/node_modules/request/request.js:1161:10)
    at Request.emit (events.js:198:13)
    at IncomingMessage.<anonymous> (/usr/local/lib/node_modules/homebridge-weather-plus/node_modules/request/request.js:1083:12)
    at Object.onceWrapper (events.js:286:20)
    at IncomingMessage.emit (events.js:203:15)
    at endReadableNT (_stream_readable.js:1145:12)
    at process._tickCallback (internal/process/next_tick.js:63:19)
knuckleheadsmiff commented 4 years ago

Nope, did not have that try catch in the code I submitted, sorry about that. can you add it?

naofireblade commented 4 years ago

Fixed and published in v2.8.2.

knuckleheadsmiff commented 4 years ago

FYI, not that it matters since it is offline but the yahoo API has a similar bug (I used that as an initial template.)