luisiam / homebridge-liftmaster2

LiftMaster Plugin for HomeBridge (API 2.0): https://github.com/nfarina/homebridge
61 stars 15 forks source link

Unhandled error in promise #30

Open jhurliman opened 6 years ago

jhurliman commented 6 years ago
Aug 18 08:10:51 raspberrypi homebridge[15911]: (node:15911) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): FetchError: invalid json response body at https://myqexternal.myqdevice.com/api/v4/UserDeviceDetails/Get reason: Unexpected token < in JSON at position 0
luisiam commented 6 years ago

That should only happen when the MyQ server is no responsive

jhurliman commented 6 years ago

RIght. The promise chain needs an error handler on the end of it for that situation. In future versions of Node, an unhandled exception in a promise will terminate the process.

tonym97 commented 6 years ago

Is there a reason you just don't do this to prevent the crash?

fetch("https://myqexternal.myqdevice.com/api/v4/User/Validate", { method: "POST", headers: HEADERS, body: JSON.stringify(body) }).then(function (res) { return res.json(); }, function (error) { return { ReturnCode: 1, ErrorMessage: 'Unable to Validate User' } }).then(function (data) { // Check for MyQ Error Codes if (data.ReturnCode === "0") { self.securityToken = data.SecurityToken; self.manufacturer = "Chamberlain"; self.getDevice(callback); } else { self.log(data.ErrorMessage); callback(data.ErrorMessage);
} });