37 lines
1.1 KiB
TypeScript
37 lines
1.1 KiB
TypeScript
import { NetworkError } from "./errors.js";
|
|
|
|
export function showErrorModal(error: unknown) {
|
|
const nodes: HTMLElement[] = [];
|
|
const title = document.createElement("h2");
|
|
title.textContent = "échec";
|
|
nodes.push(title);
|
|
if (error instanceof NetworkError) {
|
|
const content = document.createElement("p");
|
|
content.textContent = error.message;
|
|
nodes.push(content);
|
|
if (error.details.data) {
|
|
const pre = document.createElement("pre");
|
|
pre.textContent = JSON.stringify(error.details.data, undefined, 2);
|
|
nodes.push(pre);
|
|
}
|
|
} else {
|
|
const content = document.createElement("p");
|
|
content.textContent =
|
|
error instanceof Error ? error.message : String(error);
|
|
nodes.push(content);
|
|
}
|
|
showModalWith(...nodes);
|
|
}
|
|
|
|
export function showModal(text: string) {
|
|
const content = document.createElement("p");
|
|
content.textContent = text;
|
|
showModalWith(content);
|
|
}
|
|
|
|
function showModalWith(...nodes: HTMLElement[]) {
|
|
const dialog = document.getElementById("global-modal") as HTMLDialogElement;
|
|
dialog.replaceChildren(...nodes);
|
|
dialog.showModal();
|
|
}
|