Skip to content

Commit 5560890

Browse files
add error boundary
1 parent 8df0abe commit 5560890

2 files changed

Lines changed: 25 additions & 7 deletions

File tree

packages/react-ui/src/app/app-bootstrap.tsx

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@ type AppBootstrapProps = {
99
children: React.ReactNode;
1010
};
1111

12+
type BootstrapState =
13+
| { status: 'loading' }
14+
| { status: 'ready' }
15+
| { status: 'error'; error: Error };
16+
1217
export function AppBootstrap({ children }: Readonly<AppBootstrapProps>) {
13-
const [isReady, setIsReady] = useState(false);
18+
const [state, setState] = useState<BootstrapState>({ status: 'loading' });
1419

1520
useEffect(() => {
1621
let mounted = true;
@@ -22,12 +27,18 @@ export function AppBootstrap({ children }: Readonly<AppBootstrapProps>) {
2227
await initializeInternal();
2328

2429
if (mounted) {
25-
setIsReady(true);
30+
setState({ status: 'ready' });
2631
}
2732
} catch (error) {
2833
console.error('Bootstrap failed:', error);
2934
if (mounted) {
30-
setIsReady(true);
35+
setState({
36+
status: 'error',
37+
error:
38+
error instanceof Error
39+
? error
40+
: new Error('Failed to initialize application'),
41+
});
3142
}
3243
}
3344
}
@@ -39,13 +50,17 @@ export function AppBootstrap({ children }: Readonly<AppBootstrapProps>) {
3950
};
4051
}, []);
4152

42-
if (!isReady) {
53+
if (state.status === 'loading') {
4354
return (
4455
<div className="flex h-screen w-screen items-center justify-center">
4556
<LoadingSpinner size={50} />
4657
</div>
4758
);
4859
}
4960

61+
if (state.status === 'error') {
62+
throw state.error;
63+
}
64+
5065
return children;
5166
}

packages/react-ui/src/main.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ import './i18n';
66
/* Make sure i18n module is imported before App component which uses translations*/
77
import App from './app/app';
88
import { AppBootstrap } from './app/app-bootstrap';
9+
import { OpsErrorBoundary } from './app/common/error-boundaries/ops-error-boundary';
910

1011
const root = ReactDOM.createRoot(
1112
document.getElementById('root') as HTMLElement,
1213
);
1314
root.render(
1415
<StrictMode>
15-
<AppBootstrap>
16-
<App />
17-
</AppBootstrap>
16+
<OpsErrorBoundary>
17+
<AppBootstrap>
18+
<App />
19+
</AppBootstrap>
20+
</OpsErrorBoundary>
1821
</StrictMode>,
1922
);

0 commit comments

Comments
 (0)