feat(app): error boundary so a crashing screen shows the error, not a blank

A render error in any section previously white-screened the whole app
(no boundary). Wrap the section content in an ErrorBoundary keyed by
section: the error message + component stack render in a card, the
sidebar stays usable, and navigating away recovers. Logs to the console
for diagnosis.
This commit is contained in:
iamtoruk 2026-07-11 14:33:16 -07:00
parent d9c56e3c6b
commit 781d37674b
4 changed files with 79 additions and 0 deletions

View file

@ -1,5 +1,6 @@
import { useCallback, useEffect, useState } from 'react'
import { ErrorBoundary } from './components/ErrorBoundary'
import { Hint } from './components/Hint'
import { Panel } from './components/Panel'
import { Sidebar, type Section } from './components/Sidebar'
@ -138,6 +139,7 @@ export function App() {
<Window>
<Sidebar active={section} onNavigate={setSection} status={<StatusLine polled={overview} />} />
<div className="ct">
<ErrorBoundary key={section}>
{section === 'plans' ? (
<Plans period={period} refreshToken={refreshToken} />
) : section === 'settings' ? (
@ -175,6 +177,7 @@ export function App() {
</div>
</>
)}
</ErrorBoundary>
{section !== 'settings' && (
<Hint
items={[

View file

@ -0,0 +1,31 @@
// @vitest-environment jsdom
import { render, screen } from '@testing-library/react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { ErrorBoundary } from './ErrorBoundary'
function Boom(): never {
throw new Error('kaboom detail')
}
describe('ErrorBoundary', () => {
beforeEach(() => {
// React logs the caught error; silence it so the run stays clean.
vi.spyOn(console, 'error').mockImplementation(() => {})
})
afterEach(() => {
vi.restoreAllMocks()
})
it('renders children when nothing throws', () => {
render(<ErrorBoundary><p>all good</p></ErrorBoundary>)
expect(screen.getByText('all good')).toBeTruthy()
})
it('shows the error message instead of white-screening when a child throws', () => {
render(<ErrorBoundary><Boom /></ErrorBoundary>)
expect(screen.getByText('This screen hit an error')).toBeTruthy()
expect(screen.getByText('kaboom detail')).toBeTruthy()
expect(screen.getByRole('button', { name: 'Reload' })).toBeTruthy()
})
})

View file

@ -0,0 +1,40 @@
import { Component, type ErrorInfo, type ReactNode } from 'react'
type Props = { children: ReactNode }
type State = { error: Error | null; stack: string | null }
/**
* Catches render/lifecycle errors in a screen so a single crashing section
* shows a readable error instead of white-screening the whole app. Keyed by
* section in App, so navigating to another screen remounts and recovers.
*/
export class ErrorBoundary extends Component<Props, State> {
state: State = { error: null, stack: null }
static getDerivedStateFromError(error: Error): Partial<State> {
return { error }
}
componentDidCatch(error: Error, info: ErrorInfo): void {
// Surface to the renderer console for diagnosis in dev.
console.error('Screen crashed:', error, info.componentStack)
this.setState({ stack: info.componentStack ?? null })
}
render(): ReactNode {
const { error, stack } = this.state
if (!error) return this.props.children
return (
<div className="error-boundary">
<div className="panel error-card">
<h3 className="error-title">This screen hit an error</h3>
<p className="error-msg">{error.message || String(error)}</p>
{stack && <pre className="error-stack">{stack.trim()}</pre>}
<button className="btn" type="button" onClick={() => window.location.reload()}>
Reload
</button>
</div>
</div>
)
}
}

View file

@ -187,6 +187,11 @@ body { overflow: hidden; background: var(--bg); color: var(--ink); }
.btn-p { background: var(--accent); box-shadow: none; }
.btn-s { background: var(--panel); }
.panel { border-color: var(--line); box-shadow: none; }
.error-boundary { display: flex; padding: 20px; }
.error-card { max-width: 760px; padding: 16px 18px; }
.error-title { margin: 0 0 8px; font-size: 14px; font-weight: 620; color: var(--ink); }
.error-msg { margin: 0 0 12px; font-size: 12.5px; color: var(--bad); font-variant-numeric: tabular-nums; }
.error-stack { max-height: 320px; margin: 0 0 12px; padding: 10px 12px; overflow: auto; border: 1px solid var(--line2); border-radius: 8px; background: var(--phead); color: var(--mut); font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 11px; line-height: 1.5; white-space: pre; }
.phead { border-color: var(--line2); }
.phead .r.link, a { color: var(--accent); }
.bars .b, .bars .c.hi .b, .bars .c.hi2 .b { background: var(--bar); box-shadow: none; }