From 529386a9b6506529c87d7179350af354cc43a73c Mon Sep 17 00:00:00 2001 From: Zaid Abdulla Date: Thu, 16 Sep 2021 12:16:01 -0700 Subject: [PATCH] Prevent invalid YAML config from bricking the UI --- lib/local-cache.ts | 2 +- ui/components.ts | 9 ++++++++- ui/config-functions.ts | 6 +++--- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/local-cache.ts b/lib/local-cache.ts index 5994d57..4eaab88 100644 --- a/lib/local-cache.ts +++ b/lib/local-cache.ts @@ -365,7 +365,7 @@ async function fetchConfig(): Promise<[Config, UiConfig]> { let ref = ui; while (keys.length > 1) { const k = keys.shift(); - if (typeof ref[k] !== "object") ref[k] = {}; + if (ref[k] == null || typeof ref[k] !== "object") ref[k] = {}; ref = ref[k]; } ref[keys[0]] = val; diff --git a/ui/components.ts b/ui/components.ts index aca53ee..5c41e77 100644 --- a/ui/components.ts +++ b/ui/components.ts @@ -144,7 +144,14 @@ export function contextifyComponent(component: ComponentTypes): ComponentTypes { applyContext(res, context); return res; } catch (err) { - return m("p.error", { title: err.message }, "Error!"); + return m( + "p.error", + { + title: "Click to print stack trace to console", + onclick: () => console.error(err), + }, + "Error!" + ); } }; return state; diff --git a/ui/config-functions.ts b/ui/config-functions.ts index cf43ac7..84bb0de 100644 --- a/ui/config-functions.ts +++ b/ui/config-functions.ts @@ -45,7 +45,7 @@ export function flattenConfig(config: Record): any { // Order keys such that nested objects come last function orderKeys(config: any): number { let res = 1; - if (typeof config !== "object") return res; + if (config == null || typeof config !== "object") return res; if (Array.isArray(config)) { for (const c of config) res += orderKeys(c); return res; @@ -79,7 +79,7 @@ export function structureConfig(config: Config[]): any { let ref = _config; while (keys.length > 1) { const k = keys.shift(); - if (typeof ref[k] !== "object") ref[k] = {}; + if (ref[k] == null || typeof ref[k] !== "object") ref[k] = {}; ref = ref[k]; } ref[keys[0]] = c.value; @@ -137,7 +137,7 @@ export function diffConfig( }; for (const [k, v] of Object.entries(target)) - if (!current[k] || current[k] !== v) diff.add.push({ _id: k, value: v }); + if (v && current[k] !== v) diff.add.push({ _id: k, value: v }); for (const k of Object.keys(current)) if (!target[k]) diff.remove.push(k);