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(loadTranslations): Handle edge cases #644

Merged
merged 1 commit into from
Jun 25, 2023
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
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