-
Notifications
You must be signed in to change notification settings - Fork 278
Expand file tree
/
Copy pathstacktrace.js
More file actions
190 lines (176 loc) · 7.11 KB
/
stacktrace.js
File metadata and controls
190 lines (176 loc) · 7.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
(function(root, factory) {
'use strict';
// Universal Module Definition (UMD) to support AMD, CommonJS/Node.js, Rhino, and browsers.
/* istanbul ignore next */
if (typeof define === 'function' && define.amd) {
define('stacktrace', ['error-stack-parser', 'stack-generator', 'stacktrace-gps'], factory);
} else if (typeof exports === 'object') {
module.exports = factory(require('error-stack-parser'), require('stack-generator'), require('stacktrace-gps'));
} else {
root.StackTrace = factory(root.ErrorStackParser, root.StackGenerator, root.StackTraceGPS);
}
}(this, function StackTrace(ErrorStackParser, StackGenerator, StackTraceGPS) {
var _options = {
filter: function(stackframe) {
// Filter out stackframes for this library by default
return (stackframe.functionName || '').indexOf('StackTrace$$') === -1 &&
(stackframe.functionName || '').indexOf('ErrorStackParser$$') === -1 &&
(stackframe.functionName || '').indexOf('StackTraceGPS$$') === -1 &&
(stackframe.functionName || '').indexOf('StackGenerator$$') === -1;
},
sourceCache: {}
};
/**
* Merge 2 given Objects. If a conflict occurs the second object wins.
* Does not do deep merges.
*
* @param {Object} first base object
* @param {Object} second overrides
* @returns {Object} merged first and second
* @private
*/
function _merge(first, second) {
var target = {};
[first, second].forEach(function(obj) {
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
target[prop] = obj[prop];
}
}
return target;
});
return target;
}
function _isShapedLikeParsableError(err) {
return err.stack || err['opera#sourceloc'];
}
return {
/**
* Get a backtrace from invocation point.
*
* @param {Object} opts
* @returns {Array} of StackFrame
*/
get: function StackTrace$$get(opts) {
try {
// Error must be thrown to get stack in IE
throw new Error();
} catch (err) {
if (_isShapedLikeParsableError(err)) {
return this.fromError(err, opts);
} else {
return this.generateArtificially(opts);
}
}
},
/**
* Given an error object, parse it.
*
* @param {Error} error object
* @param {Object} opts
* @returns {Promise} for Array[StackFrame}
*/
fromError: function StackTrace$$fromError(error, opts) {
opts = _merge(_options, opts);
var gps = new StackTraceGPS(opts);
return new Promise(function(resolve) {
var stackframes = ErrorStackParser.parse(error);
if (typeof opts.filter === 'function') {
stackframes = stackframes.filter(opts.filter);
}
resolve(Promise.all(stackframes.map(function(sf) {
return new Promise(function(resolve) {
function resolveOriginal() {
resolve(sf);
}
gps.pinpoint(sf).then(resolve, resolveOriginal)['catch'](resolveOriginal);
});
})));
}.bind(this));
},
/**
* Use StackGenerator to generate a backtrace.
*
* @param {Object} opts
* @returns {Promise} of Array[StackFrame]
*/
generateArtificially: function StackTrace$$generateArtificially(opts) {
opts = _merge(_options, opts);
var stackFrames = StackGenerator.backtrace(opts);
if (typeof opts.filter === 'function') {
stackFrames = stackFrames.filter(opts.filter);
}
return Promise.resolve(stackFrames);
},
/**
* Given a function, wrap it such that invocations trigger a callback that
* is called with a stack trace.
*
* @param {Function} fn to be instrumented
* @param {Function} callback function to call with a stack trace on invocation
* @param {Function} errback optional function to call with error if unable to get stack trace.
* @param {Object} thisArg optional context object (e.g. window)
*/
instrument: function StackTrace$$instrument(fn, callback, errback, thisArg) {
if (typeof fn !== 'function') {
throw new Error('Cannot instrument non-function object');
} else if (typeof fn.__stacktraceOriginalFn === 'function') {
// Already instrumented, return given Function
return fn;
}
var instrumented = function StackTrace$$instrumented() {
try {
this.get().then(callback, errback)['catch'](errback);
return fn.apply(thisArg || this, arguments);
} catch (e) {
if (_isShapedLikeParsableError(e)) {
this.fromError(e).then(callback, errback)['catch'](errback);
}
throw e;
}
}.bind(this);
instrumented.__stacktraceOriginalFn = fn;
return instrumented;
},
/**
* Given a function that has been instrumented,
* revert the function to it's original (non-instrumented) state.
*
* @param {Function} fn to de-instrument
*/
deinstrument: function StackTrace$$deinstrument(fn) {
if (typeof fn !== 'function') {
throw new Error('Cannot de-instrument non-function object');
} else if (typeof fn.__stacktraceOriginalFn === 'function') {
return fn.__stacktraceOriginalFn;
} else {
// Function not instrumented, return original
return fn;
}
},
/**
* Given an Array of StackFrames, serialize and POST to given URL.
*
* @param {Array} stackframes
* @param {String} url
*/
report: function StackTrace$$report(stackframes, url) {
return new Promise(function(resolve, reject) {
var req = new XMLHttpRequest();
req.onerror = reject;
req.onreadystatechange = function onreadystatechange() {
if (req.readyState === 4) {
if (req.status >= 200 && req.status < 400) {
resolve(req.responseText);
} else {
reject(new Error('POST to ' + url + ' failed with status: ' + req.status));
}
}
};
req.open('post', url);
req.setRequestHeader('Content-Type', 'application/json');
req.send(JSON.stringify({stack: stackframes}));
});
}
};
}));