1 | 'use strict';
|
2 |
|
3 | var cookie = require('cookie');
|
4 | var urlParser = require('url');
|
5 | var stringify = require('../vendor/json-stringify-safe');
|
6 |
|
7 | var utils = require('./utils');
|
8 |
|
9 | module.exports.parseText = function parseText(message, kwargs) {
|
10 | kwargs = kwargs || {};
|
11 | kwargs.message = message;
|
12 |
|
13 | return kwargs;
|
14 | };
|
15 |
|
16 | module.exports.parseError = function parseError(err, kwargs, cb) {
|
17 | utils.parseStack(err, function(frames) {
|
18 | var name =
|
19 | ({}.hasOwnProperty.call(err, 'name') ? err.name : err.constructor.name) + '';
|
20 | if (typeof kwargs.message === 'undefined') {
|
21 | kwargs.message = name + ': ' + (err.message || '<no message>');
|
22 | }
|
23 | kwargs.exception = [
|
24 | {
|
25 | type: name,
|
26 | value: err.message,
|
27 | stacktrace: {
|
28 | frames: frames
|
29 | }
|
30 | }
|
31 | ];
|
32 |
|
33 |
|
34 | var extraErrorProps;
|
35 | for (var key in err) {
|
36 | if (err.hasOwnProperty(key)) {
|
37 | if (key !== 'name' && key !== 'message' && key !== 'stack' && key !== 'domain') {
|
38 | extraErrorProps = extraErrorProps || {};
|
39 | extraErrorProps[key] = err[key];
|
40 | }
|
41 | }
|
42 | }
|
43 | if (extraErrorProps) {
|
44 | kwargs.extra = kwargs.extra || {};
|
45 | kwargs.extra[name] = extraErrorProps;
|
46 | }
|
47 |
|
48 | if (!kwargs.transaction && !kwargs.culprit) {
|
49 | for (var n = frames.length - 1; n >= 0; n--) {
|
50 | if (frames[n].in_app) {
|
51 | kwargs.transaction = utils.getTransaction(frames[n]);
|
52 | break;
|
53 | }
|
54 | }
|
55 | }
|
56 |
|
57 | cb(kwargs);
|
58 | });
|
59 | };
|
60 |
|
61 | module.exports.parseRequest = function parseRequest(req, parseUser) {
|
62 | var kwargs = {};
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | var headers = req.headers || req.header || {};
|
68 |
|
69 |
|
70 |
|
71 | var method = req.method;
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 | var host = req.hostname || req.host || headers.host || '<no host>';
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | var protocol =
|
83 | req.protocol === 'https' || req.secure || (req.socket || {}).encrypted
|
84 | ? 'https'
|
85 | : 'http';
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | var originalUrl = req.originalUrl || req.url;
|
91 |
|
92 |
|
93 | var absoluteUrl = protocol + '://' + host + originalUrl;
|
94 |
|
95 |
|
96 |
|
97 |
|
98 | var query = req.query || urlParser.parse(originalUrl || '', true).query;
|
99 |
|
100 |
|
101 |
|
102 | var cookies = cookie.parse(headers.cookie || '');
|
103 |
|
104 |
|
105 |
|
106 | var data = req.body;
|
107 | if (['GET', 'HEAD'].indexOf(method) === -1) {
|
108 | if (typeof data === 'undefined') {
|
109 | data = '<unavailable>';
|
110 | }
|
111 | }
|
112 |
|
113 | if (data && typeof data !== 'string' && {}.toString.call(data) !== '[object String]') {
|
114 |
|
115 | data = stringify(data);
|
116 | }
|
117 |
|
118 |
|
119 | var http = {
|
120 | method: method,
|
121 | query_string: query,
|
122 | headers: headers,
|
123 | cookies: cookies,
|
124 | data: data,
|
125 | url: absoluteUrl
|
126 | };
|
127 |
|
128 |
|
129 | kwargs.request = http;
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 | if (parseUser == null) parseUser = ['id', 'username', 'email'];
|
139 | if (parseUser) {
|
140 | var user = {};
|
141 | if (typeof parseUser === 'function') {
|
142 | user = parseUser(req);
|
143 | } else if (req.user) {
|
144 | if (parseUser === true) {
|
145 | for (var key in req.user) {
|
146 | if ({}.hasOwnProperty.call(req.user, key)) {
|
147 | user[key] = req.user[key];
|
148 | }
|
149 | }
|
150 | } else {
|
151 | parseUser.forEach(function(fieldName) {
|
152 | if ({}.hasOwnProperty.call(req.user, fieldName)) {
|
153 | user[fieldName] = req.user[fieldName];
|
154 | }
|
155 | });
|
156 | }
|
157 | }
|
158 |
|
159 |
|
160 |
|
161 |
|
162 | var ip = req.ip || (req.connection && req.connection.remoteAddress);
|
163 | if (ip) {
|
164 | user.ip_address = ip;
|
165 | }
|
166 |
|
167 | kwargs.user = user;
|
168 | }
|
169 |
|
170 | return kwargs;
|
171 | };
|