Fix: Actually show errors in custom themes now

This commit is contained in:
Pieter Vander Vennet 2023-06-26 11:11:22 +02:00
parent adb5d892fb
commit 6a79dde743
2 changed files with 50 additions and 88 deletions

View file

@ -11,7 +11,6 @@ import { LocalStorageSource } from "./Web/LocalStorageSource"
import LZString from "lz-string" import LZString from "lz-string"
import { FixLegacyTheme } from "../Models/ThemeConfig/Conversion/LegacyJsonConvert" import { FixLegacyTheme } from "../Models/ThemeConfig/Conversion/LegacyJsonConvert"
import { LayerConfigJson } from "../Models/ThemeConfig/Json/LayerConfigJson" import { LayerConfigJson } from "../Models/ThemeConfig/Json/LayerConfigJson"
import SharedTagRenderings from "../Customizations/SharedTagRenderings"
import known_layers from "../assets/generated/known_layers.json" import known_layers from "../assets/generated/known_layers.json"
import { PrepareTheme } from "../Models/ThemeConfig/Conversion/PrepareTheme" import { PrepareTheme } from "../Models/ThemeConfig/Conversion/PrepareTheme"
import licenses from "../assets/generated/license_info.json" import licenses from "../assets/generated/license_info.json"
@ -24,9 +23,8 @@ import {
ValidateTagRenderings, ValidateTagRenderings,
ValidateThemeAndLayers, ValidateThemeAndLayers,
} from "../Models/ThemeConfig/Conversion/Validation" } from "../Models/ThemeConfig/Conversion/Validation"
import { DesugaringContext, Each, On } from "../Models/ThemeConfig/Conversion/Conversion" import { DesugaringContext } from "../Models/ThemeConfig/Conversion/Conversion"
import { PrepareLayer, RewriteSpecial } from "../Models/ThemeConfig/Conversion/PrepareLayer" import { RewriteSpecial } from "../Models/ThemeConfig/Conversion/PrepareLayer"
import { AllSharedLayers } from "../Customizations/AllSharedLayers"
import { TagRenderingConfigJson } from "../Models/ThemeConfig/Json/TagRenderingConfigJson" import { TagRenderingConfigJson } from "../Models/ThemeConfig/Json/TagRenderingConfigJson"
import questions from "../assets/tagRenderings/questions.json" import questions from "../assets/tagRenderings/questions.json"
@ -79,7 +77,6 @@ export default class DetermineLayout {
let hash = location.hash.substr(1) let hash = location.hash.substr(1)
let json: any let json: any
try {
// layoutFromBase64 contains the name of the theme. This is partly to do tracking with goat counter // layoutFromBase64 contains the name of the theme. This is partly to do tracking with goat counter
const dedicatedHashFromLocalStorage = LocalStorageSource.Get( const dedicatedHashFromLocalStorage = LocalStorageSource.Get(
"user-layout-" + userLayoutParam.data?.replace(" ", "_") "user-layout-" + userLayoutParam.data?.replace(" ", "_")
@ -101,33 +98,12 @@ export default class DetermineLayout {
json = JSON.parse(atob(hash)) json = JSON.parse(atob(hash))
} catch (e) { } catch (e) {
// We try to decode with lz-string // We try to decode with lz-string
try {
json = JSON.parse(Utils.UnMinify(LZString.decompressFromBase64(hash))) json = JSON.parse(Utils.UnMinify(LZString.decompressFromBase64(hash)))
} catch (e) {
console.error(e)
DetermineLayout.ShowErrorOnCustomTheme(
"Could not decode the hash",
new FixedUiElement("Not a valid (LZ-compressed) JSON")
)
return null
}
} }
const layoutToUse = DetermineLayout.prepCustomTheme(json) const layoutToUse = DetermineLayout.prepCustomTheme(json)
userLayoutParam.setData(layoutToUse.id) userLayoutParam.setData(layoutToUse.id)
return layoutToUse return layoutToUse
} catch (e) {
console.error(e)
if (hash === undefined || hash.length < 10) {
DetermineLayout.ShowErrorOnCustomTheme(
"Could not load a theme from the hash",
new FixedUiElement("Hash does not contain data"),
json
)
}
this.ShowErrorOnCustomTheme("Could not parse the hash", new FixedUiElement(e), json)
return null
}
} }
public static ShowErrorOnCustomTheme( public static ShowErrorOnCustomTheme(
@ -187,6 +163,7 @@ export default class DetermineLayout {
return dict return dict
} }
private static prepCustomTheme(json: any, sourceUrl?: string, forceId?: string): LayoutConfig { private static prepCustomTheme(json: any, sourceUrl?: string, forceId?: string): LayoutConfig {
if (json.layers === undefined && json.tagRenderings !== undefined) { if (json.layers === undefined && json.tagRenderings !== undefined) {
const iconTr = json.mapRendering.map((mr) => mr.icon).find((icon) => icon !== undefined) const iconTr = json.mapRendering.map((mr) => mr.icon).find((icon) => icon !== undefined)
@ -255,9 +232,7 @@ export default class DetermineLayout {
"maindiv" "maindiv"
) )
try {
let parsed = await Utils.downloadJson(link) let parsed = await Utils.downloadJson(link)
try {
let forcedId = parsed.id let forcedId = parsed.id
const url = new URL(link) const url = new URL(link)
if (!(url.hostname === "localhost" || url.hostname === "127.0.0.1")) { if (!(url.hostname === "localhost" || url.hostname === "127.0.0.1")) {
@ -265,22 +240,5 @@ export default class DetermineLayout {
} }
console.log("Loaded remote link:", link) console.log("Loaded remote link:", link)
return DetermineLayout.prepCustomTheme(parsed, link, forcedId) return DetermineLayout.prepCustomTheme(parsed, link, forcedId)
} catch (e) {
console.error(e)
DetermineLayout.ShowErrorOnCustomTheme(
`<a href="${link}">${link}</a> is invalid:`,
new FixedUiElement(e),
parsed
)
return null
}
} catch (e) {
console.error(e)
DetermineLayout.ShowErrorOnCustomTheme(
`<a href="${link}">${link}</a> is invalid - probably not found or invalid JSON:`,
new FixedUiElement(e)
)
return null
}
} }
} }

View file

@ -6,7 +6,8 @@ import ThemeViewGUI from "./UI/ThemeViewGUI.svelte"
import { FixedUiElement } from "./UI/Base/FixedUiElement" import { FixedUiElement } from "./UI/Base/FixedUiElement"
// @ts-ignore // @ts-ignore
DetermineLayout.GetLayout() try {
DetermineLayout.GetLayout()
.then((layout) => { .then((layout) => {
const state = new ThemeViewState(layout) const state = new ThemeViewState(layout)
const main = new SvelteUIElement(ThemeViewGUI, { state }) const main = new SvelteUIElement(ThemeViewGUI, { state })
@ -16,3 +17,6 @@ DetermineLayout.GetLayout()
console.error("Error while initializing: ", err, err.stack) console.error("Error while initializing: ", err, err.stack)
new FixedUiElement(err).SetClass("block alert").AttachTo("maindiv") new FixedUiElement(err).SetClass("block alert").AttachTo("maindiv")
}) })
} catch (err) {
new FixedUiElement(err).SetClass("block alert").AttachTo("maindiv")
}