diff --git a/app/renderer/App.tsx b/app/renderer/App.tsx index 93a49d66..60ec7071 100644 --- a/app/renderer/App.tsx +++ b/app/renderer/App.tsx @@ -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() { } />
+ {section === 'plans' ? ( ) : section === 'settings' ? ( @@ -175,6 +177,7 @@ export function App() {
)} + {section !== 'settings' && ( { + 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(

all good

) + expect(screen.getByText('all good')).toBeTruthy() + }) + + it('shows the error message instead of white-screening when a child throws', () => { + render() + expect(screen.getByText('This screen hit an error')).toBeTruthy() + expect(screen.getByText('kaboom detail')).toBeTruthy() + expect(screen.getByRole('button', { name: 'Reload' })).toBeTruthy() + }) +}) diff --git a/app/renderer/components/ErrorBoundary.tsx b/app/renderer/components/ErrorBoundary.tsx new file mode 100644 index 00000000..7531b050 --- /dev/null +++ b/app/renderer/components/ErrorBoundary.tsx @@ -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 { + state: State = { error: null, stack: null } + + static getDerivedStateFromError(error: Error): Partial { + 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 ( +
+
+

This screen hit an error

+

{error.message || String(error)}

+ {stack &&
{stack.trim()}
} + +
+
+ ) + } +} diff --git a/app/renderer/styles/plain.css b/app/renderer/styles/plain.css index 4ce381d7..eb7dc93b 100644 --- a/app/renderer/styles/plain.css +++ b/app/renderer/styles/plain.css @@ -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; }