|
| 1 | +/* One app-wide account of background reads that are failing. Pages keep their |
| 2 | + * last-known content and report here instead of rendering their own warning; |
| 3 | + * the header shows a single compact indicator once trouble persists, with the |
| 4 | + * sanitized detail behind a disclosure. Human-actionable results of a user's |
| 5 | + * own action (an unconfirmed send or ignore) stay where the action happened. */ |
| 6 | +import { createRoot, createSignal } from "solid-js" |
| 7 | + |
| 8 | +export type BackendIssue = { |
| 9 | + source: string |
| 10 | + detail: string |
| 11 | + /** Transient: load or availability (5xx, timeout, offline). Otherwise a real fault worth fixing. */ |
| 12 | + transient: boolean |
| 13 | + since: number |
| 14 | +} |
| 15 | + |
| 16 | +/** How long a background failure must persist before the indicator reports it. */ |
| 17 | +export const BACKEND_STATUS_DELAY_MS = 2000 |
| 18 | + |
| 19 | +const store = createRoot(() => { |
| 20 | + const [issues, setIssues] = createSignal<Record<string, BackendIssue>>({}) |
| 21 | + const [shown, setShown] = createSignal<"" | "updating" | "problem">("") |
| 22 | + let timer: ReturnType<typeof setTimeout> | undefined |
| 23 | + const evaluate = () => { |
| 24 | + const current = Object.values(issues()) |
| 25 | + if (!current.length) { |
| 26 | + clearTimeout(timer) |
| 27 | + timer = undefined |
| 28 | + return setShown("") |
| 29 | + } |
| 30 | + const kind = current.some((issue) => !issue.transient) ? "problem" : "updating" |
| 31 | + const due = Math.min(...current.map((issue) => issue.since)) + BACKEND_STATUS_DELAY_MS - Date.now() |
| 32 | + clearTimeout(timer) |
| 33 | + if (due <= 0) return setShown(kind) |
| 34 | + timer = setTimeout(evaluate, due) |
| 35 | + } |
| 36 | + return { issues, setIssues, shown, evaluate } |
| 37 | +}) |
| 38 | + |
| 39 | +export const backendIssues = () => Object.values(store.issues()).sort((a, b) => a.since - b.since) |
| 40 | +export const backendStatus = store.shown |
| 41 | + |
| 42 | +export const reportBackend = (source: string, cause: unknown) => { |
| 43 | + const detail = describeFailure(cause) |
| 44 | + const transient = isTransient(cause) |
| 45 | + const previous = store.issues()[source] |
| 46 | + if (previous && previous.detail === detail && previous.transient === transient) return |
| 47 | + // Agents read the console; keep one sanitized line per change, never raw bodies or URLs. |
| 48 | + console.info(`[backend] ${source}: ${detail}`) |
| 49 | + store.setIssues((current) => ({ ...current, [source]: { source, detail, transient, since: previous?.since ?? Date.now() } })) |
| 50 | + store.evaluate() |
| 51 | +} |
| 52 | + |
| 53 | +export const clearBackend = (source: string) => { |
| 54 | + if (!store.issues()[source]) return |
| 55 | + console.info(`[backend] ${source}: recovered`) |
| 56 | + store.setIssues((current) => { |
| 57 | + const next = { ...current } |
| 58 | + delete next[source] |
| 59 | + return next |
| 60 | + }) |
| 61 | + store.evaluate() |
| 62 | +} |
| 63 | + |
| 64 | +/** Test seam. */ |
| 65 | +export const resetBackend = () => { |
| 66 | + store.setIssues({}) |
| 67 | + store.evaluate() |
| 68 | +} |
| 69 | + |
| 70 | +const statusOf = (cause: unknown): number | undefined => { |
| 71 | + if (cause && typeof cause === "object" && "status" in cause && typeof cause.status === "number") return cause.status |
| 72 | + const match = /\bHTTP (\d{3})\b/.exec(String(cause instanceof Error ? cause.message : cause)) |
| 73 | + return match ? Number(match[1]) : undefined |
| 74 | +} |
| 75 | + |
| 76 | +export const isTransient = (cause: unknown) => { |
| 77 | + const status = statusOf(cause) |
| 78 | + // 501: the server has no such route; that is a missing feature, not load. |
| 79 | + if (status !== undefined) return (status >= 500 && status !== 501) || status === 408 || status === 429 |
| 80 | + // No status: the request never got an answer (offline, refused, timed out, aborted). |
| 81 | + return true |
| 82 | +} |
| 83 | + |
| 84 | +/** A short human phrase: no URLs, paths, JSON or stack text. */ |
| 85 | +export const describeFailure = (cause: unknown) => { |
| 86 | + const status = statusOf(cause) |
| 87 | + if (status === 401 || status === 403) return `not authorized (HTTP ${status})` |
| 88 | + if (status === 404 || status === 501) return `not available here (HTTP ${status})` |
| 89 | + if (status === 502 || status === 503 || status === 504) return `service unavailable (HTTP ${status})` |
| 90 | + if (status !== undefined) return `HTTP ${status}` |
| 91 | + const text = String(cause instanceof Error ? `${cause.name} ${cause.message}` : cause) |
| 92 | + if (/timeout|timed out/i.test(text)) return "timed out" |
| 93 | + if (/abort/i.test(text)) return "cancelled" |
| 94 | + return "no connection" |
| 95 | +} |
0 commit comments