Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle loading and parsing errors when loading themes #5669

Merged
merged 2 commits into from
Aug 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog/unreleased/bugfix-handle-errors-when-loading-themes
Original file line number Diff line number Diff line change
@@ -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
19 changes: 10 additions & 9 deletions packages/web-runtime/src/helpers/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
16 changes: 13 additions & 3 deletions packages/web-runtime/tests/unit/helpers/theme.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,35 @@ 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 () => {
const { theme } = await loadTheme()
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')
Expand Down