From 5e5866b17da6d5cab7d1598efc1b75468f08f30d Mon Sep 17 00:00:00 2001 From: Karol Date: Wed, 15 Nov 2017 10:42:16 +0100 Subject: [PATCH 1/2] Fix stack trace if there are anonymous functions in stack trace * Add 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 --- README.md | 6 +++--- package-lock.json | 2 +- package.json | 6 ++++++ stackdriver-errors.js | 4 +++- test/test.html | 1 - test/test.js | 37 ++++++++++++++++++++++++++++++++++++- 6 files changed, 49 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index c739915..df6f83a 100644 --- a/README.md +++ b/README.md @@ -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. 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. diff --git a/package-lock.json b/package-lock.json index 836b5a7..859ed1b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "stackdriver-errors-js", - "version": "0.1.0", + "version": "0.2.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index f387cc9..f1f16f5 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/stackdriver-errors.js b/stackdriver-errors.js index 483628d..c0a4140 100644 --- a/stackdriver-errors.js +++ b/stackdriver-errors.js @@ -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 will be used as the name. + payload.message += [' at ', stack[s].getFunctionName() || '', ' (', stack[s].getFileName(), ':', stack[s].getLineNumber() ,':', stack[s].getColumnNumber() , ')'].join(''); } that.sendErrorPayload(payload, callback); }, function(reason) { diff --git a/test/test.html b/test/test.html index 8c98737..36d73c7 100644 --- a/test/test.html +++ b/test/test.html @@ -16,7 +16,6 @@ diff --git a/test/test.js b/test/test.js index eee183e..28e4ae6 100644 --- a/test/test.js +++ b/test/test.js @@ -18,7 +18,9 @@ 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); @@ -26,6 +28,13 @@ function expectRequestWithMessage(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(){}; @@ -119,6 +128,32 @@ 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 extract and send 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(''); + done(); + }); + } + }); + }); describe('Unhandled exceptions', function () { From 4316b8b1dbc7fae0c80cdf832ed3c95c1a83322e Mon Sep 17 00:00:00 2001 From: Karol Date: Thu, 16 Nov 2017 10:16:18 +0100 Subject: [PATCH 2/2] Fixing sugestions from CR * Update test message * Use explicite anonymous function in anonymous test case --- test/test.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/test.js b/test/test.js index 28e4ae6..e9d947f 100644 --- a/test/test.js +++ b/test/test.js @@ -141,11 +141,13 @@ describe('Reporting errors', function () { } }); - it('should extract and send in stack traces', function (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 { - throwError(message) + (function () { + throw new TypeError(message); + })() } catch(e) { errorHandler.report(e, function() { expectRequestWithMessage('');