Skip to content

Commit 8fab071

Browse files
Jastrzebowskisteren
authored andcommitted
Fix stack trace if there are anonymous functions in stack trace (#24)
* Fix stack trace if there are anonymous functions in stack trace * Add <anonymous> in stack trace if there is no functionName * Add command using scripts in package.json * Add tests * Remove unused code in test.html * Update version in package lock * Update readme to include test instructions * Fixing sugestions from CR * Update test message * Use explicite anonymous function in anonymous test case
1 parent 731c9a9 commit 8fab071

6 files changed

Lines changed: 51 additions & 7 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,10 @@ import errorHandler from './errorHandlerUtility';
217217

218218
## Developing the library
219219

220-
Install developer dependencies with `npm install --dev` and install `gulp` with `npm install -g gulp`
220+
Install developer dependencies with `npm install --dev`
221221

222-
* Run `gulp` to test your changes.
223-
* Run `gulp dist` generates the minified version.
222+
* Run `npm test` or `yarn run test` to test your changes.
223+
* Run `npm run dist` or `yarn run dist` generates the minified version.
224224

225225
Start a web server at the root of this repo and open `demo/demo.html` to test reporting errors from the local library with your API key and project ID.
226226

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
"type": "git",
1111
"url": "https://github.com/GoogleCloudPlatform/stackdriver-errors-js"
1212
},
13+
"scripts": {
14+
"test": "gulp",
15+
"dist": "gulp dist",
16+
"lint": "gulp lint",
17+
"start": "gulp min-demo"
18+
},
1319
"keywords": [
1420
"stackdriver",
1521
"error",

stackdriver-errors.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,9 @@
112112
payload.message += '\n';
113113
// Reconstruct the stackframe to a JS stackframe as expected by Error Reporting parsers.
114114
// stack[s].source should not be used because not populated when created from source map.
115-
payload.message += [' at ', stack[s].getFunctionName(), ' (', stack[s].getFileName(), ':', stack[s].getLineNumber() ,':', stack[s].getColumnNumber() , ')'].join('');
115+
//
116+
// If functionName or methodName isn't available <anonymous> will be used as the name.
117+
payload.message += [' at ', stack[s].getFunctionName() || '<anonymous>', ' (', stack[s].getFileName(), ':', stack[s].getLineNumber() ,':', stack[s].getColumnNumber() , ')'].join('');
116118
}
117119
that.sendErrorPayload(payload, callback);
118120
}, function(reason) {

test/test.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
<script src="../stackdriver-errors.js"></script>
1717
<script src="test.js"></script>
1818
<script>
19-
//mocha.allowUncaught()
2019
mocha.run();
2120
</script>
2221
</body>

test/test.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,23 @@ var expect = chai.expect;
1818
var errorHandler;
1919
var xhr, requests;
2020

21-
/** Helper function testing if a given message has been reported */
21+
/**
22+
* Helper function testing if a given message has been reported
23+
*/
2224
function expectRequestWithMessage(message) {
2325
expect(requests.length).to.equal(1);
2426
var sentBody = JSON.parse(requests[0].requestBody);
2527
expect(sentBody).to.include.keys('message');
2628
expect(sentBody.message).to.contain(message);
2729
}
2830

31+
/**
32+
* Helper for testing call stack reporting
33+
*/
34+
function throwError(message) {
35+
throw new TypeError(message);
36+
}
37+
2938
beforeEach(function() {
3039
window.onerror= function(){};
3140

@@ -119,6 +128,34 @@ describe('Reporting errors', function () {
119128
}
120129
});
121130

131+
it('should extract and send functionName in stack traces', function (done) {
132+
var message = 'custom message';
133+
// PhantomJS only attaches a stack to thrown errors
134+
try {
135+
throwError(message)
136+
} catch(e) {
137+
errorHandler.report(e, function() {
138+
expectRequestWithMessage('throwError');
139+
done();
140+
});
141+
}
142+
});
143+
144+
it('should set in stack traces when frame is anonymous', function (done) {
145+
var message = 'custom message';
146+
// PhantomJS only attaches a stack to thrown errors
147+
try {
148+
(function () {
149+
throw new TypeError(message);
150+
})()
151+
} catch(e) {
152+
errorHandler.report(e, function() {
153+
expectRequestWithMessage('<anonymous>');
154+
done();
155+
});
156+
}
157+
});
158+
122159
});
123160

124161
describe('Unhandled exceptions', function () {

0 commit comments

Comments
 (0)