From c42822ee3e35d546ab83f1631bfa2dd731ce2d6e Mon Sep 17 00:00:00 2001 From: Bastien Gatellier Date: Sun, 4 Feb 2024 17:52:49 +0100 Subject: [PATCH 1/2] feat: add basic error messages if the analyzed page send back a 401 or 403 error --- assets/js/components/EcoIndexDialog.js | 46 ++++++++------------------ i18n/en.toml | 8 +++-- i18n/fr.toml | 8 +++-- 3 files changed, 26 insertions(+), 36 deletions(-) diff --git a/assets/js/components/EcoIndexDialog.js b/assets/js/components/EcoIndexDialog.js index ed20320f..aeb485f6 100644 --- a/assets/js/components/EcoIndexDialog.js +++ b/assets/js/components/EcoIndexDialog.js @@ -9,30 +9,16 @@ class EcoIndexDialog { static #a11yDialog = null; static ERROR_MESSAGES = { - 404: ` - {{- i18n "Error404" | markdownify -}} - `, - 422: ` - {{- i18n "Error422" | markdownify -}} - `, - 429: ` - {{- i18n "Error429" | markdownify -}} - `, - 500: ` - {{- i18n "Error500" | markdownify -}} - `, - 502: ` - {{- i18n "Error502" | markdownify -}} - `, - 504: ` - {{- i18n "Error504" | markdownify -}} - `, - 520: ` - {{- i18n "Error520" | markdownify -}} - `, - 521: ` - {{- i18n "Error521" | markdownify -}} - `, + 401: `{{- i18n "Error401" | markdownify -}}`, + 403: `{{- i18n "Error403" | markdownify -}}`, + 404: `{{- i18n "Error404" | markdownify -}}`, + 422: `{{- i18n "Error422" | markdownify -}}`, + 429: `{{- i18n "Error429" | markdownify -}}`, + 500: `{{- i18n "Error500" | markdownify -}}`, + 502: `{{- i18n "Error502" | markdownify -}}`, + 504: `{{- i18n "Error504" | markdownify -}}`, + 520: `{{- i18n "Error520" | markdownify -}}`, + 521: `{{- i18n "Error521" | markdownify -}}`, }; /** @@ -44,8 +30,7 @@ class EcoIndexDialog { * @returns {boolean} true if success, otherwise true */ static openPendingAnalysis(url) { - let title = ` -{{- (i18n "AnalysisInProgressFor") -}}`; + let title = `{{- (i18n "AnalysisInProgressFor") -}}`; title = replaceKeyIn(title, "URL", url); EcoIndexDialog.#openLoadingRequest(title); @@ -56,8 +41,7 @@ class EcoIndexDialog { * with a loading spinner (abort possible) */ static openAnalysisRetrieval() { - const title = ` -{{- (i18n "AnalysisRetrieval") -}}`; + const title = `{{- (i18n "AnalysisRetrieval") -}}`; EcoIndexDialog.#openLoadingRequest(title); } @@ -71,8 +55,7 @@ class EcoIndexDialog { */ static openErrorMessage(errorCode, details) { // Title - const title = ` -{{- (i18n "AnalysisErrorTitle") -}}`; + const title = `{{- (i18n "AnalysisErrorTitle") -}}`; // Body (message) let errorMessage = errorCode @@ -98,8 +81,7 @@ class EcoIndexDialog { */ static openResultSharing(url) { // Title - const title = ` -{{- (i18n "ShareTheResult") -}}`; + const title = `{{- (i18n "ShareTheResult") -}}`; // Body (message) const body = ` diff --git a/i18n/en.toml b/i18n/en.toml index 7257bb26..ea7737db 100644 --- a/i18n/en.toml +++ b/i18n/en.toml @@ -122,6 +122,12 @@ for “##URL##”""" other = "Please retry later." [Close] other = "Close" +[Error401] + other = "401 error" +[Error403] + other = "403 error" +[Error404] + other = "404 error" [Error422] other = "There is an invalid parameter, check your request." [Error429] @@ -140,8 +146,6 @@ More information on page [How it works](/how-it-works/).""" other = "The page you want to test can not be analyzed because its response status code is not 200" [NoJSMessage] other = "**Warning**: JavaScript is currently required for the new version of ecoIndex to work ([enable JavaScript in your browser](https://www.enable-javascript.com/))." -[Error404] - other = "404 error" [ShareTheResult] other = "Share my result" [CopyURL] diff --git a/i18n/fr.toml b/i18n/fr.toml index d18a4caa..5b67bdbc 100644 --- a/i18n/fr.toml +++ b/i18n/fr.toml @@ -122,6 +122,12 @@ pour « ##URL## »""" other = "Veuillez réessayer plus tard" [Close] other = "Fermer" +[Error401] + other = "Erreur 401" +[Error403] + other = "Erreur 403" +[Error404] + other = "Erreur 404" [Error422] other = "Un paramètre n’est pas valide, pouvez-vous vérifier votre requête ?" [Error429] @@ -140,8 +146,6 @@ Pour en savoir plus, veuiller consulter la page [Comment ça marche ?](/commen other = "La page que vous essayez de tester ne peut pas être analysée car elle est en erreur (son code réponse n'est pas 200)" [NoJSMessage] other = "**Attention** : Javascript est actuellement requis pour le fonctionnement de la nouvelle version d'ecoIndex ([activer JavaScript dans votre navigateur](https://www.enable-javascript.com/fr/))." -[Error404] - other = "Erreur 404" [ShareTheResult] other = "Partager mon résultat" [CopyURL] From 7cef65483bff9aadb771f443173940e46c8b075e Mon Sep 17 00:00:00 2001 From: Bastien Gatellier Date: Sat, 13 Apr 2024 23:08:33 +0200 Subject: [PATCH 2/2] refactor: improve readability --- assets/js/components/SiteAnalysisResult.js | 4 +- assets/js/services/AnalysisService.js | 61 ++++++++-------------- assets/js/services/ApiService.js | 2 +- 3 files changed, 25 insertions(+), 42 deletions(-) diff --git a/assets/js/components/SiteAnalysisResult.js b/assets/js/components/SiteAnalysisResult.js index 84b6746d..39f4425c 100644 --- a/assets/js/components/SiteAnalysisResult.js +++ b/assets/js/components/SiteAnalysisResult.js @@ -52,7 +52,7 @@ class SiteAnalysisResult { pageResultData[key] = value; if (key === "id") { - const screenshotUrl = AnalysisService.fetchAnalysisScreenshotUrlById(urlParams.get("id")) + const screenshotUrl = AnalysisService.getAnalysisScreenshotUrlById(urlParams.get("id")) screenshotImgElement.setAttribute("src", screenshotUrl) } } @@ -71,7 +71,7 @@ class SiteAnalysisResult { // window.location.pathname is something like /resultat (in french) or /en/result (in english) pageResultData = await AnalysisService.fetchAnalysisById(analysisId, window.location.pathname) - const screenshotUrl = AnalysisService.fetchAnalysisScreenshotUrlById(analysisId) + const screenshotUrl = AnalysisService.getAnalysisScreenshotUrlById(analysisId) screenshotImgElement.setAttribute("src", screenshotUrl) // update the link URL of every lang switcher diff --git a/assets/js/services/AnalysisService.js b/assets/js/services/AnalysisService.js index 050dfb0c..53e6bd4d 100644 --- a/assets/js/services/AnalysisService.js +++ b/assets/js/services/AnalysisService.js @@ -22,35 +22,25 @@ class AnalysisService { try { EcoIndexDialog.openPendingAnalysis(url); - ApiService.newAnalysisTaskByURL(url).then( - (taskId) => { - ApiService.fetchAnalysisTaskById(taskId).then( - (taskResult) => { - const ecoindex = taskResult.ecoindex_result; + const taskId = await ApiService.newAnalysisTaskByURL(url) - if (taskResult.status === "SUCCESS" && ecoindex.status === "SUCCESS") { - ResultCacheService.add(ecoindex.detail); - redirectToResults(taskId, resultUrlPrefix); - } + const taskResult = await ApiService.fetchAnalysisTaskById(taskId) + + const ecoindex = taskResult.ecoindex_result; - if (taskResult.status === "SUCCESS" && ecoindex.status === "FAILURE") { - const e = taskResult.ecoindex_result.error; - EcoIndexDialog.openErrorMessage(e.status_code, e); - } + if (taskResult.status === "SUCCESS" && ecoindex.status === "SUCCESS") { + ResultCacheService.add(ecoindex.detail); + redirectToResults(taskId, resultUrlPrefix); + } - if (taskResult.status === "FAILURE") { - EcoIndexDialog.openErrorMessage(599, taskResult.task_error); - } - }, - (e) => { - this.#handleError(e); - } - ); - }, - (e) => { - this.#handleError(e); - } - ); + if (taskResult.status === "SUCCESS" && ecoindex.status === "FAILURE") { + const e = taskResult.ecoindex_result.error; + EcoIndexDialog.openErrorMessage(e.status_code, e); + } + + if (taskResult.status === "FAILURE") { + EcoIndexDialog.openErrorMessage(599, taskResult.task_error); + } } catch (e) { this.#handleError(e); } @@ -60,8 +50,8 @@ class AnalysisService { * @param {string} id * @returns {string} */ - fetchAnalysisScreenshotUrlById(id) { - return ApiService.fetchAnalysisScreenshotUrlById(id) + getAnalysisScreenshotUrlById(id) { + return ApiService.getAnalysisScreenshotUrlById(id) } /** @@ -79,17 +69,10 @@ class AnalysisService { // Otherwise fetch from api try { EcoIndexDialog.openAnalysisRetrieval(); - await ApiService.fetchAnalysisById(id).then( - (result) => { - apiResult = result; - ResultCacheService.add(result); - redirectToResults(result.id, resultPagePrefix); - EcoIndexDialog.close(); - }, - (e) => { - this.#handleError(e); - } - ); + apiResult = await ApiService.fetchAnalysisById(id) + ResultCacheService.add(apiResult); + redirectToResults(apiResult.id, resultPagePrefix); + EcoIndexDialog.close(); } catch (e) { this.#handleError(e); return null; diff --git a/assets/js/services/ApiService.js b/assets/js/services/ApiService.js index dcb6dd2b..78143368 100644 --- a/assets/js/services/ApiService.js +++ b/assets/js/services/ApiService.js @@ -50,7 +50,7 @@ class ApiService { * @param {string} id Analysis Id * @returns {string} API URL */ - fetchAnalysisScreenshotUrlById(id) { + getAnalysisScreenshotUrlById(id) { return `${BASE_URL}ecoindexes/${id}/screenshot`; }