karbassi / netatmo

A node.js module to hook into the netatmo API.
MIT License
57 stars 46 forks source link

Handling errors when requests can’t be fulfilled #4

Closed danilowanner closed 9 years ago

danilowanner commented 9 years ago

Hello @karbassi, @Nibbler999

I have small issue using the library in a continuously running application. Sometimes, for whatever reason, a request happens to fail (e.g. internet outage).

Currently this causes the app to crash. I created a fork and am trying to fix the issue.

First I have to fix all of these sections:

if (err || response.statusCode != 200) {
  console.log(body);
  this.emit("error", new Error("getMeasure error: " + response.statusCode));
  return this;
}

to check if response is in fact set, and an object containing a statusCode property. Otherwise a Cannot read property 'statusCode' of undefined type error is thrown.

Second I would like to remove the this.emit("error... statements and have a warning logged instead. (So that a single instance of a failed request does not crash the app.)

I am unsure what the correct way of doing so is. What is the intent of the original syntax? Why are the emitted errors thrown and show up in the console? Is there a way to emit, but still catch and handle the errors in my app?

I would appreciate your input!