Watch
1
0
Fork
You've already forked RedFlag
0
RedFlag/web/src/components/ErrorBoundary.tsx
Fimeg f340c0d084 web: log errors at the boundary, ditch the toast-logging wrapper (C6)
API errors already log via the axios interceptor — wrapping 78 toast calls
would just double-log and turn validation prompts into noise. Wrapper gone;
ErrorBoundary + window error/rejection handlers log to clientErrorLogger,
skipping axios errors so the log POST can't loop. setupApiInstance logs now too.
2026-06-15 09:28:31 -04:00

71 lines
2.1 KiB
TypeScript

import { Component, ErrorInfo, ReactNode } from 'react';
import { clientErrorLogger } from '@/lib/client-error-logger';
interface Props {
children: ReactNode;
fallback?: ReactNode;
}
interface State {
hasError: boolean;
error: Error | null;
}
class ErrorBoundary extends Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = { hasError: false, error: null };
}
static getDerivedStateFromError(error: Error): State {
return { hasError: true, error };
}
componentDidCatch(error: Error, errorInfo: ErrorInfo): void {
console.error('[ErrorBoundary] Uncaught error:', error, errorInfo);
// ETHOS #1: render crashes are history too. Ship to the backend log at the
// boundary where the crash surfaces — not coupled to the toast layer.
clientErrorLogger.logError({
subsystem: 'web',
error_type: 'javascript_error',
message: error.message,
stack_trace: error.stack,
metadata: { component_stack: errorInfo.componentStack ?? undefined },
}).catch(() => {});
}
render(): ReactNode {
if (this.state.hasError) {
if (this.props.fallback) {
return this.props.fallback;
}
return (
<div className="min-h-screen flex items-center justify-center bg-gray-50">
<div className="bg-white rounded-lg shadow-lg p-8 max-w-md text-center">
<div className="text-4xl mb-4"></div>
<h2 className="text-xl font-semibold text-gray-900 mb-2">
Something went wrong
</h2>
<p className="text-gray-600 mb-6 text-sm">
{this.state.error?.message || 'An unexpected error occurred'}
</p>
<button
onClick={() => {
this.setState({ hasError: false, error: null });
window.location.reload();
}}
className="btn btn-primary"
>
Reload page
</button>
</div>
</div>
);
}
return this.props.children;
}
}
export default ErrorBoundary;