From e7587a2ec62ccd96e841d716c784b34e0d1130d1 Mon Sep 17 00:00:00 2001 From: wukko Date: Wed, 31 Jul 2024 14:57:34 +0600 Subject: [PATCH] web/TransferSettings: friendlier error messages --- web/i18n/en/error.json | 5 +++ web/i18n/en/settings.json | 2 -- .../settings/TransferSettings.svelte | 34 +++++++++---------- 3 files changed, 22 insertions(+), 19 deletions(-) create mode 100644 web/i18n/en/error.json diff --git a/web/i18n/en/error.json b/web/i18n/en/error.json new file mode 100644 index 00000000..55ec9c73 --- /dev/null +++ b/web/i18n/en/error.json @@ -0,0 +1,5 @@ +{ + "import.no_data": "there's nothing to load from the file. are you sure it's the right one?", + "import.invalid": "your file doesn't have valid cobalt settings to import. are you sure it's the right one?", + "import.unknown": "couldn't load data from the file. it may be corrupted or of wrong format. here's the error i got:\n{{ value }}" +} diff --git a/web/i18n/en/settings.json b/web/i18n/en/settings.json index 34116b90..4fc304af 100644 --- a/web/i18n/en/settings.json +++ b/web/i18n/en/settings.json @@ -103,7 +103,5 @@ "advanced.data": "settings data", "advanced.reset": "reset all settings", "advanced.import": "import", - "advanced.import.no_data": "failed loading setting data from file", - "advanced.import.invalid": "file does not contain valid cobalt settings", "advanced.export": "export" } diff --git a/web/src/components/settings/TransferSettings.svelte b/web/src/components/settings/TransferSettings.svelte index f14c8d65..4ec0cbed 100644 --- a/web/src/components/settings/TransferSettings.svelte +++ b/web/src/components/settings/TransferSettings.svelte @@ -4,7 +4,7 @@ import { storedSettings, updateSetting, - loadFromString + loadFromString, } from "$lib/state/settings"; import { validateSettings } from "$lib/settings/validate"; @@ -16,12 +16,11 @@ const updateSettings = (reader: FileReader) => { try { const data = reader.result?.toString(); - if (!data) - throw $t('settings.advanced.import.no_data'); + if (!data) throw $t("error.import.no_data"); const loadedSettings = loadFromString(data); if (!validateSettings(loadedSettings)) - throw $t('settings.advanced.import.invalid'); + throw $t("error.import.invalid"); createDialog({ id: "import-confirm", @@ -40,19 +39,19 @@ color: "red", main: true, timeout: 5000, - action: () => updateSetting(loadFromString(data)) + action: () => updateSetting(loadFromString(data)), }, ], }); } catch (e) { - let message; + let message = $t("error.import.no_data"); - if (e instanceof Error) - message = e.message; - else if (typeof e === 'string') + if (e instanceof Error) { + console.error("settings import error:", e); + message = $t("error.import.unknown", { value: e.message }); + } else if (typeof e === "string") { message = e; - else - message = $t('settings.advanced.import.no_data'); + } createDialog({ id: "settings-import-error", @@ -88,10 +87,9 @@ }; const exportSettings = () => { - const blob = new Blob( - [ JSON.stringify($storedSettings, null, 2) ], - { type: "application/json" } - ); + const blob = new Blob([JSON.stringify($storedSettings, null, 2)], { + type: "application/json", + }); const pseudolink = document.createElement("a"); pseudolink.href = URL.createObjectURL(blob); @@ -102,11 +100,13 @@
- {$t("settings.advanced.import")} + + {$t("settings.advanced.import")} {#if $storedSettings.schemaVersion} - {$t("settings.advanced.export")} + + {$t("settings.advanced.export")} {/if}