From 92e690cba59326c5727cb54b6254d205bf58e63a Mon Sep 17 00:00:00 2001 From: Carlos Fernandez Garcia Date: Sun, 22 Mar 2015 22:25:21 +0000 Subject: [PATCH] Erroring when the request gets an error or an status code greater or equals than 400 --- lib/runkeeper.js | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/lib/runkeeper.js b/lib/runkeeper.js index dd73d29..881d70a 100644 --- a/lib/runkeeper.js +++ b/lib/runkeeper.js @@ -26,7 +26,7 @@ var API = { }; var HealthGraph = exports.HealthGraph = function(options) { - + this.client_id = options.client_id || null ; this.client_secret = options.client_secret || null; this.auth_url = options.auth_url || "https://runkeeper.com/apps/authorize"; @@ -51,21 +51,22 @@ HealthGraph.prototype.getNewToken = function (authorization_code, callback) { client_secret: this.client_secret, redirect_uri: this.redirect_uri }; - + var paramlist = []; for (pk in request_params) { paramlist.push(pk + "=" + request_params[pk]); }; var body_string = paramlist.join("&"); - - var request_details = { + + var request_details = { method: "POST", headers: {'content-type' : 'application/x-www-form-urlencoded'}, uri: this.access_token_url, body: body_string }; - + request(request_details, function(error, response, body) { + if(error || response.statusCode >= 400) return callback(error || 'Response status code' + response.statusCode); callback(error, JSON.parse(body)['access_token']); }); }; @@ -83,6 +84,7 @@ HealthGraph.prototype.apiCall = function(method, media_type, endpoint, callback) uri: "https://" + this.api_domain + endpoint }; request(request_details, function(error, response, body) { + if(error || response.statusCode >= 400) return callback(error || 'Response status code' + response.statusCode); var parsed; try { parsed = JSON.parse(body); @@ -106,7 +108,8 @@ for (func_name in API) { uri: "https://" + this.api_domain + API[func_name]['uri'] }; request(request_details, - function(error, response, body) { + function(error, response, body) { + if(error || response.statusCode >= 400) return callback(error || 'Response status code' + response.statusCode); var parsed; try { parsed = JSON.parse(body); @@ -117,10 +120,6 @@ for (func_name in API) { callback(error, parsed); } }); - }; + }; })(func_name); }; - - - -