Improve error handling with native app comms for settings (#94)

This commit is contained in:
Erayd
2019-04-14 03:54:13 +12:00
committed by GitHub
parent 8cf375d4ba
commit 0a06ba6009
4 changed files with 23 additions and 5 deletions

View File

@@ -392,7 +392,7 @@ async function getFullSettings() {
});
var response = await hostAction(configureSettings, "configure");
if (response.status != "ok") {
throw new Error(JSON.stringify(response)); // TODO handle host error
settings.hostError = response;
}
settings.version = response.version;
if (Object.keys(settings.stores).length > 0) {
@@ -411,7 +411,7 @@ async function getFullSettings() {
}
}
}
} else {
} else if (response.status == "ok") {
// no user-configured stores, so use the default store
settings.stores.default = {
id: "default",
@@ -593,7 +593,7 @@ async function handleMessage(settings, message, sendResponse) {
} catch (e) {
sendResponse({
status: "error",
message: "Unable to save settings" + e.toString()
message: e.message
});
}
break;
@@ -863,9 +863,10 @@ async function saveSettings(settings) {
// 'default' is our reserved name for the default store
delete settingsToSave.stores.default;
// verify that the native host is happy with the provided settings
var response = await hostAction(settingsToSave, "configure");
if (response.status != "ok") {
throw new Error(JSON.stringify(response)); // TODO handle host error
throw new Error(`${response.params.message}: ${response.params.error}`);
}
// before save, make sure to remove store settings that we receive from the host app

View File

@@ -76,6 +76,10 @@ function view(ctl, params) {
if (typeof this.error !== "undefined") {
nodes.push(m("div.error", this.error.message));
}
if (this.settings.hasOwnProperty("hostError")) {
let hostError = this.settings.hostError;
nodes.push(m("div.error", hostError.params.message));
}
nodes.push(
m(
@@ -84,7 +88,7 @@ function view(ctl, params) {
disabled: !this.saveEnabled,
onclick: async () => {
try {
await this.saveSettings(this.settings);
this.settings = await this.saveSettings(this.settings);
this.error = undefined;
} catch (e) {
this.error = e;

View File

@@ -33,6 +33,7 @@ function handleError(error, type = "error") {
* @since 3.0.0
*
* @param object settings Settings object
* @return object Settings object
*/
async function saveSettings(settings) {
var response = await chrome.runtime.sendMessage({
@@ -42,6 +43,14 @@ async function saveSettings(settings) {
if (response.status != "ok") {
throw new Error(response.message);
}
// reload settings
var response = await chrome.runtime.sendMessage({ action: "getSettings" });
if (response.status != "ok") {
throw new Error(response.message);
}
return response.settings;
}
/**

View File

@@ -67,6 +67,10 @@ async function run() {
}
var settings = response.settings;
if (settings.hasOwnProperty("hostError")) {
throw new Error(settings.hostError.params.message);
}
if (typeof settings.host === "undefined") {
throw new Error("Unable to retrieve current tab information");
}