Repository navigation
Expand file tree
/
Copy pathcapture-errors.js
More file actions
47 lines (44 loc) · 2.16 KB
/
Copy pathcapture-errors.js
File metadata and controls
47 lines (44 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Runs before the app's own scripts in every document (web({ initScripts })).
// It records what the page hides from the screen: uncaught errors, rejected
// promises, console.error, and 5xx answers from Supabase. The student fixture
// reads the list at teardown and fails the test on anything it does not expect.
// sessionStorage keeps the list across reloads and navigations in the tab.
(() => {
const KEY = "__agentic_errors";
const record = (kind, text) => {
try {
const list = JSON.parse(sessionStorage.getItem(KEY) || "[]");
if (list.length < 50) list.push(`${kind}: ${String(text).slice(0, 500)} [${location.pathname}]`);
sessionStorage.setItem(KEY, JSON.stringify(list));
} catch {
// Storage can be unavailable in an opaque-origin frame; nothing to record there.
}
};
const describe = (value) => (value instanceof Error ? `${value.name}: ${value.message}` : typeof value === "string" ? value : (() => {
try { return JSON.stringify(value); } catch { return String(value); }
})());
window.addEventListener("error", (event) => record("pageerror", event.error ? describe(event.error) : event.message));
window.addEventListener("unhandledrejection", (event) => record("unhandledrejection", describe(event.reason)));
const consoleError = console.error.bind(console);
console.error = (...args) => {
record("console.error", args.map(describe).join(" "));
consoleError(...args);
};
const nativeFetch = window.fetch.bind(window);
window.fetch = async (input, init) => {
const response = await nativeFetch(input, init);
// Recording must never change what the app's request returns.
try {
if (response.status >= 500) {
const url = new URL(input instanceof Request ? input.url : String(input), location.href);
if (/^\/(functions|rest|auth)\//.test(url.pathname)) {
const method = (init && init.method) || (input instanceof Request ? input.method : "GET");
record("http", `${response.status} ${method.toUpperCase()} ${url.pathname}`);
}
}
} catch {
// An input this script cannot parse is the app's business, not a finding.
}
return response;
};
})();