Improve error handling with native app comms for settings (#94)
This commit is contained in:
@@ -392,7 +392,7 @@ async function getFullSettings() {
|
|||||||
});
|
});
|
||||||
var response = await hostAction(configureSettings, "configure");
|
var response = await hostAction(configureSettings, "configure");
|
||||||
if (response.status != "ok") {
|
if (response.status != "ok") {
|
||||||
throw new Error(JSON.stringify(response)); // TODO handle host error
|
settings.hostError = response;
|
||||||
}
|
}
|
||||||
settings.version = response.version;
|
settings.version = response.version;
|
||||||
if (Object.keys(settings.stores).length > 0) {
|
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
|
// no user-configured stores, so use the default store
|
||||||
settings.stores.default = {
|
settings.stores.default = {
|
||||||
id: "default",
|
id: "default",
|
||||||
@@ -593,7 +593,7 @@ async function handleMessage(settings, message, sendResponse) {
|
|||||||
} catch (e) {
|
} catch (e) {
|
||||||
sendResponse({
|
sendResponse({
|
||||||
status: "error",
|
status: "error",
|
||||||
message: "Unable to save settings" + e.toString()
|
message: e.message
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -863,9 +863,10 @@ async function saveSettings(settings) {
|
|||||||
// 'default' is our reserved name for the default store
|
// 'default' is our reserved name for the default store
|
||||||
delete settingsToSave.stores.default;
|
delete settingsToSave.stores.default;
|
||||||
|
|
||||||
|
// verify that the native host is happy with the provided settings
|
||||||
var response = await hostAction(settingsToSave, "configure");
|
var response = await hostAction(settingsToSave, "configure");
|
||||||
if (response.status != "ok") {
|
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
|
// before save, make sure to remove store settings that we receive from the host app
|
||||||
|
@@ -76,6 +76,10 @@ function view(ctl, params) {
|
|||||||
if (typeof this.error !== "undefined") {
|
if (typeof this.error !== "undefined") {
|
||||||
nodes.push(m("div.error", this.error.message));
|
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(
|
nodes.push(
|
||||||
m(
|
m(
|
||||||
@@ -84,7 +88,7 @@ function view(ctl, params) {
|
|||||||
disabled: !this.saveEnabled,
|
disabled: !this.saveEnabled,
|
||||||
onclick: async () => {
|
onclick: async () => {
|
||||||
try {
|
try {
|
||||||
await this.saveSettings(this.settings);
|
this.settings = await this.saveSettings(this.settings);
|
||||||
this.error = undefined;
|
this.error = undefined;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.error = e;
|
this.error = e;
|
||||||
|
@@ -33,6 +33,7 @@ function handleError(error, type = "error") {
|
|||||||
* @since 3.0.0
|
* @since 3.0.0
|
||||||
*
|
*
|
||||||
* @param object settings Settings object
|
* @param object settings Settings object
|
||||||
|
* @return object Settings object
|
||||||
*/
|
*/
|
||||||
async function saveSettings(settings) {
|
async function saveSettings(settings) {
|
||||||
var response = await chrome.runtime.sendMessage({
|
var response = await chrome.runtime.sendMessage({
|
||||||
@@ -42,6 +43,14 @@ async function saveSettings(settings) {
|
|||||||
if (response.status != "ok") {
|
if (response.status != "ok") {
|
||||||
throw new Error(response.message);
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -67,6 +67,10 @@ async function run() {
|
|||||||
}
|
}
|
||||||
var settings = response.settings;
|
var settings = response.settings;
|
||||||
|
|
||||||
|
if (settings.hasOwnProperty("hostError")) {
|
||||||
|
throw new Error(settings.hostError.params.message);
|
||||||
|
}
|
||||||
|
|
||||||
if (typeof settings.host === "undefined") {
|
if (typeof settings.host === "undefined") {
|
||||||
throw new Error("Unable to retrieve current tab information");
|
throw new Error("Unable to retrieve current tab information");
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user