feat: add error boundary to prevent white-screen crashes

Wraps the entire app in an ErrorBoundary that shows a friendly
error message with reload button instead of crashing to white.
This commit is contained in:
Viktor Barzin 2026-02-28 16:18:10 +00:00
parent 8f112f30e3
commit be2f0ef282
No known key found for this signature in database
GPG key ID: 0EB088298288D958
2 changed files with 52 additions and 5 deletions

View file

@ -0,0 +1,44 @@
import { Component, type ErrorInfo, type ReactNode } from 'react';
interface Props {
children: ReactNode;
}
interface State {
hasError: boolean;
error: Error | null;
}
export class ErrorBoundary extends Component<Props, State> {
state: State = { hasError: false, error: null };
static getDerivedStateFromError(error: Error): State {
return { hasError: true, error };
}
componentDidCatch(error: Error, errorInfo: ErrorInfo) {
console.error('ErrorBoundary caught:', error, errorInfo);
}
render() {
if (this.state.hasError) {
return (
<div className="flex items-center justify-center min-h-screen bg-background">
<div className="text-center p-8 max-w-md">
<h1 className="text-xl font-bold mb-2 text-foreground">Something went wrong</h1>
<p className="text-muted-foreground mb-4 text-sm">
{this.state.error?.message ?? 'An unexpected error occurred.'}
</p>
<button
onClick={() => window.location.reload()}
className="px-4 py-2 bg-primary text-primary-foreground rounded-md text-sm font-medium hover:bg-primary/90 transition-colors"
>
Reload Page
</button>
</div>
</div>
);
}
return this.props.children;
}
}

View file

@ -2,6 +2,7 @@ import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import { BrowserRouter } from 'react-router-dom';
import App from './App.tsx';
import { ErrorBoundary } from './components/ErrorBoundary.tsx';
import './index.css';
import { AuthProvider } from "react-oidc-context";
@ -12,10 +13,12 @@ startCollector();
createRoot(document.getElementById('root')!).render(
<StrictMode>
<AuthProvider {...oidcConfig}>
<BrowserRouter>
<App />
</BrowserRouter>
</AuthProvider>
<ErrorBoundary>
<AuthProvider {...oidcConfig}>
<BrowserRouter>
<App />
</BrowserRouter>
</AuthProvider>
</ErrorBoundary>
</StrictMode>,
)