Throw Error instead of string

This commit is contained in:
toasted-nutbread 2019-10-07 20:46:02 -04:00
parent bf5d301685
commit 88de427184
6 changed files with 30 additions and 44 deletions

View File

@ -67,7 +67,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 new Error('Extension and plugin versions incompatible');
} }
} }
} }

View File

@ -75,7 +75,7 @@ class Backend {
const promise = handler(params, sender); const promise = handler(params, sender);
promise promise
.then(result => callback({result})) .then(result => callback({result}))
.catch(error => callback({error: typeof error.toString === 'function' ? error.toString() : error})); .catch(error => callback(errorToJson(error)));
} }
return true; return true;

View File

@ -25,7 +25,7 @@ class Database {
async prepare() { async prepare() {
if (this.db) { if (this.db) {
throw 'Database already initialized'; throw new Error('Database already initialized');
} }
this.db = new Dexie('dict'); this.db = new Dexie('dict');
@ -48,9 +48,7 @@ class Database {
} }
async purge() { async purge() {
if (!this.db) { this.validate();
throw 'Database not initialized';
}
this.db.close(); this.db.close();
await this.db.delete(); await this.db.delete();
@ -61,9 +59,7 @@ class Database {
} }
async findTerms(term, titles) { async findTerms(term, titles) {
if (!this.db) { this.validate();
throw 'Database not initialized';
}
const results = []; const results = [];
await this.db.terms.where('expression').equals(term).or('reading').equals(term).each(row => { await this.db.terms.where('expression').equals(term).or('reading').equals(term).each(row => {
@ -107,9 +103,7 @@ class Database {
} }
async findTermsExact(term, reading, titles) { async findTermsExact(term, reading, titles) {
if (!this.db) { this.validate();
throw 'Database not initialized';
}
const results = []; const results = [];
await this.db.terms.where('expression').equals(term).each(row => { await this.db.terms.where('expression').equals(term).each(row => {
@ -122,9 +116,7 @@ class Database {
} }
async findTermsBySequence(sequence, mainDictionary) { async findTermsBySequence(sequence, mainDictionary) {
if (!this.db) { this.validate();
throw 'Database not initialized';
}
const results = []; const results = [];
await this.db.terms.where('sequence').equals(sequence).each(row => { await this.db.terms.where('sequence').equals(sequence).each(row => {
@ -137,9 +129,7 @@ class Database {
} }
async findTermMeta(term, titles) { async findTermMeta(term, titles) {
if (!this.db) { this.validate();
throw 'Database not initialized';
}
const results = []; const results = [];
await this.db.termMeta.where('expression').equals(term).each(row => { await this.db.termMeta.where('expression').equals(term).each(row => {
@ -181,9 +171,7 @@ class Database {
} }
async findKanji(kanji, titles) { async findKanji(kanji, titles) {
if (!this.db) { this.validate();
throw 'Database not initialized';
}
const results = []; const results = [];
await this.db.kanji.where('character').equals(kanji).each(row => { await this.db.kanji.where('character').equals(kanji).each(row => {
@ -204,9 +192,7 @@ class Database {
} }
async findKanjiMeta(kanji, titles) { async findKanjiMeta(kanji, titles) {
if (!this.db) { this.validate();
throw 'Database not initialized';
}
const results = []; const results = [];
await this.db.kanjiMeta.where('character').equals(kanji).each(row => { await this.db.kanjiMeta.where('character').equals(kanji).each(row => {
@ -232,9 +218,7 @@ class Database {
} }
async findTagForTitle(name, title) { async findTagForTitle(name, title) {
if (!this.db) { this.validate();
throw 'Database not initialized';
}
const cache = (this.tagCache.hasOwnProperty(title) ? this.tagCache[title] : (this.tagCache[title] = {})); const cache = (this.tagCache.hasOwnProperty(title) ? this.tagCache[title] : (this.tagCache[title] = {}));
@ -251,17 +235,13 @@ class Database {
} }
async summarize() { async summarize() {
if (this.db) { this.validate();
return this.db.dictionaries.toArray();
} else { return this.db.dictionaries.toArray();
throw 'Database not initialized';
}
} }
async importDictionary(archive, progressCallback, exceptions) { async importDictionary(archive, progressCallback, exceptions) {
if (!this.db) { this.validate();
throw 'Database not initialized';
}
const maxTransactionLength = 1000; const maxTransactionLength = 1000;
const bulkAdd = async (table, items, total, current) => { const bulkAdd = async (table, items, total, current) => {
@ -301,12 +281,12 @@ class Database {
const indexDataLoaded = async summary => { const indexDataLoaded = async summary => {
if (summary.version > 3) { if (summary.version > 3) {
throw 'Unsupported dictionary version'; throw new Error('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 is already imported'; throw new Error('Dictionary is already imported');
} }
await this.db.dictionaries.add(summary); await this.db.dictionaries.add(summary);
@ -432,6 +412,12 @@ class Database {
); );
} }
validate() {
if (this.db === null) {
throw new Error('Database not initialized');
}
}
static async importDictionaryZip( static async importDictionaryZip(
archive, archive,
indexDataLoaded, indexDataLoaded,
@ -445,12 +431,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 new Error('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 new Error('Unrecognized dictionary format');
} }
const summary = { const summary = {

View File

@ -69,7 +69,7 @@ class PopupProxyHost {
getPopup(id) { getPopup(id) {
if (!this.popups.hasOwnProperty(id)) { if (!this.popups.hasOwnProperty(id)) {
throw 'Invalid popup ID'; throw new Error('Invalid popup ID');
} }
return this.popups[id]; return this.popups[id];

View File

@ -39,11 +39,11 @@ class Display {
} }
onError(error) { onError(error) {
throw 'Override me'; throw new Error('Override me');
} }
onSearchClear() { onSearchClear() {
throw 'Override me'; throw new Error('Override me');
} }
onSourceTermView(e) { onSourceTermView(e) {
@ -386,7 +386,7 @@ class Display {
viewerButton.dataset.noteId = noteId; viewerButton.dataset.noteId = noteId;
} }
} else { } else {
throw 'Note could note be added'; throw new Error('Note could not be added');
} }
} catch (e) { } catch (e) {
this.onError(e); this.onError(e);

View File

@ -34,7 +34,7 @@ function toIterable(value) {
} }
} }
throw 'Could not convert to iterable'; throw new Error('Could not convert to iterable');
} }
function extensionHasChrome() { function extensionHasChrome() {