fuzzyworld

explodorama / files / src / devlog.ts

devlog.ts

1.2 KB · history · blame

1/**
2 * Errors the game catches and carries on from (`guarded` frames, refused turns). They go to the
3 * console with their stack and the device, and `npm run dev` forwards the console to its terminal
4 * (`server.forwardConsole` in vite.config.ts), from a phone on the Wi-Fi too (user request,
5 * 2026-10-23: a crash on the phone showed nothing anywhere).
6 */
7
8/** Which device it came from, in a few words: "Android 390×844", "iPhone 390×844", "desktop 1280×720". */
9export function device(): string {
10 const ua = navigator.userAgent
11 const kind = /iPad/.test(ua) ? 'iPad' : /iPhone/.test(ua) ? 'iPhone' : /Android/.test(ua) ? 'Android' : 'desktop'
12 return `${kind} ${innerWidth}×${innerHeight}`
13}
14
15/** The last few errors caught, oldest first, for a bug report (main.ts `reportBug`). */
16const recent: string[] = []
17const RECENT_KEPT = 5
18
19export function reportError(where: string, error: unknown): void {
20 const detail = error instanceof Error ? (error.stack ?? `${error.name}: ${error.message}`) : String(error)
21 console.error(`[${device()}] ${where}: ${detail}`)
22 recent.push(`${where}: ${detail.split('\n')[0]}`)
23 if (recent.length > RECENT_KEPT) recent.shift()
24}
25
26export function recentErrors(): readonly string[] {
27 return recent
28}