diff options
author | Alex Yatskov <alex@foosoft.net> | 2016-11-07 22:31:57 -0800 |
---|---|---|
committer | Alex Yatskov <alex@foosoft.net> | 2016-11-07 22:31:57 -0800 |
commit | af45e8fab63487e0e5ffc43b99ff6c075d38e4ab (patch) | |
tree | 25270ed9de5f18b2730a7a5725ed5da613c15ae8 | |
parent | f2f7b7f3dbf609619fc7de86df53ceec4c931351 (diff) |
Error handling
-rw-r--r-- | ext/bg/js/util.js | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js index 124438ed..914164b1 100644 --- a/ext/bg/js/util.js +++ b/ext/bg/js/util.js @@ -111,10 +111,17 @@ function splitField(field) { function loadJson(url) { return new Promise((resolve, reject) => { const xhr = new XMLHttpRequest(); - xhr.addEventListener('load', () => resolve(JSON.parse(xhr.responseText))); + xhr.addEventListener('load', () => resolve(xhr.responseText)); xhr.addEventListener('error', () => reject('failed to execute network request')); xhr.open('GET', url); xhr.send(); + }).then(responseText => { + try { + return JSON.parse(responseText); + } + catch (e) { + return Promise.reject('invalid JSON response'); + } }); } @@ -125,6 +132,10 @@ function loadJsonInt(url) { function importJsonDb(indexUrl, indexLoaded, termsLoaded, kanjiLoaded) { const indexDir = indexUrl.slice(0, indexUrl.lastIndexOf('/')); return loadJson(indexUrl).then(index => { + if (!index.title || !index.version) { + return Promise.reject('unrecognized dictionary format'); + } + if (indexLoaded !== null) { return indexLoaded( index.title, |