UNPKG

4.56 kBJavaScriptView Raw
1'use strict';
2
3var cookie = require('cookie');
4var urlParser = require('url');
5var stringify = require('../vendor/json-stringify-safe');
6
7var utils = require('./utils');
8
9module.exports.parseText = function parseText(message, kwargs) {
10 kwargs = kwargs || {};
11 kwargs.message = message;
12
13 return kwargs;
14};
15
16module.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 // Save additional error properties to `extra` under the error type (e.g. `extra.AttributeError`)
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
61module.exports.parseRequest = function parseRequest(req, parseUser) {
62 var kwargs = {};
63
64 // headers:
65 // node, express: req.headers
66 // koa: req.header
67 var headers = req.headers || req.header || {};
68
69 // method:
70 // node, express, koa: req.method
71 var method = req.method;
72
73 // host:
74 // express: req.hostname in > 4 and req.host in < 4
75 // koa: req.host
76 // node: req.headers.host
77 var host = req.hostname || req.host || headers.host || '<no host>';
78
79 // protocol:
80 // node: <n/a>
81 // express, koa: req.protocol
82 var protocol =
83 req.protocol === 'https' || req.secure || (req.socket || {}).encrypted
84 ? 'https'
85 : 'http';
86
87 // url (including path and query string):
88 // node, express: req.originalUrl
89 // koa: req.url
90 var originalUrl = req.originalUrl || req.url;
91
92 // absolute url
93 var absoluteUrl = protocol + '://' + host + originalUrl;
94
95 // query string:
96 // node: req.url (raw)
97 // express, koa: req.query
98 var query = req.query || urlParser.parse(originalUrl || '', true).query;
99
100 // cookies:
101 // node, express, koa: req.headers.cookie
102 var cookies = cookie.parse(headers.cookie || '');
103
104 // body data:
105 // node, express, koa: req.body
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 // Make sure the request body is a string
115 data = stringify(data);
116 }
117
118 // http interface
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 // expose http interface
129 kwargs.request = http;
130
131 // user: typically found on req.user in express/passport patterns
132 // five cases for parseUser value:
133 // absent: grab only id, username, email from req.user
134 // false: capture nothing
135 // true: capture all keys from req.user
136 // array: provided whitelisted keys to grab from req.user
137 // function :: req -> user: custom parsing function
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 // client ip:
160 // node: req.connection.remoteAddress
161 // express, koa: req.ip
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};