From 014f2053e5360baa98762f163f11e88843eed9a8 Mon Sep 17 00:00:00 2001 From: Stas Vilchik Date: Mon, 12 Sep 2016 13:05:15 +0200 Subject: [PATCH] SONAR-7941 When l10n bundle is corrupted or missing UI fails to render --- server/sonar-web/src/main/js/helpers/l10n.js | 30 ++++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/server/sonar-web/src/main/js/helpers/l10n.js b/server/sonar-web/src/main/js/helpers/l10n.js index 3c8fd0d99d4..89954667d93 100644 --- a/server/sonar-web/src/main/js/helpers/l10n.js +++ b/server/sonar-web/src/main/js/helpers/l10n.js @@ -54,6 +54,21 @@ function makeRequest (params) { }); } +function checkCachedBundle () { + const cached = localStorage.getItem('l10n.bundle'); + + if (!cached) { + return false; + } + + try { + const parsed = JSON.parse(cached); + return parsed != null && typeof parsed === 'object'; + } catch (e) { + return false; + } +} + export function requestMessages () { const currentLocale = getCurrentLocale(); const cachedLocale = localStorage.getItem('l10n.locale'); @@ -64,15 +79,20 @@ export function requestMessages () { const bundleTimestamp = localStorage.getItem('l10n.timestamp'); const params = { locale: currentLocale }; - if (bundleTimestamp !== null) { + if (bundleTimestamp !== null && checkCachedBundle()) { params.ts = bundleTimestamp; } return makeRequest(params).then(bundle => { - const currentTimestamp = moment().format('YYYY-MM-DDTHH:mm:ssZZ'); - localStorage.setItem('l10n.timestamp', currentTimestamp); - localStorage.setItem('l10n.locale', currentLocale); - localStorage.setItem('l10n.bundle', JSON.stringify(bundle)); + try { + const currentTimestamp = moment().format('YYYY-MM-DDTHH:mm:ssZZ'); + localStorage.setItem('l10n.timestamp', currentTimestamp); + localStorage.setItem('l10n.locale', currentLocale); + localStorage.setItem('l10n.bundle', JSON.stringify(bundle)); + } catch (e) { + // do nothing + } + messages = bundle; }); } -- 2.39.5