Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,10 @@ import errorHandler from './errorHandlerUtility';

## Developing the library

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

* Run `gulp` to test your changes.
* Run `gulp dist` generates the minified version.
* Run `npm test` or `yarn run test` to test your changes.
* Run `npm run dist` or `yarn run dist` generates the minified version.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the improvements!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're welcome, still, find NPM scripts an underappreciated feature :) perfect for simple commands.


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.

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
"type": "git",
"url": "https://github.com/GoogleCloudPlatform/stackdriver-errors-js"
},
"scripts": {
"test": "gulp",
"dist": "gulp dist",
"lint": "gulp lint",
"start": "gulp min-demo"
},
"keywords": [
"stackdriver",
"error",
Expand Down
4 changes: 3 additions & 1 deletion stackdriver-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@
payload.message += '\n';
// Reconstruct the stackframe to a JS stackframe as expected by Error Reporting parsers.
// stack[s].source should not be used because not populated when created from source map.
payload.message += [' at ', stack[s].getFunctionName(), ' (', stack[s].getFileName(), ':', stack[s].getLineNumber() ,':', stack[s].getColumnNumber() , ')'].join('');
//
// If functionName or methodName isn't available <anonymous> will be used as the name.
payload.message += [' at ', stack[s].getFunctionName() || '<anonymous>', ' (', stack[s].getFileName(), ':', stack[s].getLineNumber() ,':', stack[s].getColumnNumber() , ')'].join('');
}
that.sendErrorPayload(payload, callback);
}, function(reason) {
Expand Down
1 change: 0 additions & 1 deletion test/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
<script src="../stackdriver-errors.js"></script>
<script src="test.js"></script>
<script>
//mocha.allowUncaught()
mocha.run();
</script>
</body>
Expand Down
39 changes: 38 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,23 @@ var expect = chai.expect;
var errorHandler;
var xhr, requests;

/** Helper function testing if a given message has been reported */
/**
* Helper function testing if a given message has been reported
*/
function expectRequestWithMessage(message) {
expect(requests.length).to.equal(1);
var sentBody = JSON.parse(requests[0].requestBody);
expect(sentBody).to.include.keys('message');
expect(sentBody.message).to.contain(message);
}

/**
* Helper for testing call stack reporting
*/
function throwError(message) {
throw new TypeError(message);
}

beforeEach(function() {
window.onerror= function(){};

Expand Down Expand Up @@ -119,6 +128,34 @@ describe('Reporting errors', function () {
}
});

it('should extract and send functionName in stack traces', function (done) {
var message = 'custom message';
// PhantomJS only attaches a stack to thrown errors
try {
throwError(message)
} catch(e) {
errorHandler.report(e, function() {
expectRequestWithMessage('throwError');
done();
});
}
});

it('should set in stack traces when frame is anonymous', function (done) {
var message = 'custom message';
// PhantomJS only attaches a stack to thrown errors
try {
(function () {
throw new TypeError(message);
})()
} catch(e) {
errorHandler.report(e, function() {
expectRequestWithMessage('<anonymous>');
done();
});
}
});

});

describe('Unhandled exceptions', function () {
Expand Down