Improved error handling

This commit is contained in:
Alex Yatskov 2016-10-19 21:21:00 -07:00
parent 7618b8e909
commit ba02ae7f8d
4 changed files with 33 additions and 25 deletions

View File

@ -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();

View File

@ -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) {
for (let resultBase = 0; resultBase < raw.length; resultBase += modes.length) {
const state = {};
for (let modeOffset = 0; modeOffset < modes.length; ++modeOffset) {
state[modes[modeOffset]] = results[resultBase + modeOffset];
state[modes[modeOffset]] = raw[resultBase + modeOffset];
}
states.push(state);
}
}
callback(states, null);
callback({result: states});
}).catch(error => {
callback(null, error);
callback({error});
});
}
}

View File

@ -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);
});
}
}

View File

@ -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 {