From 50433de063fa06a4e93d8bdc9dc2879bf16fda6b Mon Sep 17 00:00:00 2001 From: Peter Hedenskog Date: Sat, 9 Nov 2019 21:54:44 +0100 Subject: [PATCH] Handle errors that do not belong to a URL. (#2772) --- lib/plugins/html/dataCollector.js | 13 +++++++++++++ lib/plugins/html/htmlBuilder.js | 4 ++++ lib/plugins/html/index.js | 14 +++++++++----- lib/plugins/html/templates/errors.pug | 3 ++- lib/plugins/slack/dataCollector.js | 13 +++++++++++++ lib/plugins/slack/index.js | 11 ++++++++++- 6 files changed, 51 insertions(+), 7 deletions(-) diff --git a/lib/plugins/html/dataCollector.js b/lib/plugins/html/dataCollector.js index 0e55869be..89391a212 100644 --- a/lib/plugins/html/dataCollector.js +++ b/lib/plugins/html/dataCollector.js @@ -12,6 +12,7 @@ class DataCollector { this.urlPages = {}; this.summaryPages = {}; this.browsertimeScreenshots = false; + this.errors = {}; } _addUrl(url) { @@ -66,6 +67,18 @@ class DataCollector { ); } + getErrors() { + return this.errors; + } + + addError(source, data) { + if (this.errors[source]) { + this.errors[source].push(data); + } else { + this.errors[source] = [data]; + } + } + addErrorForUrl(url, source, data) { this._addUrl(url); const errors = get(this.urlPages[url], 'errors', {}); diff --git a/lib/plugins/html/htmlBuilder.js b/lib/plugins/html/htmlBuilder.js index 3134e3681..5596e1c8c 100644 --- a/lib/plugins/html/htmlBuilder.js +++ b/lib/plugins/html/htmlBuilder.js @@ -67,6 +67,10 @@ class HTMLBuilder { const nTestedPages = dataCollector.getURLs().length; log.debug('Render HTML for %s page(s) ', nTestedPages); const errors = dataCollector.getErrorUrls(); + // If we have any errors that are not linked to a URL, add them + if (Object.keys(dataCollector.getErrors()).length > 0) { + errors['generic'] = dataCollector.getErrors(); + } const css = this.inlineCSS.join(''); const assetsBaseURL = this.options.html.assetsBaseURL; if (Object.keys(errors).length > 0) { diff --git a/lib/plugins/html/index.js b/lib/plugins/html/index.js index a99360ba4..84c5de240 100644 --- a/lib/plugins/html/index.js +++ b/lib/plugins/html/index.js @@ -106,11 +106,15 @@ module.exports = { } else { switch (message.type) { case 'error': { - dataCollector.addErrorForUrl( - message.url, - message.source, - message.data - ); + if (message.url) { + dataCollector.addErrorForUrl( + message.url, + message.source, + message.data + ); + } else { + dataCollector.addError(message.source, message.data); + } break; } // we always want to add data from our HARs diff --git a/lib/plugins/html/templates/errors.pug b/lib/plugins/html/templates/errors.pug index 55c774d97..1716dbf8f 100644 --- a/lib/plugins/html/templates/errors.pug +++ b/lib/plugins/html/templates/errors.pug @@ -5,7 +5,8 @@ mixin row(errors) each error, url in errors tr td - a(href=url)= url + - if (url !== 'generic') + a(href=url)= url td ul each pageerrors, tool in error diff --git a/lib/plugins/slack/dataCollector.js b/lib/plugins/slack/dataCollector.js index 5235ef9a5..147946d18 100644 --- a/lib/plugins/slack/dataCollector.js +++ b/lib/plugins/slack/dataCollector.js @@ -9,6 +9,7 @@ class DataCollector { this.resultUrls = context.resultUrls; this.urlRunPages = {}; this.urlPages = {}; + this.errors = {}; this.summaryPage = {}; } @@ -34,6 +35,18 @@ class DataCollector { return this.urlPages[url]; } + getErrors() { + return this.errors; + } + + addError(source, data) { + if (this.errors[source]) { + this.errors[source].push(data); + } else { + this.errors[source] = [data]; + } + } + addErrorForUrl(url, source, data) { this._addUrl(url); diff --git a/lib/plugins/slack/index.js b/lib/plugins/slack/index.js index 8a31d0691..1d326a4d7 100644 --- a/lib/plugins/slack/index.js +++ b/lib/plugins/slack/index.js @@ -33,6 +33,7 @@ function send(options, dataCollector, context, screenshotType) { for (const url of dataCollector.getURLs()) { const errors = dataCollector.getURLData(url).errors; + if (errors) { pageErrors.push(errors); } @@ -98,7 +99,15 @@ module.exports = { switch (message.type) { case 'error': { - dataCollector.addErrorForUrl(message.url, message.source, message.data); + if (message.url) { + dataCollector.addErrorForUrl( + message.url, + message.source, + message.data + ); + } else { + dataCollector.addError(message.source, message.data); + } break; }