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