From 6bef6d89d280b4ffa7c39526d5f49795e5967e56 Mon Sep 17 00:00:00 2001 From: Robert Pocklington Date: Fri, 13 Aug 2021 12:12:46 +1000 Subject: [PATCH 1/2] fix: handle loading and parsing errors when loading themes --- packages/web-runtime/src/helpers/theme.js | 19 ++++++++++--------- .../tests/unit/helpers/theme.spec.js | 16 +++++++++++++--- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/packages/web-runtime/src/helpers/theme.js b/packages/web-runtime/src/helpers/theme.js index 825e56b9c6d..67e3a4c6bad 100644 --- a/packages/web-runtime/src/helpers/theme.js +++ b/packages/web-runtime/src/helpers/theme.js @@ -4,20 +4,21 @@ export const loadTheme = async (location = '') => { const defaults = { theme: defaultTheme } if (location.split('.').pop() !== 'json') { + console.error(`Theme '${location}' does not specify a json file, using default theme.`) return defaults } - let response try { - response = await fetch(location) + const response = await fetch(location) + if (!response.ok) { + return defaults + } + const theme = await response.json() + return { theme } } catch (e) { + console.error( + `Failed to load theme '${location}' is not a valid json file, using default theme.` + ) return defaults } - - if (!response.ok) { - return defaults - } - - const theme = await response.json() - return { theme } } diff --git a/packages/web-runtime/tests/unit/helpers/theme.spec.js b/packages/web-runtime/tests/unit/helpers/theme.spec.js index 719fa09b421..36da68a51d6 100644 --- a/packages/web-runtime/tests/unit/helpers/theme.spec.js +++ b/packages/web-runtime/tests/unit/helpers/theme.spec.js @@ -4,7 +4,10 @@ import merge from 'lodash-es/merge' describe('theme loading and error reporting', () => { beforeEach(() => { - fetch.resetMocks() + global.console = { error: jest.fn() } + }) + afterEach(() => { + jest.clearAllMocks() }) it('should load the default theme if location is empty', async () => { @@ -12,17 +15,24 @@ describe('theme loading and error reporting', () => { expect(theme).toMatchObject(defaultTheme) }) - it('should load the default theme if location is not a json file', async () => { + it('should load the default theme if location is not a json file extension', async () => { const { theme } = await loadTheme('some_location_without_json_file_ending.xml') expect(theme).toMatchObject(defaultTheme) }) - it('should load the default theme if location errors', async () => { + it('should load the default theme if location is not found', async () => { fetch.mockResponse(new Error(), { status: 404 }) const { theme } = await loadTheme('http://www.owncloud.com/unknown.json') expect(theme).toMatchObject(defaultTheme) }) + it('should load the default theme if location is not a valid json file', async () => { + const customTheme = merge({}, defaultTheme, { default: { logo: { login: 'custom.svg' } } }) + fetch.mockResponse(JSON.stringify(customTheme) + '-invalid') + const { theme } = await loadTheme('http://www.owncloud.com/invalid.json') + expect(theme).toMatchObject(defaultTheme) + }) + it('should load the default theme if server errors', async () => { fetch.mockReject(new Error()) const { theme } = await loadTheme('http://www.owncloud.com') From c7bb70bff75b64d00a7bad3e65b3248ccf2760e0 Mon Sep 17 00:00:00 2001 From: Robert Pocklington Date: Fri, 13 Aug 2021 12:54:49 +1000 Subject: [PATCH 2/2] Add changelog item --- .../unreleased/bugfix-handle-errors-when-loading-themes | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 changelog/unreleased/bugfix-handle-errors-when-loading-themes diff --git a/changelog/unreleased/bugfix-handle-errors-when-loading-themes b/changelog/unreleased/bugfix-handle-errors-when-loading-themes new file mode 100644 index 00000000000..cb2a2f070e4 --- /dev/null +++ b/changelog/unreleased/bugfix-handle-errors-when-loading-themes @@ -0,0 +1,5 @@ +Bugfix: Handle loading and parsing errors when loading themes + +Adds graceful error handling of json parse errors when loading custom themes. + +https://github.com/owncloud/web/pull/5669