Skip to content

Commit a99a416

Browse files
committed
request log removed, debugger added
1 parent 6026507 commit a99a416

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed

lib/request.js

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,8 @@
1111
*/
1212
var request = require('request');
1313
var chalk = require('chalk');
14-
// var config = require('./config').config();
14+
var debug = require('debug')('request');
1515
var pkg = require('../package');
16-
17-
var warning = chalk.yellow;
18-
var succcess = chalk.green;
19-
var error = chalk.red;
20-
var info = chalk.blue;
21-
var log = console.log;
2216
var MAX_RETRY_LIMIT = 5;
2317

2418
function validate(req, cb) {
@@ -53,33 +47,34 @@ var makeCall = module.exports = function(req, cb, RETRY) {
5347
} else if (RETRY > MAX_RETRY_LIMIT) {
5448
return cb(new Error('Max retry limit exceeded!'));
5549
}
50+
debug(`Requesting API\n${JSON.stringify(req, null, 2)}`);
5651
return request(req, function(err, response, body) {
5752
if (err) {
5853
return cb(err);
5954
}
6055
var timeDelay;
6156
if (response.statusCode >= 200 && response.statusCode <= 399) {
62-
log(succcess(`${req.method}: ${req.uri || req.url} completed succcessfully with ${response.statusCode} statusCode!`));
57+
debug(`${req.method}: ${req.uri || req.url} completed succcessfully with ${response.statusCode} statusCode!`);
6358
return cb(null, body);
6459
} else if (response.statusCode === 429) {
6560
timeDelay = Math.pow(Math.SQRT2, RETRY) * 100;
66-
log(warning(`API rate limit exceeded.\nReceived ${response.statusCode} status\nBody ${JSON.stringify(body)}`));
67-
log(warning(`Retrying ${req.uri || req.url} with ${timeDelay} sec delay`));
61+
debug(`API rate limit exceeded.\nReceived ${response.statusCode} status\nBody ${JSON.stringify(body)}`);
62+
debug(`Retrying ${req.uri || req.url} with ${timeDelay} sec delay`);
6863
return setTimeout(function (req, cb, RETRY) {
6964
return makeCall(req, cb, RETRY);
7065
}, timeDelay, req, cb, RETRY);
7166
} else if (response.statusCode >= 500) {
7267
// retry, with delay
7368
timeDelay = Math.pow(Math.SQRT2, RETRY) * 100;
74-
log(warning(`SERVER ERROR\nRecevied ${response.statusCode} status from Contentstack!\nBody ${JSON.stringify(body)}`));
75-
log(warning(`Retrying ${req.uri || req.url} with ${timeDelay} sec delay`));
69+
debug(`SERVER ERROR\nRecevied ${response.statusCode} status from Contentstack!\nBody ${JSON.stringify(body)}`);
70+
debug(`Retrying ${req.uri || req.url} with ${timeDelay} sec delay`);
7671
RETRY++;
7772
return setTimeout(function (req, cb, RETRY) {
7873
return makeCall(req, cb, RETRY);
7974
}, timeDelay, req, cb, RETRY);
8075
} else {
81-
log(error(`Request failed\n${JSON.stringify(req)}`));
82-
log(error(`Response received\n${JSON.stringify(body)}`));
76+
debug(`Request failed\n${JSON.stringify(req)}`);
77+
debug(`Response received\n${JSON.stringify(body)}`);
8378
return cb(body);
8479
}
8580
});

0 commit comments

Comments
 (0)