2019-12-11 14:15:45 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import Alert from '.';
|
|
|
|
|
|
|
|
interface ErrorBoundaryProps {
|
|
|
|
message?: React.ReactNode;
|
|
|
|
description?: React.ReactNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class ErrorBoundary extends React.Component<
|
|
|
|
ErrorBoundaryProps,
|
|
|
|
{
|
|
|
|
error?: Error | null;
|
|
|
|
info: {
|
|
|
|
componentStack?: string;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
> {
|
|
|
|
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) {
|
2019-12-18 12:25:43 +08:00
|
|
|
return (
|
|
|
|
<Alert type="error" message={errorMessage} description={<pre>{errorDescription}</pre>} />
|
|
|
|
);
|
2019-12-11 14:15:45 +08:00
|
|
|
}
|
|
|
|
return children;
|
|
|
|
}
|
|
|
|
}
|