Skip to content

Commit 76820d1

Browse files
alexk-blackopsalexk-blackops
authored andcommitted
minor fixes
request parsing methods moved to helpers
1 parent 42419cd commit 76820d1

File tree

8 files changed

+142
-114
lines changed

8 files changed

+142
-114
lines changed

index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@ var api = require('./lib/api'), // wrappers for API calls
77
sender = require('./lib/sender'); // wrapper for http/https requests
88

99
module.exports = {
10-
10+
// start sending logs
1111
start: function(options) {
1212
api.methods.identifyApp(options);
13-
/* exc.exc();
14-
*/ },
13+
exc.exc();
14+
},
1515

1616
log: logger.methods.log,
1717
trace: logger.methods.trace,
1818
debug: logger.methods.debug,
1919
info: logger.methods.info,
2020
warn: logger.methods.warn,
2121
error: logger.methods.error,
22-
22+
2323
push: logger.methods.push,
2424

2525
excHandler: exc.exc,

lib/api.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ var os = require('os'),
99
pkginfo = require('pkginfo')(module, 'name'),
1010

1111
appname = module.exports.name, // name of the app from package.json
12-
fail_delay = 0, // scheduled delay when postLogs call failed
13-
last_api_error,
12+
last_api_error,
1413

1514
//getting all the headers for requests
1615
options = function options(path, body, proxy) {
@@ -79,7 +78,8 @@ module.exports.methods = {
7978
},
8079

8180
postLogs: function postLogs(messages, cb) {
82-
var body = {
81+
var delay = 0, // scheduled delay when postLogs call failed
82+
body = {
8383
CDID: CONFIG.APP_DETAILS.DeviceID,
8484
CDAppID: CONFIG.APP_DETAILS.DeviceAppID,
8585
AppNameID: CONFIG.APP_DETAILS.AppNameID,
@@ -96,24 +96,23 @@ module.exports.methods = {
9696
opt = options(CONFIG.LOG_SAVE_PATH, body, CONFIG.PROXY),
9797

9898
//retry the request if it failed
99-
fail = function(code) {
99+
fail = function (code) {
100100
var sinceFirstError;
101101

102102
if (code === 401) {
103-
fail_delay = CONFIG.DELAY.FIVE_MINUTES;
103+
delay = CONFIG.DELAY.FIVE_MINUTES;
104104
last_api_error = new Date().getTime();
105105
} else {
106106
last_api_error = last_api_error || new Date().getTime();
107107
sinceFirstError = new Date().getTime() - last_api_error;
108-
fail_delay = Math.min(Math.max(sinceFirstError, CONFIG.DELAY.ONE_SECOND), CONFIG.DELAY.ONE_MINUTE);
108+
delay = Math.min(Math.max(sinceFirstError, CONFIG.DELAY.ONE_SECOND), CONFIG.DELAY.ONE_MINUTE);
109109
}
110110

111111
setTimeout(function () {
112112
send(opt, cb, fail);
113-
}, fail_delay);
114-
115-
};
113+
}, delay);
116114

115+
};
117116
send(opt, cb, fail);
118117
}
119118
};

lib/error.js

Lines changed: 5 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -7,67 +7,10 @@ var url = require('url'),
77
helpers = require('./helpers'),
88
CONFIG = require('../config/config'),
99

10-
// extracting cookies from request object
11-
getCookies = function getCookies(req) {
12-
var key,
13-
keys,
14-
cookies = req.cookies,
15-
result = {};
16-
if (cookies) {
17-
if (Object.keys(cookies).length) {
18-
for (key in cookies) {
19-
if (cookies.hasOwnProperty(key)) {
20-
result[key] = CONFIG.COOKIE_MASK;
21-
}
22-
}
23-
}
24-
} else if (req.headers.cookie) {
25-
keys = req.headers.cookie.split('; ');
26-
keys.forEach(function (elem) {
27-
var parts = elem.split('=');
28-
result[parts[0]] = CONFIG.COOKIE_MASK;
29-
});
30-
}
31-
32-
return result;
33-
},
34-
// extracting post data
35-
getPostData = function getPostData(req) {
36-
var result = {};
37-
if (req.body instanceof Object) {
38-
result = req.body;
39-
}
40-
return result;
41-
},
42-
43-
//getting the trace of an exception
44-
getTrace = function getTrace(err) {
45-
var trace = stackTrace.parse(err),
46-
result = [];
47-
/* remove methods of logger itself from the stack trace */
48-
trace.forEach(function (val, index, arr) {
49-
var method = val.methodName || (val.functionName || val.typeName) + '.<anonymous>';
50-
51-
if (method.search('lib/logger.js') < 0 && method.search('stackify') < 0) {
52-
result.push({
53-
CodeFileName: val.fileName,
54-
LineNum: val.lineNumber,
55-
Method: method
56-
});
57-
}
58-
});
59-
return result;
60-
},
61-
// correctly parsing the URL
62-
getURL = function getURL(req) {
63-
var href = (req.connection.encrypted ? "https://" : "http://") + req.headers.host + req.url;
64-
return href;
65-
};
66-
6710
module.exports = {
6811
/* getting source method and source method line of code*/
6912
getStackTraceItem: function getStackTraceItem(err) {
70-
var trace = getTrace(err);
13+
var trace = helpers.getTrace(err);
7114
return {
7215
SrcMethod: trace[0].Method,
7316
SrcLine: trace[0].LineNum
@@ -76,7 +19,7 @@ module.exports = {
7619

7720
// create the exception details object
7821
formatEx : function formatEx(err, req, msg) {
79-
var trace = getTrace(err),
22+
var trace = helpers.getTrace(err),
8023
ex = {
8124
OccuredEpochMillis: Date.now(),
8225
Error: {
@@ -101,7 +44,7 @@ module.exports = {
10144
key;
10245

10346
if (req) {
104-
href = url.parse(getURL(req), true);
47+
href = url.parse(helpers.getURL(req), true);
10548

10649
if (req.headers.cookie) {
10750
headers = {};
@@ -121,9 +64,9 @@ module.exports = {
12164
RequestUrlRoot: href.hostname,
12265
ReferralUrl: req.headers.referer,
12366
Headers: headers,
124-
Cookies: getCookies(req),
67+
Cookies: helpers.getCookies(req),
12568
QueryString: href.query,
126-
PostData: getPostData(req)
69+
PostData: helpers.getPostData(req)
12770
};
12871
}
12972

lib/exception.js

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,11 @@ var qs = require('querystring'),
44
error = require('./error'),
55
logger = require('./logger'),
66
api = require('./api'),
7-
CONFIG = require('../config/config'),
8-
9-
10-
// function that sends all the messages in the queue to Stackify when exception is caught
11-
handler = function handler(err, req, cb) {
12-
logger.methods.push('error', err.message, {error: error}, req);
13-
api.methods.postLogs(logger.storage, cb);
14-
};
15-
16-
// flag used to prevent catching the same exception twice (inside and outside of the createServer method)
17-
module.exports.excCaught = excCaught = false;
7+
CONFIG = require('../config/config');
188

199
module.exports = {
10+
// flag used to prevent catching the same exception twice (inside and outside of the createServer method)
11+
excCaught : false,
2012
// Pure Node apps exception catching
2113
exc : function exc(req, res) {
2214
return (function () {
@@ -34,16 +26,15 @@ module.exports = {
3426

3527
process.on('uncaughtException', function (err) {
3628
console.log('exc');
37-
if (!excCaught) {
38-
excCaught = true;
39-
handler(err, req);
29+
if (!this.excCaught) {
30+
this.excCaught = true;
31+
logger.methods.stop(err, req, function () {return true; });
4032
}
4133
});
4234
}());
4335
},
4436
// Express error handling middleware
4537
expressExc : function expressExc(err, req, res, next) {
46-
4738
var cb = function () {
4839
next(err);
4940
};
@@ -52,6 +43,10 @@ module.exports = {
5243
return next();
5344
}
5445

55-
handler(err, req, cb);
46+
if (!this.excCaught) {
47+
this.excCaught = true;
48+
logger.methods.stop(err, req, cb);
49+
}
50+
5651
}
5752
};

lib/helpers.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,74 @@ module.exports.parseMeta = function parseMeta(meta, err) {
2929
throw new TypeError('Metadata should be valid JSON Object');
3030
}
3131

32+
};
33+
34+
/*
35+
*** Function for extracting cookies from request object ***
36+
*/
37+
module.exports.getCookies = function getCookies(req) {
38+
var key,
39+
keys,
40+
cookies = req.cookies,
41+
result = {};
42+
if (cookies) {
43+
if (Object.keys(cookies).length) {
44+
for (key in cookies) {
45+
if (cookies.hasOwnProperty(key)) {
46+
result[key] = CONFIG.COOKIE_MASK;
47+
}
48+
}
49+
}
50+
} else if (req.headers.cookie) {
51+
keys = req.headers.cookie.split('; ');
52+
keys.forEach(function (elem) {
53+
var parts = elem.split('=');
54+
result[parts[0]] = CONFIG.COOKIE_MASK;
55+
});
56+
}
57+
58+
return result;
59+
};
60+
61+
/*
62+
*** Function for extracting post data from request object ***
63+
*/
64+
65+
module.exports.getPostData = function getPostData(req) {
66+
var result = {};
67+
if (req.body instanceof Object) {
68+
result = req.body;
69+
}
70+
return result;
71+
};
72+
73+
/*
74+
*** Function for getting the trace of an exception object ***
75+
*/
76+
77+
module.exports.getTrace = function getTrace(err) {
78+
var trace = stackTrace.parse(err),
79+
result = [];
80+
/* remove methods of logger itself from the stack trace */
81+
trace.forEach(function (val, index, arr) {
82+
var method = val.methodName || (val.functionName || val.typeName) + '.<anonymous>';
83+
84+
if (method.search('lib/logger.js') < 0 && method.search('stackify') < 0) {
85+
result.push({
86+
CodeFileName: val.fileName,
87+
LineNum: val.lineNumber,
88+
Method: method
89+
});
90+
}
91+
});
92+
return result;
93+
};
94+
95+
/*
96+
*** Function for correctly parsing the URL ***
97+
*/
98+
99+
module.exports.getURL = function getURL(req) {
100+
var href = (req.connection.encrypted ? "https://" : "http://") + req.headers.host + req.url;
101+
return href;
32102
};

0 commit comments

Comments
 (0)