Skip to content

Commit b20195d

Browse files
author
Eric Wendelin
committed
Add StackTraceGPS dependency and use for enhancing stack frames.
1 parent c6ff195 commit b20195d

3 files changed

Lines changed: 25 additions & 50 deletions

File tree

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ Looking for the [stable version](https://github.com/stacktracejs/stacktrace.js/t
1010
```
1111
npm install stacktrace-js
1212
bower install stacktrace-js
13-
component install stacktrace.js
1413
wget https://rawgithub.com/stacktracejs/stacktrace.js/master/stacktrace.js
1514
```
1615

spec/stacktrace-spec.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ describe('StackTrace', function () {
33
describe('#constructor', function () {
44
it('should allow empty arguments', function () {
55
expect(function () {
6-
new StackTrace();
6+
new StackTrace(); // jshint ignore:line
77
}).not.toThrow();
88
});
99
});
@@ -43,13 +43,4 @@ describe('StackTrace', function () {
4343
expect(stackFrames.length).toEqual(1);
4444
});
4545
});
46-
47-
describe('#withFilter', function () {
48-
var unit = new StackTrace();
49-
it('throws an error given input other than a function', function () {
50-
expect(function () {
51-
unit.withFilter('BOGUS')
52-
}).toThrow(new TypeError('Can only apply filter with a function'));
53-
});
54-
});
55-
});
46+
});

stacktrace.js

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
'use strict';
44
// Universal Module Definition (UMD) to support AMD, CommonJS/Node.js, Rhino, and browsers.
55
if (typeof define === 'function' && define.amd) {
6-
define(['error-stack-parser', 'stack-generator'], factory);
6+
define(['error-stack-parser', 'stack-generator', 'stacktrace-gps'], factory);
77
} else if (typeof exports === 'object') {
8-
module.exports = factory(require('error-stack-parser'), require('stack-generator'));
8+
module.exports = factory(require('error-stack-parser'), require('stack-generator'), require('stacktrace-gps'));
99
} else {
10-
root.StackTrace = factory(root.ErrorStackParser, root.StackGenerator);
10+
root.StackTrace = factory(root.ErrorStackParser, root.StackGenerator, root.StackTraceGPS);
1111
}
12-
}(this, function (ErrorStackParser, StackGenerator) {
12+
}(this, function (ErrorStackParser, StackGenerator, StackTraceGPS) {
1313
// { filter: fnRef
1414
// sourceMap: ???
1515
// cors: ???
@@ -18,16 +18,6 @@
1818
// formatter: fnRef
1919
// }
2020

21-
// Do not process or try to enhance filtered StackFrames
22-
// new StackTrace()
23-
// .withEnhancedFunctionNames()
24-
// .withEnhancedSourceLocations()
25-
// .withFilter(fn)
26-
// .withMaxStackSize(10)
27-
// .withFormatter(fn)
28-
// .instrument(fn)
29-
// .get(opts) => Array[StackFrame]
30-
3121
/**
3222
* Merge 2 given Objects. If a conflict occurs the second object wins.
3323
* Does not do deep merges.
@@ -57,12 +47,12 @@
5747
* @private
5848
*/
5949
function _isStrictMode() {
60-
return (eval("var __temp = null"), (typeof __temp === "undefined"));
50+
return (eval("var __temp = null"), (typeof __temp === "undefined")); // jshint ignore:line
6151
}
6252

6353
return function StackTrace() {
6454
// TODO: utils to facilitate automatic bug reporting
65-
55+
this.gps = undefined;
6656
this.options = {};
6757

6858
/**
@@ -72,9 +62,9 @@
7262
*/
7363
this.get = function (opts) {
7464
try {
75-
throw new Error("From StackTrace.get()");
65+
throw new Error('From StackTrace.get()');
7666
} catch (e) {
77-
if (e['stack'] || e['opera#sourceloc']) {
67+
if (e.stack || e['opera#sourceloc']) {
7868
return this.fromError(e, _merge(this.options, opts));
7969
} else {
8070
return this.generateArtificially(_merge(this.options, opts));
@@ -91,15 +81,23 @@
9181
this.fromError = function fromError(error, opts) {
9282
opts = _merge(this.options, opts);
9383

94-
var stackframes = new ErrorStackParser().parse(error); //ErrorStackParser.parse(error)
84+
var stackframes = ErrorStackParser.parse(error);
9585
if (typeof opts.filter === 'function') {
96-
// TODO: stackframes = stackframes.filter(opts.filter);
97-
}
98-
// TODO: apply enhancements here
99-
if (typeof opts.formatter === 'function') {
100-
// TODO: stackframes = stackframes.map(opts.formatter);
86+
stackframes = stackframes.filter(opts.filter);
10187
}
10288

89+
stackframes.map(function(sf) {
90+
if (typeof this.gps !== 'function') {
91+
this.gps = new StackTraceGPS();
92+
}
93+
this.gps.findFunctionName(sf, function(name) {
94+
sf.setFunctionName(name);
95+
return sf;
96+
}, function(err) {
97+
return sf;
98+
});
99+
}.bind(this));
100+
103101
return stackframes;
104102
};
105103

@@ -111,19 +109,6 @@
111109
this.generateArtificially = function generateArtificially(opts) {
112110
return StackGenerator.backtrace(opts);
113111
};
114-
115-
this.withFilter = function withFilter(fn) {
116-
if (typeof fn !== 'function') {
117-
throw new TypeError('Can only apply filter with a function')
118-
}
119-
this.options.filter = fn;
120-
return this;
121-
};
122-
123-
this.withFormatter = function withFormatter(fn) {
124-
this.options.formatter = fn;
125-
return this;
126-
};
127-
}
112+
};
128113
}));
129114

0 commit comments

Comments
 (0)