forked from stacktracejs/stacktrace.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstacktrace.js
More file actions
129 lines (114 loc) · 4 KB
/
Copy pathstacktrace.js
File metadata and controls
129 lines (114 loc) · 4 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
/* global StackFrame: false, ErrorStackParser: false */
(function (root, factory) {
'use strict';
// Universal Module Definition (UMD) to support AMD, CommonJS/Node.js, Rhino, and browsers.
if (typeof define === 'function' && define.amd) {
define(['error-stack-parser', 'stack-generator'], factory);
} else if (typeof exports === 'object') {
module.exports = factory(require('error-stack-parser'), require('stack-generator'));
} else {
root.StackTrace = factory(root.ErrorStackParser, root.StackGenerator);
}
}(this, function (ErrorStackParser, StackGenerator) {
// { filter: fnRef
// sourceMap: ???
// cors: ???
// enhancedFunctionNames: true
// enhancedSourceLocations: true
// formatter: fnRef
// }
// Do not process or try to enhance filtered StackFrames
// new StackTrace()
// .withEnhancedFunctionNames()
// .withEnhancedSourceLocations()
// .withFilter(fn)
// .withMaxStackSize(10)
// .withFormatter(fn)
// .instrument(fn)
// .get(opts) => Array[StackFrame]
/**
* Merge 2 given Objects. If a conflict occurs the second object wins.
* Does not do deep merges.
* @param first Object
* @param second Object
* @returns new Object merged first and second
* @private
*/
function _merge(first, second) {
var target = {};
var prop;
[first, second].forEach(function (obj) {
for (prop in obj) {
if (obj.hasOwnProperty(prop)) {
target[prop] = obj[prop];
}
}
return target;
});
return target;
}
/**
* Return true if called from context within strict mode.
* @private
*/
function _isStrictMode() {
return (eval("var __temp = null"), (typeof __temp === "undefined"));
}
return function StackTrace() {
// TODO: utils to facilitate automatic bug reporting
this.options = {};
/**
* Get a backtrace from invocation point.
* @param opts Options Object
* @return Array[StackFrame]
*/
this.get = function (opts) {
try {
throw new Error("From StackTrace.get()");
} catch (e) {
if (e['stack'] || e['opera#sourceloc']) {
return this.fromError(e, _merge(this.options, opts));
} else {
return this.generateArtificially(_merge(this.options, opts));
}
}
};
/**
* Given an error object, parse it.
* @param error Error object
* @param opts Object for options
* @return Array[StackFrame]
*/
this.fromError = function fromError(error, opts) {
opts = _merge(this.options, opts);
var stackframes = new ErrorStackParser().parse(error); //ErrorStackParser.parse(error)
if (typeof opts.filter === 'function') {
// TODO: stackframes = stackframes.filter(opts.filter);
}
// TODO: apply enhancements here
if (typeof opts.formatter === 'function') {
// TODO: stackframes = stackframes.map(opts.formatter);
}
return stackframes;
};
/**
* Use StackGenerator to generate a backtrace.
* @param opts Object options
* @returns Array[StackFrame]
*/
this.generateArtificially = function generateArtificially(opts) {
return StackGenerator.backtrace(opts);
};
this.withFilter = function withFilter(fn) {
if (typeof fn !== 'function') {
throw new TypeError('Can only apply filter with a function')
}
this.options.filter = fn;
return this;
};
this.withFormatter = function withFormatter(fn) {
this.options.formatter = fn;
return this;
};
}
}));