Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | 1x 1x 2x 2x | import { Component, type ErrorInfo, type ReactNode } from "react";
interface Props {
readonly children: ReactNode;
}
interface State {
hasError: boolean;
error: Error | null;
}
export 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, info: ErrorInfo): void {
console.error("[ErrorBoundary] Uncaught render error:", error, info.componentStack);
}
render(): ReactNode {
Iif (this.state.hasError) {
return (
<div style={{
display: "flex",
flexDirection: "column",
alignItems: "center",
justifyContent: "center",
height: "100vh",
background: "#111",
color: "#e5e5e5",
fontFamily: "system-ui, sans-serif",
padding: "2rem",
textAlign: "center",
}}>
<h1 style={{ fontSize: "1.5rem", marginBottom: "0.5rem" }}>Something went wrong</h1>
<p style={{ color: "#999", marginBottom: "1.5rem", maxWidth: "40rem" }}>
{this.state.error?.message ?? "An unexpected error occurred."}
</p>
<button
type="button"
onClick={() => globalThis.location.reload()}
style={{
padding: "0.5rem 1.5rem",
background: "#333",
color: "#e5e5e5",
border: "1px solid #555",
borderRadius: "0.375rem",
cursor: "pointer",
}}
>
Reload page
</button>
</div>
);
}
return this.props.children;
}
}
|