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.
71 lines
2.1 KiB
TypeScript
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;
|