mirror of
https://gitee.com/ant-design/ant-design.git
synced 2024-11-30 11:08:45 +08:00
e80e40b764
Co-authored-by: jiang_chunqiao <chunqiao_jiang@icloud.com>
45 lines
1.1 KiB
TypeScript
45 lines
1.1 KiB
TypeScript
import * as React from 'react';
|
|
import Alert from '.';
|
|
|
|
interface ErrorBoundaryProps {
|
|
message?: React.ReactNode;
|
|
description?: React.ReactNode;
|
|
children?: React.ReactNode;
|
|
}
|
|
|
|
interface ErrorBoundaryStates {
|
|
error?: Error | null;
|
|
info?: {
|
|
componentStack?: string;
|
|
};
|
|
}
|
|
|
|
class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundaryStates> {
|
|
state = {
|
|
error: undefined,
|
|
info: {
|
|
componentStack: '',
|
|
},
|
|
};
|
|
|
|
componentDidCatch(error: Error | null, info: object) {
|
|
this.setState({ error, info });
|
|
}
|
|
|
|
render() {
|
|
const { message, description, children } = this.props;
|
|
const { error, info } = this.state;
|
|
const componentStack = info && info.componentStack ? info.componentStack : null;
|
|
const errorMessage = typeof message === 'undefined' ? (error || '').toString() : message;
|
|
const errorDescription = typeof description === 'undefined' ? componentStack : description;
|
|
if (error) {
|
|
return (
|
|
<Alert type="error" message={errorMessage} description={<pre>{errorDescription}</pre>} />
|
|
);
|
|
}
|
|
return children;
|
|
}
|
|
}
|
|
|
|
export default ErrorBoundary;
|