From 8ba8397170c99e1d4416277e26d91ebb4b8cfed1 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Fri, 22 Sep 2017 19:39:05 -0700 Subject: [PATCH] update error handling --- ext/bg/js/anki.js | 2 +- ext/bg/js/audio.js | 4 ++-- ext/bg/js/database.js | 26 +++++++++++++------------- ext/bg/js/request.js | 4 ++-- ext/bg/js/settings.js | 8 ++++---- ext/bg/settings.html | 23 ++++++----------------- ext/mixed/js/display.js | 6 +++--- 7 files changed, 31 insertions(+), 42 deletions(-) diff --git a/ext/bg/js/anki.js b/ext/bg/js/anki.js index c327969f..183f37bc 100644 --- a/ext/bg/js/anki.js +++ b/ext/bg/js/anki.js @@ -62,7 +62,7 @@ class AnkiConnect { if (this.remoteVersion < this.localVersion) { this.remoteVersion = await this.ankiInvoke('version'); if (this.remoteVersion < this.localVersion) { - throw 'extension and plugin versions incompatible'; + throw 'Extension and plugin versions incompatible'; } } } diff --git a/ext/bg/js/audio.js b/ext/bg/js/audio.js index 0952887e..ce47490c 100644 --- a/ext/bg/js/audio.js +++ b/ext/bg/js/audio.js @@ -57,7 +57,7 @@ async function audioBuildUrl(definition, mode, cache={}) { const xhr = new XMLHttpRequest(); xhr.open('POST', 'https://www.japanesepod101.com/learningcenter/reference/dictionary_post'); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); - xhr.addEventListener('error', () => reject('failed to scrape audio data')); + xhr.addEventListener('error', () => reject('Failed to scrape audio data')); xhr.addEventListener('load', () => { cache[definition.expression] = xhr.responseText; resolve(xhr.responseText); @@ -87,7 +87,7 @@ async function audioBuildUrl(definition, mode, cache={}) { } else { const xhr = new XMLHttpRequest(); xhr.open('GET', `http://jisho.org/search/${definition.expression}`); - xhr.addEventListener('error', () => reject('failed to scrape audio data')); + xhr.addEventListener('error', () => reject('Failed to scrape audio data')); xhr.addEventListener('load', () => { cache[definition.expression] = xhr.responseText; resolve(xhr.responseText); diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js index f94c572e..e7316b3a 100644 --- a/ext/bg/js/database.js +++ b/ext/bg/js/database.js @@ -25,7 +25,7 @@ class Database { async prepare() { if (this.db) { - throw 'database already initialized'; + throw 'Database already initialized'; } this.db = new Dexie('dict'); @@ -46,7 +46,7 @@ class Database { async purge() { if (!this.db) { - throw 'database not initialized'; + throw 'Database not initialized'; } this.db.close(); @@ -59,7 +59,7 @@ class Database { async findTerms(term, titles) { if (!this.db) { - throw 'database not initialized'; + throw 'Database not initialized'; } const results = []; @@ -83,7 +83,7 @@ class Database { async findTermFreq(term, titles) { if (!this.db) { - throw 'database not initialized'; + throw 'Database not initialized'; } const results = []; @@ -98,7 +98,7 @@ class Database { async findKanji(kanji, titles) { if (!this.db) { - return Promise.reject('database not initialized'); + throw 'Database not initialized'; } const results = []; @@ -121,7 +121,7 @@ class Database { async findKanjiFreq(kanji, titles) { if (!this.db) { - throw 'database not initialized'; + throw 'Database not initialized'; } const results = []; @@ -136,7 +136,7 @@ class Database { async findTagForTitle(name, title) { if (!this.db) { - throw 'database not initialized'; + throw 'Database not initialized'; } this.tagCache[title] = this.tagCache[title] || {}; @@ -159,23 +159,23 @@ class Database { if (this.db) { return this.db.dictionaries.toArray(); } else { - throw 'database not initialized'; + throw 'Database not initialized'; } } async importDictionary(archive, callback) { if (!this.db) { - return Promise.reject('database not initialized'); + throw 'Database not initialized'; } const indexDataLoaded = async summary => { if (summary.version > 2) { - throw 'unsupported dictionary version'; + throw 'Unsupported dictionary version'; } const count = await this.db.dictionaries.where('title').equals(summary.title).count(); if (count > 0) { - throw `dictionary "${summary.title}" is already imported`; + throw 'Dictionary is already imported'; } await this.db.dictionaries.add(summary); @@ -329,12 +329,12 @@ class Database { const indexFile = zip.files['index.json']; if (!indexFile) { - throw 'no dictionary index found in archive'; + throw 'No dictionary index found in archive'; } const index = JSON.parse(await indexFile.async('string')); if (!index.title || !index.revision) { - throw 'unrecognized dictionary format'; + throw 'Unrecognized dictionary format'; } const summary = { diff --git a/ext/bg/js/request.js b/ext/bg/js/request.js index 94fd135a..e4359863 100644 --- a/ext/bg/js/request.js +++ b/ext/bg/js/request.js @@ -22,7 +22,7 @@ function requestJson(url, action, params) { const xhr = new XMLHttpRequest(); xhr.overrideMimeType('application/json'); xhr.addEventListener('load', () => resolve(xhr.responseText)); - xhr.addEventListener('error', () => reject('failed to execute network request')); + xhr.addEventListener('error', () => reject('Failed to connect')); xhr.open(action, url); if (params) { xhr.send(JSON.stringify(params)); @@ -34,7 +34,7 @@ function requestJson(url, action, params) { return JSON.parse(responseText); } catch (e) { - return Promise.reject('invalid JSON response'); + return Promise.reject('Invalid response'); } }); } diff --git a/ext/bg/js/settings.js b/ext/bg/js/settings.js index d73d7509..161e9abe 100644 --- a/ext/bg/js/settings.js +++ b/ext/bg/js/settings.js @@ -142,7 +142,7 @@ async function onReady() { $('#scan-length').val(options.scanning.length); $('#scan-modifier-key').val(options.scanning.modifier); - $('#dict-purge').click(utilAsync(onDictionaryPurge)); + $('#dict-purge-link').click(utilAsync(onDictionaryPurge)); $('#dict-file').change(utilAsync(onDictionaryImport)); $('#anki-enable').prop('checked', options.anki.enable); @@ -179,7 +179,7 @@ $(document).ready(utilAsync(onReady)); function dictionaryErrorShow(error) { const dialog = $('#dict-error'); if (error) { - dialog.show().find('span').text(error); + dialog.show().text(error); } else { dialog.hide(); } @@ -245,7 +245,7 @@ async function onDictionaryPurge(e) { e.preventDefault(); const dictControls = $('#dict-importer, #dict-groups').hide(); - const dictProgress = $('#dict-purge-progress').show(); + const dictProgress = $('#dict-purge').show(); try { dictionaryErrorShow(); @@ -314,7 +314,7 @@ function ankiSpinnerShow(show) { function ankiErrorShow(error) { const dialog = $('#anki-error'); if (error) { - dialog.show().find('span').text(error); + dialog.show().text(error); } else { dialog.hide(); diff --git a/ext/bg/settings.html b/ext/bg/settings.html index c2612967..4315d74b 100644 --- a/ext/bg/settings.html +++ b/ext/bg/settings.html @@ -7,7 +7,7 @@