aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Yatskov <alex@foosoft.net>2016-10-19 21:21:00 -0700
committerAlex Yatskov <alex@foosoft.net>2016-10-19 21:21:00 -0700
commitba02ae7f8dca794032959c62f72603b2f7814e68 (patch)
treea62a1ac8ec72d590253970c7dc5ab90dda6adc1a
parent7618b8e90904e354f43d3447363e2c5a1f98d357 (diff)
Improved error handling
-rw-r--r--ext/bg/js/ankiweb.js6
-rw-r--r--ext/bg/js/yomichan.js40
-rw-r--r--ext/fg/js/driver.js10
-rw-r--r--ext/fg/js/util.js2
4 files changed, 33 insertions, 25 deletions
diff --git a/ext/bg/js/ankiweb.js b/ext/bg/js/ankiweb.js
index bed605ec..41bf74bd 100644
--- a/ext/bg/js/ankiweb.js
+++ b/ext/bg/js/ankiweb.js
@@ -27,7 +27,7 @@ class AnkiWeb {
return this.retrieve().then(info => {
const model = info.models.find(m => m.name === note.modelName);
if (!model) {
- return Promise.reject('invalid model');
+ return Promise.reject('cannot add note model provided');
}
const fields = [];
@@ -110,7 +110,7 @@ class AnkiWeb {
static login(username, password) {
if (username.length === 0 || password.length === 0) {
- return Promise.reject('unspecified login credentials');
+ return Promise.reject('login credentials not specified');
}
const data = {username, password, submitted: 1};
@@ -143,7 +143,7 @@ class AnkiWeb {
}
const xhr = new XMLHttpRequest();
- xhr.addEventListener('error', () => reject('failed to execute request'));
+ xhr.addEventListener('error', () => reject('failed to execute network request'));
xhr.addEventListener('load', () => resolve(xhr.responseText));
xhr.open('GET', url);
xhr.send();
diff --git a/ext/bg/js/yomichan.js b/ext/bg/js/yomichan.js
index 416a4911..4ba44588 100644
--- a/ext/bg/js/yomichan.js
+++ b/ext/bg/js/yomichan.js
@@ -231,43 +231,43 @@ class Yomichan {
}
api_getEnabled({callback}) {
- callback(this.state === 'enabled', null);
+ callback({result: this.state === 'enabled'});
}
api_getOptions({callback}) {
loadOptions().then(result => {
- callback(result, null);
+ callback({result});
}).catch(error => {
- callback(null, error);
+ callback({error});
});
}
api_findKanji({text, callback}) {
this.translator.findKanji(text).then(result => {
- callback(result, null);
+ callback({result});
}).catch(error => {
- callback(null, error);
+ callback({error});
});
}
api_findTerm({text, callback}) {
this.translator.findTerm(text).then(result => {
- callback(result, null);
+ callback({result});
}).catch(error => {
- callback(null, error);
+ callback({error});
});
}
api_renderText({template, data, callback}) {
- callback(Handlebars.templates[template](data), null);
+ callback({result: Handlebars.templates[template](data)});
}
api_addDefinition({definition, mode, callback}) {
const note = this.formatNote(definition, mode);
this.anki.addNote(note).then(result => {
- callback(result, null);
+ callback({result});
}).catch(error => {
- callback(null, error);
+ callback({error});
});
}
@@ -279,22 +279,20 @@ class Yomichan {
}
}
- this.anki.canAddNotes(notes).then(results => {
+ this.anki.canAddNotes(notes).then(raw => {
const states = [];
- if (results !== null) {
- for (let resultBase = 0; resultBase < results.length; resultBase += modes.length) {
- const state = {};
- for (let modeOffset = 0; modeOffset < modes.length; ++modeOffset) {
- state[modes[modeOffset]] = results[resultBase + modeOffset];
- }
-
- states.push(state);
+ for (let resultBase = 0; resultBase < raw.length; resultBase += modes.length) {
+ const state = {};
+ for (let modeOffset = 0; modeOffset < modes.length; ++modeOffset) {
+ state[modes[modeOffset]] = raw[resultBase + modeOffset];
}
+
+ states.push(state);
}
- callback(states, null);
+ callback({result: states});
}).catch(error => {
- callback(null, error);
+ callback({error});
});
}
}
diff --git a/ext/fg/js/driver.js b/ext/fg/js/driver.js
index 75dbe219..9aab7950 100644
--- a/ext/fg/js/driver.js
+++ b/ext/fg/js/driver.js
@@ -163,6 +163,8 @@ class Driver {
return true;
});
}
+ }).catch(error => {
+ alert('Error: ' + error);
});
}
@@ -202,6 +204,8 @@ class Driver {
return true;
});
}
+ }).catch(error => {
+ alert('Error: ' + error);
});
}
@@ -231,6 +235,8 @@ class Driver {
}
});
}
+ }).catch(error => {
+ alert('Error: ' + error);
});
}
@@ -273,6 +279,8 @@ class Driver {
} else {
alert('Note could not be added');
}
+ }).catch(error => {
+ alert('Error: ' + error);
});
}
@@ -317,6 +325,8 @@ class Driver {
states.forEach((state, index) => this.popup.invokeApi('setActionState', {index, state, sequence}));
}
});
+ }).catch(error => {
+ alert('Error: ' + error);
});
}
}
diff --git a/ext/fg/js/util.js b/ext/fg/js/util.js
index 7248ae86..6f2fd28d 100644
--- a/ext/fg/js/util.js
+++ b/ext/fg/js/util.js
@@ -19,7 +19,7 @@
function invokeApiBg(action, params) {
return new Promise((resolve, reject) => {
- chrome.runtime.sendMessage({action, params}, (result, error) => {
+ chrome.runtime.sendMessage({action, params}, ({result, error}) => {
if (error) {
reject(error);
} else {