Skip to content

Commit 92cab08

Browse files
author
Eric Wendelin
committed
Use stack-generator and add Stacktrace#generateArtificially to utilize it. Fixes #95
1 parent b395ee9 commit 92cab08

7 files changed

+61
-35
lines changed

bower.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
],
99
"description": "Extract meaning from JS Errors",
1010
"dependencies": {
11-
"error-stack-parser": "~0.1.0"
11+
"error-stack-parser": "~0.1.0",
12+
"stack-generator": "~0.1.0"
1213
},
1314
"devDependencies": {
1415
"jasmine": "~1.3.1",

component.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
"repo": "stacktracejs/stacktrace.js",
55
"main": "stacktrace.js",
66
"scripts": [
7-
"stacktrace.js"
7+
"stacktrace.js"
88
],
99
"dependencies": {
10-
"error-stack-parser": "~0.1.0"
10+
"error-stack-parser": "~0.1.0",
11+
"stack-generator": "~0.1.0"
1112
}
1213
}

package.json

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
{
2-
"name": "stacktrace-js",
3-
"description": "Framework-agnostic, micro-library for getting stack traces in all environments",
4-
"author": "Eric Wendelin <me@eriwen.com> (http://www.eriwen.com)",
5-
"version": "0.6.2",
6-
"keywords": ["stack-trace", "cross-browser", "framework-agnostic", "client", "browser"],
7-
"homepage": "http://stacktracejs.com",
8-
"repository": {
9-
"type": "git",
10-
"url": "git://github.com/stacktracejs/stacktrace.js.git"
11-
},
12-
"main": "./stacktrace.js",
13-
"engines": {
14-
"node": "*"
15-
},
16-
"dependencies": {},
17-
"devDependencies": {
18-
"jshint": "0.9.x"
19-
}
2+
"name": "stacktrace-js",
3+
"description": "Framework-agnostic, micro-library for getting stack traces in all environments",
4+
"author": "Eric Wendelin <me@eriwen.com> (http://www.eriwen.com)",
5+
"version": "0.6.2",
6+
"keywords": ["stack-trace", "cross-browser", "framework-agnostic", "client", "browser"],
7+
"homepage": "http://www.stacktracejs.com",
8+
"repository": {
9+
"type": "git",
10+
"url": "git://github.com/stacktracejs/stacktrace.js.git"
11+
},
12+
"main": "./stacktrace.js",
13+
"engines": {
14+
"node": "*"
15+
},
16+
"dependencies": {}
2017
}

spec/jsTestDriver.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ load:
55
- ../bower_components/jasmine-jstd-adapter/src/JasmineAdapter.js
66
- ../bower_components/stackframe/stackframe.js
77
- ../bower_components/error-stack-parser/error-stack-parser.js
8+
- ../bower_components/stack-generator/stack-generator.js
89
- ../stacktrace.js
910
test:
1011
- stacktrace-spec.js

spec/spec-runner.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<script type="text/javascript" src="../bower_components/jasmine/lib/jasmine-core/jasmine-html.js"></script>
1010
<script type="text/javascript" src="../bower_components/stackframe/stackframe.js"></script>
1111
<script type="text/javascript" src="../bower_components/error-stack-parser/error-stack-parser.js"></script>
12+
<script type="text/javascript" src="../bower_components/stack-generator/stack-generator.js"></script>
1213

1314
<script type="text/javascript" src="../stacktrace.js"></script>
1415

spec/stacktrace-spec.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,19 @@ describe('StackTrace', function () {
3434
});
3535
});
3636

37+
describe('#generateArtificially', function () {
38+
var unit = new StackTrace();
39+
it('gets stacktrace from current location', function testGenerateArtificially() {
40+
var stackFrames = unit.generateArtificially().filter(function (stackFrame) {
41+
return stackFrame.getFunctionName() && stackFrame.getFunctionName().indexOf('testGenerateArtificially') > -1;
42+
});
43+
expect(stackFrames.length).toEqual(1);
44+
});
45+
});
46+
3747
describe('#withFilter', function () {
3848
var unit = new StackTrace();
39-
xit('throws an error given input other than a function', function () {
49+
it('throws an error given input other than a function', function () {
4050
expect(function () {
4151
unit.withFilter('BOGUS')
4252
}).toThrow(new TypeError('Can only apply filter with a function'));

stacktrace.js

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +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'], factory);
6+
define(['error-stack-parser', 'stack-generator'], factory);
77
} else if (typeof exports === 'object') {
8-
module.exports = factory(require('error-stack-parser'));
8+
module.exports = factory(require('error-stack-parser'), require('stack-generator'));
99
} else {
10-
root.StackTrace = factory(root.ErrorStackParser);
10+
root.StackTrace = factory(root.ErrorStackParser, root.StackGenerator);
1111
}
12-
}(this, function () {
13-
'use strict';
14-
12+
}(this, function (ErrorStackParser, StackGenerator) {
1513
// { filter: fnRef
1614
// sourceMap: ???
1715
// cors: ???
@@ -25,6 +23,7 @@
2523
// .withEnhancedFunctionNames()
2624
// .withEnhancedSourceLocations()
2725
// .withFilter(fn)
26+
// .withMaxStackSize(10)
2827
// .withFormatter(fn)
2928
// .instrument(fn)
3029
// .get(opts) => Array[StackFrame]
@@ -41,7 +40,7 @@
4140
var target = {};
4241
var prop;
4342

44-
[first, second].forEach(function(obj) {
43+
[first, second].forEach(function (obj) {
4544
for (prop in obj) {
4645
if (obj.hasOwnProperty(prop)) {
4746
target[prop] = obj[prop];
@@ -53,6 +52,14 @@
5352
return target;
5453
}
5554

55+
/**
56+
* Return true if called from context within strict mode.
57+
* @private
58+
*/
59+
function _isStrictMode() {
60+
return (eval("var __temp = null"), (typeof __temp === "undefined"));
61+
}
62+
5663
return function StackTrace() {
5764
// TODO: utils to facilitate automatic bug reporting
5865

@@ -67,7 +74,11 @@
6774
try {
6875
throw new Error("From StackTrace.get()");
6976
} catch (e) {
70-
return this.fromError(e, _merge(this.options, opts));
77+
if (e['stack'] || e['opera#sourceloc']) {
78+
return this.fromError(e, _merge(this.options, opts));
79+
} else {
80+
return this.generateArtificially(_merge(this.options, opts));
81+
}
7182
}
7283
};
7384

@@ -92,6 +103,15 @@
92103
return stackframes;
93104
};
94105

106+
/**
107+
* Use StackGenerator to generate a backtrace.
108+
* @param opts Object options
109+
* @returns Array[StackFrame]
110+
*/
111+
this.generateArtificially = function generateArtificially(opts) {
112+
return StackGenerator.backtrace(opts);
113+
};
114+
95115
this.withFilter = function withFilter(fn) {
96116
if (typeof fn !== 'function') {
97117
throw new TypeError('Can only apply filter with a function')
@@ -104,11 +124,6 @@
104124
this.options.formatter = fn;
105125
return this;
106126
};
107-
108-
// I want to know when a specific function is called
109-
this.instrument = function instrument(fnRef, opts) {};
110-
111-
this.deinstrument = function deinstrument(fnRef) {};
112127
}
113128
}));
114129

0 commit comments

Comments
 (0)