|
| 1 | +import * as Sentry from '@sentry/browser'; |
| 2 | +import * as hoistNonReactStatic from 'hoist-non-react-statics'; |
| 3 | +import * as React from 'react'; |
| 4 | + |
| 5 | +export const UNKNOWN_COMPONENT = 'unknown'; |
| 6 | + |
| 7 | +export type FallbackRender = (fallback: { |
| 8 | + error: Error | null; |
| 9 | + componentStack: string | null; |
| 10 | + resetError(): void; |
| 11 | +}) => React.ReactNode; |
| 12 | + |
| 13 | +export type ErrorBoundaryProps = { |
| 14 | + showDialog?: boolean; |
| 15 | + dialogOptions?: Sentry.ReportDialogOptions; |
| 16 | + // tslint:disable-next-line: no-null-undefined-union |
| 17 | + fallback?: React.ReactNode | FallbackRender; |
| 18 | + onError?(error: Error, componentStack: string): void; |
| 19 | + onMount?(): void; |
| 20 | + onReset?(error: Error | null, componentStack: string | null): void; |
| 21 | + onUnmount?(error: Error | null, componentStack: string | null): void; |
| 22 | +}; |
| 23 | + |
| 24 | +type ErrorBoundaryState = { |
| 25 | + componentStack: string | null; |
| 26 | + error: Error | null; |
| 27 | +}; |
| 28 | + |
| 29 | +const INITIAL_STATE = { |
| 30 | + componentStack: null, |
| 31 | + error: null, |
| 32 | +}; |
| 33 | + |
| 34 | +class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundaryState> { |
| 35 | + public state: ErrorBoundaryState = INITIAL_STATE; |
| 36 | + |
| 37 | + public componentDidCatch(error: Error, { componentStack }: React.ErrorInfo): void { |
| 38 | + Sentry.captureException(error, { contexts: { react: { componentStack } } }); |
| 39 | + const { onError, showDialog, dialogOptions } = this.props; |
| 40 | + if (onError) { |
| 41 | + onError(error, componentStack); |
| 42 | + } |
| 43 | + if (showDialog) { |
| 44 | + Sentry.showReportDialog(dialogOptions); |
| 45 | + } |
| 46 | + |
| 47 | + // componentDidCatch is used over getDerivedStateFromError |
| 48 | + // so that componentStack is accessible through state. |
| 49 | + this.setState({ error, componentStack }); |
| 50 | + } |
| 51 | + |
| 52 | + public componentDidMount(): void { |
| 53 | + const { onMount } = this.props; |
| 54 | + if (onMount) { |
| 55 | + onMount(); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public componentWillUnmount(): void { |
| 60 | + const { error, componentStack } = this.state; |
| 61 | + const { onUnmount } = this.props; |
| 62 | + if (onUnmount) { |
| 63 | + onUnmount(error, componentStack); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + public resetErrorBoundary = () => { |
| 68 | + const { onReset } = this.props; |
| 69 | + if (onReset) { |
| 70 | + onReset(this.state.error, this.state.componentStack); |
| 71 | + } |
| 72 | + this.setState(INITIAL_STATE); |
| 73 | + }; |
| 74 | + |
| 75 | + public render(): React.ReactNode { |
| 76 | + const { fallback } = this.props; |
| 77 | + const { error, componentStack } = this.state; |
| 78 | + |
| 79 | + if (error) { |
| 80 | + if (React.isValidElement(fallback)) { |
| 81 | + return fallback; |
| 82 | + } |
| 83 | + if (typeof fallback === 'function') { |
| 84 | + return fallback({ error, componentStack, resetError: this.resetErrorBoundary }) as FallbackRender; |
| 85 | + } |
| 86 | + |
| 87 | + // Fail gracefully if no fallback provided |
| 88 | + return null; |
| 89 | + } |
| 90 | + |
| 91 | + return this.props.children; |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +function withErrorBoundary<P extends object>( |
| 96 | + WrappedComponent: React.ComponentType<P>, |
| 97 | + errorBoundaryOptions: ErrorBoundaryProps, |
| 98 | +): React.FC<P> { |
| 99 | + const componentDisplayName = WrappedComponent.displayName || WrappedComponent.name || UNKNOWN_COMPONENT; |
| 100 | + |
| 101 | + const Wrapped: React.FC<P> = (props: P) => ( |
| 102 | + <ErrorBoundary {...errorBoundaryOptions}> |
| 103 | + <WrappedComponent {...props} /> |
| 104 | + </ErrorBoundary> |
| 105 | + ); |
| 106 | + |
| 107 | + Wrapped.displayName = `errorBoundary(${componentDisplayName})`; |
| 108 | + |
| 109 | + // Copy over static methods from Wrapped component to Profiler HOC |
| 110 | + // See: https://reactjs.org/docs/higher-order-components.html#static-methods-must-be-copied-over |
| 111 | + hoistNonReactStatic(Wrapped, WrappedComponent); |
| 112 | + return Wrapped; |
| 113 | +} |
| 114 | + |
| 115 | +export { ErrorBoundary, withErrorBoundary }; |
0 commit comments