From 901b71a711f9f7faa963f051b9416f3f24a8fc53 Mon Sep 17 00:00:00 2001 From: Kamil Burzynski Date: Thu, 5 Nov 2015 12:03:31 +0100 Subject: [PATCH 1/4] Added support for 439 status code (too many requests) --- lib/utils.js | 65 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 41 insertions(+), 24 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 24b1f87..578b3ca 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -13,13 +13,13 @@ var http = require('http'), module.exports.request = function(options) { return new Promise(function(resolve, reject) { - var opts = { service: 'apigateway', region: options.region, method: options.method, path: options.path }; + if (options.body) opts.body = JSON.stringify(options.body); opts = aws4.sign(opts, { @@ -27,42 +27,59 @@ module.exports.request = function(options) { secretAccessKey: options.secretAccessKey }); - var req = https.request(opts, function(res) { + var request = function () { + return new Promise(function(resolve, reject){ + var req = https.request(opts, function(res) { + var body = ''; - var body = ''; + res.on('data', function(d) { + body += d; + }); - res.on('data', function(d) { - body += d; - }); + res.on('end', function() { + resolve({ + "statusCode": res.statusCode, + "body": JSON.parse(body) + }); + }); + }); - res.on('end', function() { + req.on('error', function(e) { + // General error, i.e. + // - ECONNRESET - server closed the socket unexpectedly + // - ECONNREFUSED - server did not listen + // - HPE_INVALID_VERSION + // - HPE_INVALID_STATUS + reject(e); + }); + if (options.body) req.write(JSON.stringify(options.body)); + req.end(); + }); + }; + + var handle = function(){ + request().then(function (res) { if (~[200,201,203,205,206].indexOf(res.statusCode)) { // Successful w/ Body - resolve(JSON.parse(body)); + resolve(res.body); } else if (~[202,204].indexOf(res.statusCode)){ //Successful w/o Body resolve({ message:"Request is processing" }); + } else if (~[439].indexOf(res.statusCode)){ + //Too Many Requests + setTimeout( handle, 5000 ); } else { - body = JSON.parse(body); - body.statusCode = res.statusCode; - reject(body); + reject(res); } - }); - }); - req.on('error', function(e) { - // General error, i.e. - // - ECONNRESET - server closed the socket unexpectedly - // - ECONNREFUSED - server did not listen - // - HPE_INVALID_VERSION - // - HPE_INVALID_STATUS - reject(e); - }); + },function(err){ + reject( err ); + }); + }; - if (options.body) req.write(JSON.stringify(options.body)); - req.end(); + handle(); }); -} +}; From 37aad9e1b936462a358d3ff4bc2c63c854be578d Mon Sep 17 00:00:00 2001 From: Kamil Burzynski Date: Thu, 5 Nov 2015 12:14:41 +0100 Subject: [PATCH 2/4] Fixed body handling --- lib/utils.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 578b3ca..3b7e6e6 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -39,7 +39,7 @@ module.exports.request = function(options) { res.on('end', function() { resolve({ "statusCode": res.statusCode, - "body": JSON.parse(body) + "body": body }); }); }); @@ -62,7 +62,7 @@ module.exports.request = function(options) { request().then(function (res) { if (~[200,201,203,205,206].indexOf(res.statusCode)) { // Successful w/ Body - resolve(res.body); + resolve(JSON.parse(res.body)); } else if (~[202,204].indexOf(res.statusCode)){ //Successful w/o Body resolve({ @@ -72,6 +72,7 @@ module.exports.request = function(options) { //Too Many Requests setTimeout( handle, 5000 ); } else { + res.body = JSON.parse(res.body); reject(res); } From a0a4e1e41db5808cdc549a3fe70216b39833d22c Mon Sep 17 00:00:00 2001 From: Kamil Burzynski Date: Thu, 5 Nov 2015 12:30:10 +0100 Subject: [PATCH 3/4] 429. I said 429! --- lib/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/utils.js b/lib/utils.js index 3b7e6e6..749e6eb 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -68,7 +68,7 @@ module.exports.request = function(options) { resolve({ message:"Request is processing" }); - } else if (~[439].indexOf(res.statusCode)){ + } else if (~[429].indexOf(res.statusCode)){ //Too Many Requests setTimeout( handle, 5000 ); } else { From b60fe1bf5c70152383e8cb100113a564af301bf0 Mon Sep 17 00:00:00 2001 From: Kamil Burzynski Date: Thu, 12 Nov 2015 15:15:28 +0100 Subject: [PATCH 4/4] Fixed 404 error handling. --- lib/utils.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 749e6eb..0171b78 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -72,8 +72,9 @@ module.exports.request = function(options) { //Too Many Requests setTimeout( handle, 5000 ); } else { - res.body = JSON.parse(res.body); - reject(res); + var body = JSON.parse(res.body); + body.statusCode = res.statusCode; + reject(body); } },function(err){