Skip to content

Commit

Permalink
Merge pull request #644 from nextcloud/fix/handle-edge-cases-load-tra…
Browse files Browse the repository at this point in the history
…nslations
  • Loading branch information
skjnldsv committed Jun 25, 2023
2 parents 965b02a + a605fd2 commit 03070c9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
10 changes: 7 additions & 3 deletions lib/translation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,13 @@ export function loadTranslations(appName: string, callback: (...args: []) => unk
}
request.onload = () => {
if (request.status >= 200 && request.status < 300) {
const bundle = JSON.parse(request.responseText)
if (bundle?.translations) resolve(bundle)
else reject(new Error('Invalid content of translation bundle'))
try {
const bundle = JSON.parse(request.responseText)
if (typeof bundle.translations === 'object') resolve(bundle)
} catch (error) {
// error is probably a SyntaxError due to invalid response text, this is handled by next line
}
reject(new Error('Invalid content of translation bundle'))
} else {
reject(new Error(request.statusText))
}
Expand Down
39 changes: 27 additions & 12 deletions tests/loadTranslations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,25 @@ describe('loadTranslations', () => {
},
}),
})
.addHandler('GET', '/invalid/l10n/de.json', {
// Response with empty body
.addHandler('GET', '/empty/l10n/de.json', {
status: 200,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
strings: {
'Hello world!': 'Hallo Welt!',
},
}),
body: '',
})
.addHandler('GET', '/empty/l10n/de.json', {
// Response contains JSON but no translations
.addHandler('GET', '/missing-bundle/l10n/de.json', {
status: 200,
headers: { 'Content-Type': 'application/json' },
body: '',
body: JSON.stringify({}),
})
// Response contains JSON but the translations are invalid
.addHandler('GET', '/invalid/l10n/de.json', {
status: 200,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
translations: 'invalid',
}),
})
.addHandler('GET', '/404/l10n/de.json', {
status: 404,
Expand Down Expand Up @@ -104,8 +110,6 @@ describe('loadTranslations', () => {
expect(translate('myapp', 'Hello world!')).toBe('Hallo Welt!')
} catch (e) {
expect(e).toBe('Unexpected error')
} finally {
console.warn(server.getRequestLog()[0])
}
})

Expand Down Expand Up @@ -153,10 +157,21 @@ describe('loadTranslations', () => {
}
})

it('does reject on empty bundle', async () => {
it('does reject on missing bundle', async () => {
const callback = jest.fn()
try {
await loadTranslations('invalid', callback)
await loadTranslations('missing-bundle', callback)
expect('').toBe('Unexpected pass')
} catch (e) {
expect(e instanceof Error).toBe(true)
expect((<Error>e).message).toBe('Invalid content of translation bundle')
}
})

it('does reject on empty response', async () => {
const callback = jest.fn()
try {
await loadTranslations('empty', callback)
expect('').toBe('Unexpected pass')
} catch (e) {
expect(e instanceof Error).toBe(true)
Expand Down

0 comments on commit 03070c9

Please sign in to comment.