more logs

This commit is contained in:
parent f61801e855
commit 2715c56b4f
2 changed files with 105 additions and 0 deletions

View file

@ -1,4 +1,46 @@
import ReactDOM from "react-dom/client";
import App from "./App";
const backendBase =
(import.meta.env.VITE_BACKEND_URL as string | undefined) ?? "http://localhost:8000";
const sendFrontLog = (message: string, level: "info" | "warn" | "error" = "info") => {
fetch(`${backendBase}/logs`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ message, level, source: "frontend" }),
}).catch(() => {});
};
window.addEventListener("error", (event) => {
const err = event.error instanceof Error ? event.error : null;
const details = err?.stack || err?.message || String(event.message || "unknown error");
sendFrontLog(`window.error: ${details}`, "error");
});
window.addEventListener("unhandledrejection", (event) => {
const reason = event.reason instanceof Error ? event.reason.stack || event.reason.message : String(event.reason);
sendFrontLog(`unhandledrejection: ${reason}`, "error");
});
const _consoleError = console.error.bind(console);
console.error = (...args: unknown[]) => {
try {
sendFrontLog(`console.error: ${args.map(String).join(" ")}`, "error");
} catch {
// ignore
}
_consoleError(...args);
};
const _consoleWarn = console.warn.bind(console);
console.warn = (...args: unknown[]) => {
try {
sendFrontLog(`console.warn: ${args.map(String).join(" ")}`, "warn");
} catch {
// ignore
}
_consoleWarn(...args);
};
ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(<App />);