Show error status for prepare() errors

This commit is contained in:
toasted-nutbread 2020-04-11 20:58:52 -04:00
parent 5c5c70326d
commit dee7d924a8

View File

@ -77,6 +77,7 @@ class Backend {
this._defaultBrowserActionTitle = null;
this._isPrepared = false;
this._prepareError = false;
this._badgePrepareDelayTimer = null;
this._messageHandlers = new Map([
@ -123,24 +124,21 @@ class Backend {
}
async prepare() {
try {
this._defaultBrowserActionTitle = await this._getBrowserIconTitle();
this._badgePrepareDelayTimer = setTimeout(() => {
this._badgePrepareDelayTimer = null;
this._updateBadge();
}, 1000);
this._updateBadge();
await this.database.prepare();
await this.translator.prepare();
this.optionsSchema = await requestJson(chrome.runtime.getURL('/bg/data/options-schema.json'), 'GET');
this.defaultAnkiFieldTemplates = await requestText(chrome.runtime.getURL('/bg/data/default-anki-field-templates.handlebars'), 'GET');
this.options = await optionsLoad();
try {
this.options = JsonSchema.getValidValueOrDefault(this.optionsSchema, this.options);
} catch (e) {
// This shouldn't happen, but catch errors just in case of bugs
logError(e);
}
this.onOptionsUpdated('background');
@ -163,14 +161,20 @@ class Backend {
const callback = () => this.checkLastError(chrome.runtime.lastError);
chrome.runtime.sendMessage({action: 'backendPrepared'}, callback);
this._isPrepared = true;
} catch (e) {
this._prepareError = true;
logError(e);
throw e;
} finally {
if (this._badgePrepareDelayTimer !== null) {
clearTimeout(this._badgePrepareDelayTimer);
this._badgePrepareDelayTimer = null;
}
this._isPrepared = true;
this._updateBadge();
}
}
isPrepared() {
return this._isPrepared;
@ -888,7 +892,11 @@ class Backend {
let status = null;
if (!this._isPrepared) {
if (this._badgePrepareDelayTimer === null) {
if (this._prepareError !== null) {
text = '!!';
color = '#f04e4e';
status = 'Error';
} else if (this._badgePrepareDelayTimer === null) {
text = '...';
color = '#f0ad4e';
status = 'Loading';