Files
cunkebao_v3/Cunkebao/app/components/ErrorBoundary.tsx
2025-04-09 09:31:09 +08:00

48 lines
1.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use client"
import React, { type ErrorInfo } from "react"
import { Card } from "@/components/ui/card"
import { Button } from "@/components/ui/button"
interface ErrorBoundaryProps {
children: React.ReactNode
}
interface ErrorBoundaryState {
hasError: boolean
error?: Error
}
class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundaryState> {
constructor(props: ErrorBoundaryProps) {
super(props)
this.state = { hasError: false }
}
static getDerivedStateFromError(error: Error): ErrorBoundaryState {
return { hasError: true, error }
}
componentDidCatch(error: Error, errorInfo: ErrorInfo) {
console.error("Uncaught error:", error, errorInfo)
}
render() {
if (this.state.hasError) {
return (
<Card className="p-6 max-w-md mx-auto mt-8">
<h2 className="text-2xl font-bold mb-4 text-red-600"></h2>
<p className="text-gray-600 mb-4"></p>
<p className="text-sm text-gray-500 mb-4">{this.state.error?.message}</p>
<Button onClick={() => this.setState({ hasError: false })}></Button>
</Card>
)
}
return this.props.children
}
}
export default ErrorBoundary