From 6fbe4b93d1dccdb305148164660d756ebd58c9d8 Mon Sep 17 00:00:00 2001 From: Milutin Kristofic Date: Wed, 4 Sep 2019 11:39:43 +0200 Subject: [PATCH] Reporting top 3 lines of stack for js errors This should help investigating js errors. It includes line, column and names of two functions who calls method with js error. Change-Id: Icc893161e9c9bb0cde4ec2aecd96fe5e71d780f3 --- .../app/elements/core/gr-reporting/gr-reporting.js | 8 +++++++- .../elements/core/gr-reporting/gr-reporting_test.html | 10 ++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/polygerrit-ui/app/elements/core/gr-reporting/gr-reporting.js b/polygerrit-ui/app/elements/core/gr-reporting/gr-reporting.js index ca494d2f2c..397189a52d 100644 --- a/polygerrit-ui/app/elements/core/gr-reporting/gr-reporting.js +++ b/polygerrit-ui/app/elements/core/gr-reporting/gr-reporting.js @@ -116,7 +116,13 @@ if (error) { line = line || error.lineNumber; column = column || error.columnNumber; - msg = msg || error.toString(); + let shortenedErrorStack = msg; + if (error.stack) { + const errorStackLines = error.stack.split('\n'); + shortenedErrorStack = errorStackLines.slice(0, + Math.min(3, errorStackLines.length)).join('\n'); + } + msg = shortenedErrorStack || error.toString(); } const payload = { url, diff --git a/polygerrit-ui/app/elements/core/gr-reporting/gr-reporting_test.html b/polygerrit-ui/app/elements/core/gr-reporting/gr-reporting_test.html index f5053117ec..a0a25b3831 100644 --- a/polygerrit-ui/app/elements/core/gr-reporting/gr-reporting_test.html +++ b/polygerrit-ui/app/elements/core/gr-reporting/gr-reporting_test.html @@ -341,6 +341,7 @@ limitations under the License. test('is reported', () => { const error = new Error('bar'); + error.stack = undefined; emulateThrow('bar', 'http://url', 4, 2, error); assert.isTrue(reporter.calledWith('error', 'exception', 'bar')); const payload = reporter.lastCall.args[3]; @@ -352,6 +353,15 @@ limitations under the License. }); }); + test('is reported with 3 lines of stack', () => { + const error = new Error('bar'); + emulateThrow('bar', 'http://url', 4, 2, error); + const expectedStack = error.stack.split('\n').slice(0, 3) + .join('\n'); + assert.isTrue(reporter.calledWith('error', 'exception', + expectedStack)); + }); + test('prevent default event handler', () => { assert.isTrue(emulateThrow()); });