From f87ecabedfa3c1c9be07263e068c7b40fa7c8987 Mon Sep 17 00:00:00 2001 From: Marvin Klein <32510006+MarvinKlein1508@users.noreply.github.com> Date: Wed, 19 Jun 2024 13:21:42 +0200 Subject: [PATCH] Add try catch in FluentDesignTheme to prevent crash on invalid localStorage (#2204) --- .../DesignSystemProvider/FluentDesignTheme.razor.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Core/Components/DesignSystemProvider/FluentDesignTheme.razor.js b/src/Core/Components/DesignSystemProvider/FluentDesignTheme.razor.js index 4749945c23..b1241e9559 100644 --- a/src/Core/Components/DesignSystemProvider/FluentDesignTheme.razor.js +++ b/src/Core/Components/DesignSystemProvider/FluentDesignTheme.razor.js @@ -13,8 +13,15 @@ export function addThemeChangeEvent(dotNetHelper, id) { } }); - const theme = element.themeStorage.readLocalStorage() - return theme == null ? theme : JSON.stringify(theme); + try { + // This can fail when localStorage does not contain a valid JSON object + const theme = element.themeStorage.readLocalStorage() + return theme == null ? theme : JSON.stringify(theme); + } catch (error) { + ClearLocalStorage(id); + console.error(`FluentDesignTheme: failing to load theme from localStorage.`, error); + } + } return null;