Files
jiang13-forum/frontend/src/components/ErrorBoundary.tsx

58 lines
2.1 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.
import { Component, ErrorInfo, ReactNode } from 'react';
import { Button } from '@/components/ui/button';
import { isChunkLoadError, reloadForStaleChunk } from '../utils/chunkLoad';
interface Props { children: ReactNode }
interface State { error: Error | null; reloading: boolean }
/** 捕获渲染异常;发版 chunk 失效时自动刷新 */
export default class ErrorBoundary extends Component<Props, State> {
state: State = { error: null, reloading: false };
static getDerivedStateFromError(error: Error) {
if (isChunkLoadError(error) && reloadForStaleChunk()) {
return { error, reloading: true };
}
return { error, reloading: false };
}
componentDidCatch(error: Error, info: ErrorInfo) {
console.error('[Jiang13Forum]', error, info.componentStack);
}
render() {
if (this.state.reloading) {
return (
<div className="error-page-shell">
<div className="error-page">
<div className="error-page__code" aria-hidden></div>
<h1 className="error-page__title"></h1>
<p className="error-page__desc"></p>
</div>
</div>
);
}
if (this.state.error) {
return (
<div className="error-page-shell">
<div className="error-page">
<div className="error-page__code" aria-hidden>500</div>
<h1 className="error-page__title"></h1>
<p className="error-page__desc">{this.state.error.message || '发生了意外错误,请尝试刷新页面。'}</p>
<div className="error-page__actions">
<Button size="sm" onClick={() => { this.setState({ error: null }); window.location.reload(); }}>
</Button>
<Button size="sm" variant="outline" onClick={() => { window.location.href = '/'; }}>
</Button>
</div>
</div>
</div>
);
}
return this.props.children;
}
}