christianbundy/node-hacker-news-api — pull request #2 by euank (2014-05-18 18:48:45+00:00)
Title: Handle invalid json

If a hacker news item doesn't exist, the api returns a 404 as well as invalid json which looks something like `GET https://UJ5WYC0L7X-2.algolia.io/1/indexes/Item_production/7?params=advancedSyntax%3Dtrue%26analytics%3Dfalse: {"message":"ObjectID does not exist"}`. Your current code attempts to run JSON.parse even if statusCode != 200, and thus fails on this case. I think that doing a try-catch there is appropriate. If the resulting body was not parsed correctly, error will always be set.

The minimal failing example is:
```
require('hacker-news-api').getItem(7, console.log);
```
since the item with id '7' was deleted or is otherwise not present.