Webview: improve error debug abilities
This commit is contained in:
@@ -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 =
|
const deserialize =
|
||||||
<T>(fn: (response: T) => void) =>
|
<T>(fn: (response: T) => void) =>
|
||||||
(str: string) => {
|
(str: string) => {
|
||||||
try {
|
try {
|
||||||
fn(JSON.parse(str) as T);
|
fn(JSON.parse(str) as T);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
console.log("Error parsing JSON: ", e);
|
||||||
|
console.log({ download: () => download("error.json", str) });
|
||||||
console.error(str);
|
console.error(str);
|
||||||
alert(`Error parsing JSON: ${e}`);
|
alert(`Error parsing JSON: ${e}`);
|
||||||
}
|
}
|
||||||
|
|||||||
33
pkgs/webview-ui/app/util.ts
Normal file
33
pkgs/webview-ui/app/util.ts
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
export function isValidHostname(value: string | null | undefined) {
|
||||||
|
if (typeof value !== "string") return false;
|
||||||
|
|
||||||
|
const validHostnameChars = /^[a-zA-Z0-9-.]{1,253}\.?$/g;
|
||||||
|
|
||||||
|
if (!validHostnameChars.test(value)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (value.endsWith(".")) {
|
||||||
|
value = value.slice(0, value.length - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (value.length > 253) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const labels = value.split(".");
|
||||||
|
|
||||||
|
const isValid = labels.every(function (label) {
|
||||||
|
const validLabelChars = /^([a-zA-Z0-9-]+)$/g;
|
||||||
|
|
||||||
|
const validLabel =
|
||||||
|
validLabelChars.test(label) &&
|
||||||
|
label.length < 64 &&
|
||||||
|
!label.startsWith("-") &&
|
||||||
|
!label.endsWith("-");
|
||||||
|
|
||||||
|
return validLabel;
|
||||||
|
});
|
||||||
|
|
||||||
|
return isValid;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user