airapi icon indicating copy to clipboard operation
airapi copied to clipboard

Better error handling

Open rogchap opened this issue 9 years ago • 1 comments

request does not always have an err object when res.status !== 200

Using getInfo.js as an example:

request(requestConfigs, function(err, res, body) {
  if (!err && res.statusCode == 200) {
    resolve(JSON.parse(body));
  } else if (err) {
    reject(err);
  }
});

When calling this method with a listing ID that does not exist Airbnb return status = 404 with the body = {error_code:404, error: 'record_not_fond' ...} but err object in null.

Because of your "if else" statement neither resolve or reject is called... which is bad as this hangs and never returns.

Propose a change to something like:

request(requestConfigs, function(err, res, body) {
  if (res.statusCode == 200) {
    resolve(JSON.parse(body));
  } else {
    reject(JSON.parse(body));
  }
});

rogchap avatar Apr 06 '16 07:04 rogchap

@rogchap Thank you for pointing that out! I'll make the changes necessary. Keep 'em feedback coming! Much appreciated.

Si.

phamtrisi avatar Apr 06 '16 17:04 phamtrisi