-
Notifications
You must be signed in to change notification settings - Fork 1
ui: add root and tab error boundaries with retry fallback #173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ import '../i18n'; | |
|
|
||
| import ConfigTab from './ConfigTab'; | ||
| import DarkModeToggle from './DarkModeToggle'; | ||
| import ErrorBoundary from './ErrorBoundary'; | ||
| import LanguageSelector from './LanguageSelector'; | ||
| import ProcessedTab from './ProcessedTab'; | ||
| import SourceTab from './SourceTab'; | ||
|
|
@@ -60,6 +61,9 @@ const AppContent = () => { | |
| } = useApp(); | ||
|
|
||
| const appWindow = globalThis as Window & typeof globalThis; | ||
| const tabFallbackTitle = t('errors.tabCrashedTitle'); | ||
| const tabFallbackMessage = t('errors.tabCrashedDescription'); | ||
| const retryLabel = t('errors.retryRender'); | ||
|
|
||
| return ( | ||
| <div className='mx-auto flex h-screen w-full max-w-screen-2xl flex-col p-4'> | ||
|
|
@@ -134,7 +138,14 @@ const AppContent = () => { | |
| aria-labelledby='tab-config' | ||
| className={`absolute inset-0 overflow-y-auto bg-white dark:bg-gray-800 p-4 text-gray-900 dark:text-gray-100 transition-colors duration-200 ${activeTab === 'config' ? '' : 'hidden'}`} | ||
| > | ||
| <ConfigTab configContent={configContent} onConfigChange={updateConfig} /> | ||
| <ErrorBoundary | ||
| fallbackTitle={tabFallbackTitle} | ||
| fallbackMessage={tabFallbackMessage} | ||
| resetLabel={retryLabel} | ||
| resetKeys={[activeTab, configContent]} | ||
| > | ||
| <ConfigTab configContent={configContent} onConfigChange={updateConfig} /> | ||
| </ErrorBoundary> | ||
| </div> | ||
|
|
||
| <div | ||
|
|
@@ -143,20 +154,27 @@ const AppContent = () => { | |
| aria-labelledby='tab-source' | ||
| className={`absolute inset-0 overflow-y-auto bg-white dark:bg-gray-800 p-4 text-gray-900 dark:text-gray-100 transition-colors duration-200 ${activeTab === 'source' ? '' : 'hidden'}`} | ||
| > | ||
| <SourceTab | ||
| isActive={activeTab === 'source'} | ||
| rootPath={rootPath} | ||
| directoryTree={directoryTree} | ||
| selectedFiles={selectedFiles} | ||
| selectedFolders={selectedFolders} | ||
| configContent={configContent} | ||
| onDirectorySelect={selectDirectory} | ||
| onFileSelect={handleFileSelect} | ||
| onFolderSelect={handleFolderSelect} | ||
| onBatchSelect={handleBatchSelect} | ||
| onAnalyze={handleAnalyze} | ||
| onRefreshTree={refreshDirectoryTree} | ||
| /> | ||
| <ErrorBoundary | ||
| fallbackTitle={tabFallbackTitle} | ||
| fallbackMessage={tabFallbackMessage} | ||
| resetLabel={retryLabel} | ||
| resetKeys={[activeTab, rootPath, selectedFiles.size, selectedFolders.size]} | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using To fix this, the reset key should depend on the content of the selection sets, not just their size. A simple way to create a stable key from the sets is to convert them to a sorted string. |
||
| > | ||
| <SourceTab | ||
| isActive={activeTab === 'source'} | ||
| rootPath={rootPath} | ||
| directoryTree={directoryTree} | ||
| selectedFiles={selectedFiles} | ||
| selectedFolders={selectedFolders} | ||
| configContent={configContent} | ||
| onDirectorySelect={selectDirectory} | ||
| onFileSelect={handleFileSelect} | ||
| onFolderSelect={handleFolderSelect} | ||
| onBatchSelect={handleBatchSelect} | ||
| onAnalyze={handleAnalyze} | ||
| onRefreshTree={refreshDirectoryTree} | ||
| /> | ||
| </ErrorBoundary> | ||
| </div> | ||
|
|
||
| <div | ||
|
|
@@ -165,11 +183,18 @@ const AppContent = () => { | |
| aria-labelledby='tab-processed' | ||
| className={`absolute inset-0 overflow-y-auto bg-white dark:bg-gray-800 p-4 text-gray-900 dark:text-gray-100 transition-colors duration-200 ${activeTab === 'processed' ? '' : 'hidden'}`} | ||
| > | ||
| <ProcessedTab | ||
| processedResult={processedResult} | ||
| onSave={handleSaveOutput} | ||
| onRefresh={handleRefreshProcessed} | ||
| /> | ||
| <ErrorBoundary | ||
| fallbackTitle={tabFallbackTitle} | ||
| fallbackMessage={tabFallbackMessage} | ||
| resetLabel={retryLabel} | ||
| resetKeys={[activeTab, processedResult?.content]} | ||
| > | ||
| <ProcessedTab | ||
| processedResult={processedResult} | ||
| onSave={handleSaveOutput} | ||
| onRefresh={handleRefreshProcessed} | ||
| /> | ||
| </ErrorBoundary> | ||
| </div> | ||
| </div> | ||
| </div> | ||
|
|
@@ -178,12 +203,20 @@ const AppContent = () => { | |
| }; | ||
|
|
||
| const App = () => { | ||
| const { t } = useTranslation(); | ||
|
|
||
| return ( | ||
| <DarkModeProvider> | ||
| <AppProvider> | ||
| <AppContent /> | ||
| </AppProvider> | ||
| </DarkModeProvider> | ||
| <ErrorBoundary | ||
| fallbackTitle={t('errors.rendererRootCrashedTitle')} | ||
| fallbackMessage={t('errors.rendererRootCrashedDescription')} | ||
| resetLabel={t('errors.retryRender')} | ||
| > | ||
| <DarkModeProvider> | ||
| <AppProvider> | ||
| <AppContent /> | ||
| </AppProvider> | ||
| </DarkModeProvider> | ||
| </ErrorBoundary> | ||
| ); | ||
| }; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import React, { Component, ErrorInfo, ReactNode } from 'react'; | ||
|
|
||
| type ErrorBoundaryProps = { | ||
| children: ReactNode; | ||
| fallbackTitle: string; | ||
| fallbackMessage: string; | ||
| resetLabel: string; | ||
| onReset?: () => void; | ||
| resetKeys?: ReadonlyArray<unknown>; | ||
| }; | ||
|
|
||
| type ErrorBoundaryState = { | ||
| hasError: boolean; | ||
| }; | ||
|
|
||
| const haveResetKeysChanged = ( | ||
| previousKeys: ReadonlyArray<unknown> = [], | ||
| nextKeys: ReadonlyArray<unknown> = [] | ||
| ) => { | ||
| if (previousKeys.length !== nextKeys.length) { | ||
| return true; | ||
| } | ||
|
|
||
| for (let index = 0; index < previousKeys.length; index += 1) { | ||
| if (!Object.is(previousKeys[index], nextKeys[index])) { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| }; | ||
|
|
||
| class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> { | ||
| state: ErrorBoundaryState = { | ||
| hasError: false, | ||
| }; | ||
|
|
||
| static getDerivedStateFromError() { | ||
| return { hasError: true }; | ||
| } | ||
|
|
||
| componentDidCatch(error: Error, errorInfo: ErrorInfo) { | ||
| console.error('Renderer ErrorBoundary captured an error:', error, errorInfo); | ||
| } | ||
|
|
||
| componentDidUpdate(previousProps: ErrorBoundaryProps) { | ||
| if (!this.state.hasError) { | ||
| return; | ||
| } | ||
|
|
||
| if (haveResetKeysChanged(previousProps.resetKeys, this.props.resetKeys)) { | ||
| this.setState({ hasError: false }); | ||
| } | ||
| } | ||
|
Comment on lines
+46
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2. Componentdidupdate missing previousstate guard The componentDidUpdate override only accepts previousProps, omitting previousState. After getDerivedStateFromError transitions hasError to true, React commits the fallback UI and then calls componentDidUpdate with previousState.hasError === false. Without checking previousState.hasError, if any resetKey also changed in the same render cycle, the just-caught error is immediately cleared, the broken child re-renders, throws again, and the cycle repeats. Agent Prompt
|
||
|
|
||
| private readonly handleReset = () => { | ||
| this.props.onReset?.(); | ||
| this.setState({ hasError: false }); | ||
| }; | ||
|
|
||
| render() { | ||
| if (!this.state.hasError) { | ||
| return this.props.children; | ||
| } | ||
|
|
||
| return ( | ||
| <div | ||
| role='alert' | ||
| className='m-4 rounded-md border border-red-200 bg-red-50 p-4 text-sm text-red-900 dark:border-red-800 dark:bg-red-900/30 dark:text-red-100' | ||
| > | ||
| <h2 className='text-base font-semibold'>{this.props.fallbackTitle}</h2> | ||
| <p className='mt-2'>{this.props.fallbackMessage}</p> | ||
| <button | ||
| type='button' | ||
| onClick={this.handleReset} | ||
| className='mt-3 rounded border border-red-300 bg-white px-3 py-1 text-xs font-medium text-red-700 hover:bg-red-100 dark:border-red-700 dark:bg-red-900/40 dark:text-red-100 dark:hover:bg-red-900/60' | ||
| > | ||
| {this.props.resetLabel} | ||
| </button> | ||
| </div> | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| export default ErrorBoundary; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import React, { useState } from 'react'; | ||
| import { fireEvent, render, screen } from '@testing-library/react'; | ||
|
|
||
| import ErrorBoundary from '../../../src/renderer/components/ErrorBoundary'; | ||
|
|
||
| const ProblemChild = ({ shouldThrow }: { shouldThrow: boolean }) => { | ||
| if (shouldThrow) { | ||
| throw new Error('render boom'); | ||
| } | ||
| return <div>child rendered</div>; | ||
| }; | ||
|
|
||
| describe('ErrorBoundary', () => { | ||
| let consoleErrorSpy: jest.SpyInstance; | ||
|
|
||
| beforeEach(() => { | ||
| consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| consoleErrorSpy.mockRestore(); | ||
| }); | ||
|
|
||
| it('renders children when no error is thrown', () => { | ||
| render( | ||
| <ErrorBoundary | ||
| fallbackTitle='Fallback title' | ||
| fallbackMessage='Fallback message' | ||
| resetLabel='Retry' | ||
| > | ||
| <ProblemChild shouldThrow={false} /> | ||
| </ErrorBoundary> | ||
| ); | ||
|
|
||
| expect(screen.getByText('child rendered')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('shows fallback UI when a child throws', () => { | ||
| render( | ||
| <ErrorBoundary | ||
| fallbackTitle='Fallback title' | ||
| fallbackMessage='Fallback message' | ||
| resetLabel='Retry' | ||
| > | ||
| <ProblemChild shouldThrow /> | ||
| </ErrorBoundary> | ||
| ); | ||
|
|
||
| expect(screen.getByRole('alert')).toHaveTextContent('Fallback title'); | ||
| expect(screen.getByRole('alert')).toHaveTextContent('Fallback message'); | ||
| }); | ||
|
|
||
| it('resets and renders children again when reset keys change', () => { | ||
| const Harness = () => { | ||
| const [shouldThrow, setShouldThrow] = useState(true); | ||
| return ( | ||
| <> | ||
| <button type='button' onClick={() => setShouldThrow(false)}> | ||
| recover | ||
| </button> | ||
| <ErrorBoundary | ||
| fallbackTitle='Fallback title' | ||
| fallbackMessage='Fallback message' | ||
| resetLabel='Retry' | ||
| resetKeys={[shouldThrow]} | ||
| > | ||
| <ProblemChild shouldThrow={shouldThrow} /> | ||
| </ErrorBoundary> | ||
| </> | ||
| ); | ||
| }; | ||
|
|
||
| render(<Harness />); | ||
| expect(screen.getByRole('alert')).toBeInTheDocument(); | ||
|
|
||
| fireEvent.click(screen.getByText('recover')); | ||
| expect(screen.getByText('child rendered')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('calls onReset when retry button is clicked', () => { | ||
| const onReset = jest.fn(); | ||
|
|
||
| render( | ||
| <ErrorBoundary | ||
| fallbackTitle='Fallback title' | ||
| fallbackMessage='Fallback message' | ||
| resetLabel='Retry' | ||
| onReset={onReset} | ||
| > | ||
| <ProblemChild shouldThrow /> | ||
| </ErrorBoundary> | ||
| ); | ||
|
|
||
| fireEvent.click(screen.getByRole('button', { name: 'Retry' })); | ||
| expect(onReset).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1. Activetab in resetkeys auto-retries crashed tabs
🐞 Bug✓ CorrectnessAgent Prompt
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools