update error handling

This commit is contained in:
Alex Yatskov 2017-09-22 19:39:05 -07:00
parent 27296de9f5
commit 8ba8397170
7 changed files with 31 additions and 42 deletions

View File

@ -62,7 +62,7 @@ class AnkiConnect {
if (this.remoteVersion < this.localVersion) { if (this.remoteVersion < this.localVersion) {
this.remoteVersion = await this.ankiInvoke('version'); this.remoteVersion = await this.ankiInvoke('version');
if (this.remoteVersion < this.localVersion) { if (this.remoteVersion < this.localVersion) {
throw 'extension and plugin versions incompatible'; throw 'Extension and plugin versions incompatible';
} }
} }
} }

View File

@ -57,7 +57,7 @@ async function audioBuildUrl(definition, mode, cache={}) {
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
xhr.open('POST', 'https://www.japanesepod101.com/learningcenter/reference/dictionary_post'); xhr.open('POST', 'https://www.japanesepod101.com/learningcenter/reference/dictionary_post');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 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', () => { xhr.addEventListener('load', () => {
cache[definition.expression] = xhr.responseText; cache[definition.expression] = xhr.responseText;
resolve(xhr.responseText); resolve(xhr.responseText);
@ -87,7 +87,7 @@ async function audioBuildUrl(definition, mode, cache={}) {
} else { } else {
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
xhr.open('GET', `http://jisho.org/search/${definition.expression}`); 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', () => { xhr.addEventListener('load', () => {
cache[definition.expression] = xhr.responseText; cache[definition.expression] = xhr.responseText;
resolve(xhr.responseText); resolve(xhr.responseText);

View File

@ -25,7 +25,7 @@ class Database {
async prepare() { async prepare() {
if (this.db) { if (this.db) {
throw 'database already initialized'; throw 'Database already initialized';
} }
this.db = new Dexie('dict'); this.db = new Dexie('dict');
@ -46,7 +46,7 @@ class Database {
async purge() { async purge() {
if (!this.db) { if (!this.db) {
throw 'database not initialized'; throw 'Database not initialized';
} }
this.db.close(); this.db.close();
@ -59,7 +59,7 @@ class Database {
async findTerms(term, titles) { async findTerms(term, titles) {
if (!this.db) { if (!this.db) {
throw 'database not initialized'; throw 'Database not initialized';
} }
const results = []; const results = [];
@ -83,7 +83,7 @@ class Database {
async findTermFreq(term, titles) { async findTermFreq(term, titles) {
if (!this.db) { if (!this.db) {
throw 'database not initialized'; throw 'Database not initialized';
} }
const results = []; const results = [];
@ -98,7 +98,7 @@ class Database {
async findKanji(kanji, titles) { async findKanji(kanji, titles) {
if (!this.db) { if (!this.db) {
return Promise.reject('database not initialized'); throw 'Database not initialized';
} }
const results = []; const results = [];
@ -121,7 +121,7 @@ class Database {
async findKanjiFreq(kanji, titles) { async findKanjiFreq(kanji, titles) {
if (!this.db) { if (!this.db) {
throw 'database not initialized'; throw 'Database not initialized';
} }
const results = []; const results = [];
@ -136,7 +136,7 @@ class Database {
async findTagForTitle(name, title) { async findTagForTitle(name, title) {
if (!this.db) { if (!this.db) {
throw 'database not initialized'; throw 'Database not initialized';
} }
this.tagCache[title] = this.tagCache[title] || {}; this.tagCache[title] = this.tagCache[title] || {};
@ -159,23 +159,23 @@ class Database {
if (this.db) { if (this.db) {
return this.db.dictionaries.toArray(); return this.db.dictionaries.toArray();
} else { } else {
throw 'database not initialized'; throw 'Database not initialized';
} }
} }
async importDictionary(archive, callback) { async importDictionary(archive, callback) {
if (!this.db) { if (!this.db) {
return Promise.reject('database not initialized'); throw 'Database not initialized';
} }
const indexDataLoaded = async summary => { const indexDataLoaded = async summary => {
if (summary.version > 2) { if (summary.version > 2) {
throw 'unsupported dictionary version'; throw 'Unsupported dictionary version';
} }
const count = await this.db.dictionaries.where('title').equals(summary.title).count(); const count = await this.db.dictionaries.where('title').equals(summary.title).count();
if (count > 0) { if (count > 0) {
throw `dictionary "${summary.title}" is already imported`; throw 'Dictionary is already imported';
} }
await this.db.dictionaries.add(summary); await this.db.dictionaries.add(summary);
@ -329,12 +329,12 @@ class Database {
const indexFile = zip.files['index.json']; const indexFile = zip.files['index.json'];
if (!indexFile) { if (!indexFile) {
throw 'no dictionary index found in archive'; throw 'No dictionary index found in archive';
} }
const index = JSON.parse(await indexFile.async('string')); const index = JSON.parse(await indexFile.async('string'));
if (!index.title || !index.revision) { if (!index.title || !index.revision) {
throw 'unrecognized dictionary format'; throw 'Unrecognized dictionary format';
} }
const summary = { const summary = {

View File

@ -22,7 +22,7 @@ function requestJson(url, action, params) {
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
xhr.overrideMimeType('application/json'); xhr.overrideMimeType('application/json');
xhr.addEventListener('load', () => resolve(xhr.responseText)); 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); xhr.open(action, url);
if (params) { if (params) {
xhr.send(JSON.stringify(params)); xhr.send(JSON.stringify(params));
@ -34,7 +34,7 @@ function requestJson(url, action, params) {
return JSON.parse(responseText); return JSON.parse(responseText);
} }
catch (e) { catch (e) {
return Promise.reject('invalid JSON response'); return Promise.reject('Invalid response');
} }
}); });
} }

View File

@ -142,7 +142,7 @@ async function onReady() {
$('#scan-length').val(options.scanning.length); $('#scan-length').val(options.scanning.length);
$('#scan-modifier-key').val(options.scanning.modifier); $('#scan-modifier-key').val(options.scanning.modifier);
$('#dict-purge').click(utilAsync(onDictionaryPurge)); $('#dict-purge-link').click(utilAsync(onDictionaryPurge));
$('#dict-file').change(utilAsync(onDictionaryImport)); $('#dict-file').change(utilAsync(onDictionaryImport));
$('#anki-enable').prop('checked', options.anki.enable); $('#anki-enable').prop('checked', options.anki.enable);
@ -179,7 +179,7 @@ $(document).ready(utilAsync(onReady));
function dictionaryErrorShow(error) { function dictionaryErrorShow(error) {
const dialog = $('#dict-error'); const dialog = $('#dict-error');
if (error) { if (error) {
dialog.show().find('span').text(error); dialog.show().text(error);
} else { } else {
dialog.hide(); dialog.hide();
} }
@ -245,7 +245,7 @@ async function onDictionaryPurge(e) {
e.preventDefault(); e.preventDefault();
const dictControls = $('#dict-importer, #dict-groups').hide(); const dictControls = $('#dict-importer, #dict-groups').hide();
const dictProgress = $('#dict-purge-progress').show(); const dictProgress = $('#dict-purge').show();
try { try {
dictionaryErrorShow(); dictionaryErrorShow();
@ -314,7 +314,7 @@ function ankiSpinnerShow(show) {
function ankiErrorShow(error) { function ankiErrorShow(error) {
const dialog = $('#anki-error'); const dialog = $('#anki-error');
if (error) { if (error) {
dialog.show().find('span').text(error); dialog.show().text(error);
} }
else { else {
dialog.hide(); dialog.hide();

View File

@ -7,7 +7,7 @@
<link rel="stylesheet" type="text/css" href="/mixed/lib/bootstrap/css/bootstrap-theme.min.css"> <link rel="stylesheet" type="text/css" href="/mixed/lib/bootstrap/css/bootstrap-theme.min.css">
<style> <style>
#anki-spinner, #anki-general, #anki-error, #anki-spinner, #anki-general, #anki-error,
#dict-spinner, #dict-error, #dict-warning, #dict-purge-progress, #dict-import-progress, #dict-spinner, #dict-error, #dict-warning, #dict-purge, #dict-import-progress,
#debug, .options-advanced { #debug, .options-advanced {
display: none; display: none;
} }
@ -129,24 +129,16 @@
<p class="help-block"> <p class="help-block">
Yomichan can import and use a variety of dictionary formats. Unneeded dictionaries can be disabled, Yomichan can import and use a variety of dictionary formats. Unneeded dictionaries can be disabled,
or you can simply <a href="#" id="dict-purge">purge the database</a> to delete everything. or you can simply <a href="#" id="dict-purge-link">purge the database</a> to delete everything.
</p> </p>
<p class="help-block"> <p class="help-block">
Please visit the <a href="https://foosoft.net/projects/yomichan" target="_blank">Yomichan</a> homepage to download free Please visit the <a href="https://foosoft.net/projects/yomichan" target="_blank">Yomichan</a> homepage to download free
dictionaries that you can use with this extension. dictionaries that you can use with this extension.
</p> </p>
<div id="dict-purge-progress" class="text-danger">Dictionary data is being purged, please be patient...</div> <div class="text-danger" id="dict-purge">Dictionary data is being purged, please be patient...</div>
<div class="alert alert-warning" id="dict-warning">No dictionaries have been installed</div>
<div class="alert alert-warning" id="dict-warning"> <div class="alert alert-danger" id="dict-error"></div>
<strong>Warning:</strong>
<span>no dictionaries found; please use the importer below to install packaged dictionaries</span>
</div>
<div class="alert alert-danger" id="dict-error">
<strong>Error:</strong>
<span></span>
</div>
<div id="dict-groups"></div> <div id="dict-groups"></div>
@ -174,10 +166,7 @@
<a href="https://foosoft.net/projects/anki-connect/" target="_blank">AnkiConnect</a> plugin. <a href="https://foosoft.net/projects/anki-connect/" target="_blank">AnkiConnect</a> plugin.
</p> </p>
<div class="alert alert-danger" id="anki-error"> <div class="alert alert-danger" id="anki-error"></div>
<strong>Error:</strong>
<span></span>
</div>
<div class="checkbox"> <div class="checkbox">
<label><input type="checkbox" id="anki-enable"> Enable Anki integration</label> <label><input type="checkbox" id="anki-enable"> Enable Anki integration</label>

View File

@ -32,11 +32,11 @@ class Display {
} }
onError(error) { onError(error) {
throw 'override me'; throw 'Override me';
} }
onSearchClear() { onSearchClear() {
throw 'override me'; throw 'Override me';
} }
onSourceTermView(e) { onSourceTermView(e) {
@ -350,7 +350,7 @@ class Display {
Display.adderButtonFind(index, mode).addClass('disabled'); Display.adderButtonFind(index, mode).addClass('disabled');
Display.viewerButtonFind(index).removeClass('pending disabled').data('noteId', noteId); Display.viewerButtonFind(index).removeClass('pending disabled').data('noteId', noteId);
} else { } else {
throw 'note could note be added'; throw 'Note could note be added';
} }
} catch (e) { } catch (e) {
this.onError(e); this.onError(e);