From af45e8fab63487e0e5ffc43b99ff6c075d38e4ab Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Mon, 7 Nov 2016 22:31:57 -0800 Subject: [PATCH] Error handling --- ext/bg/js/util.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) 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,