Webview: improve error debug abilities

This commit is contained in:
Johannes Kirschbauer
2024-07-10 16:57:13 +02:00
parent 3c86f0a327
commit f1dece0c04
2 changed files with 51 additions and 0 deletions

View File

@@ -94,12 +94,30 @@ type PyApi = {
};
};
function download(filename: string, text: string) {
const element = document.createElement("a");
element.setAttribute(
"href",
"data:text/plain;charset=utf-8," + encodeURIComponent(text)
);
element.setAttribute("download", filename);
element.style.display = "none";
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
const deserialize =
<T>(fn: (response: T) => void) =>
(str: string) => {
try {
fn(JSON.parse(str) as T);
} catch (e) {
console.log("Error parsing JSON: ", e);
console.log({ download: () => download("error.json", str) });
console.error(str);
alert(`Error parsing JSON: ${e}`);
}