mylesagray / homebridge-blueair

BlueAir air purifier plugin for homebridge
Apache License 2.0
39 stars 20 forks source link

plugin and homebridge crashes on foobot.io api failure #22

Closed YuAo closed 5 years ago

YuAo commented 5 years ago

Sometime the foobot.io api returns non json strings. It causes the plugin crash.

<!DOCTYPE html><html><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewpo
rt" content="width=device-width, initial-scale=1"><title>503 Service Unavailable</title><style type="text/css">body{marg
in:5% auto 0 auto;padding:0 18px}.P{margin:0 22%}.O{margin-top:20px}.N{margin-top:10px}.M{margin:10px 0 30px 0}.L{margin
-bottom:60px}.K{font-size:25px;color:#F90}.J{font-size:14px}.I{font-size:20px}.H{font-size:18px}.G{font-size:16px}.F{wid
th:230px;float:left}.E{margin-top:5px}.D{margin:8px 0 0 -20px}.C{color:#3CF;cursor:pointer}.B{color:#909090;margin-top:1
5px}.A{line-height:30px}.hide_me{display:none}</style></head><body><div id="p" class="P"><div class="K">503</div><div cl
ass="O I">Service Unavailable</div><p class="J A L">Error Times: Sat, 13 Apr 2019 06:43:59 GMT<br><span class="F">IP: ****</span>Node information:tb74:3<br>URL: https://api-cn-north-1.foobot.io/v2/device/*******/info/<br
><span class="hide_me">Please contact our support                                                                       

SyntaxError: Unexpected token < in JSON at position 0                                                                   
    at JSON.parse (<anonymous>)                                                                                         
    at BlueAir.<anonymous> (/usr/local/lib/nodejs/node-v10.15.0/lib/node_modules/homebridge-blueair/index.js:442:27)    
    at BlueAir.<anonymous> (/usr/local/lib/nodejs/node-v10.15.0/lib/node_modules/homebridge-blueair/index.js:264:6)     
    at Request.self.callback (/usr/local/lib/nodejs/node-v10.15.0/lib/node_modules/homebridge-blueair/node_modules/reque
st/request.js:185:22)                                                                                                   
    at Request.emit (events.js:182:13)                                                                                  
    at Request.<anonymous> (/usr/local/lib/nodejs/node-v10.15.0/lib/node_modules/homebridge-blueair/node_modules/request
/request.js:1161:10)                                                                                                    
    at Request.emit (events.js:182:13)                                                                                  
    at IncomingMessage.<anonymous> (/usr/local/lib/nodejs/node-v10.15.0/lib/node_modules/homebridge-blueair/node_modules
/request/request.js:1083:12)                                                                                            
    at Object.onceWrapper (events.js:273:13)                                                                            
    at IncomingMessage.emit (events.js:187:15)                     

Maybe we should add some protection around JSON.parse(...) ?

mylesagray commented 5 years ago

@YuAo good find, I've never experienced this myself - but you're right, we should definitely add some protection there.

I'll take look into what can be implemented.

mylesagray commented 5 years ago

Added new tryParseJSON function and use that as a wrapper for all JSON calls to catch non-JSON response bodies.

Published to NPM as v0.4.2

Closing