better errors and refactoring

This commit is contained in:
2026-01-11 21:24:18 +01:00
parent e7fede576c
commit 2ee08dc7d8
7 changed files with 232 additions and 125 deletions
+48
View File
@@ -0,0 +1,48 @@
import { Component, type ErrorInfo, type ReactNode } from "react";
import { ErrorState } from "./components/EmptyState";
interface Props {
children: ReactNode;
fallback?: ReactNode;
}
interface State {
hasError: boolean;
error: Error | null;
}
export class ErrorBoundary extends Component<Props, State> {
public state: State = {
hasError: false,
error: null
};
public static getDerivedStateFromError(error: Error): State {
return { hasError: true, error };
}
public componentDidCatch(error: Error, errorInfo: ErrorInfo) {
console.error("ErrorBoundary caught an error:", error, errorInfo);
}
public handleReset = () => {
this.setState({ hasError: false, error: null });
};
public render() {
if (this.state.hasError) {
if (this.props.fallback) {
return this.props.fallback;
}
return (
<ErrorState
message={this.state.error?.message || "An unexpected error occurred"}
onRetry={this.handleReset}
/>
);
}
return this.props.children;
}
}