diff --git a/lib/api-request.js b/lib/api-request.js index a1dbad4..8d2f8ce 100644 --- a/lib/api-request.js +++ b/lib/api-request.js @@ -10,9 +10,23 @@ module.exports = { return; } if (response.statusCode >= 200 && response.statusCode < 300) { - cb(null, JSON.parse(body)); + var json; + try { + json = JSON.parse(body); + } catch (ex) { + return cb(new Error('Unparseable response')); + } + cb(null, json); } else { - cb(new Error(JSON.parse(body).error.message)); + var json; + try { + json = JSON.parse(body); + } catch (ex) { + return cb(new Error('Unparseable response')); + } + if (!json||!json.error||!json.error.message) + return cb(new Error('Unknown error')); + cb(new Error(json.error.message)); } } const requestOptions = {