explodorama / files / src / devlog.ts
devlog.ts
52d68b59 2026-10-24 Pavel Safronov | 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". */ | |
33a0256b 2026-10-24 Pavel Safronov | 9 | export function device(): string { |
52d68b59 2026-10-24 Pavel Safronov | 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 | ||
33a0256b 2026-10-24 Pavel Safronov | 15 | /** The last few errors caught, oldest first, for a bug report (main.ts `reportBug`). */ |
| 16 | const recent: string[] = [] | |
| 17 | const RECENT_KEPT = 5 | |
| 18 | ||
52d68b59 2026-10-24 Pavel Safronov | 19 | export 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}`) | |
33a0256b 2026-10-24 Pavel Safronov | 22 | recent.push(`${where}: ${detail.split('\n')[0]}`) |
| 23 | if (recent.length > RECENT_KEPT) recent.shift() | |
| 24 | } | |
| 25 | ||
| 26 | export function recentErrors(): readonly string[] { | |
| 27 | return recent | |
52d68b59 2026-10-24 Pavel Safronov | 28 | } |