|
| 1 | +import Router from 'next/router'; |
| 2 | +import type { ErrorInfo } from 'react'; |
| 3 | +import React, { Component } from 'react'; |
| 4 | + |
| 5 | +import type { |
| 6 | + ErrorBoundaryProps, |
| 7 | + ErrorBoundaryState, |
| 8 | + ErrorFallbackProps |
| 9 | +} from '@/types/components/error/ErrorBoundaryProps'; |
| 10 | + |
| 11 | +import GlobalErrorFallback from './GlobalErrorFallback'; |
| 12 | + |
| 13 | +/** |
| 14 | + * @description Determine whether the ordered list of reset keys has changed |
| 15 | + * between two renders. A change triggers an automatic reset of the boundary. |
| 16 | + * @param {unknown[]} previous - The previous reset keys. |
| 17 | + * @param {unknown[]} next - The next reset keys. |
| 18 | + */ |
| 19 | +function haveResetKeysChanged(previous: unknown[] = [], next: unknown[] = []): boolean { |
| 20 | + if (previous.length !== next.length) return true; |
| 21 | + |
| 22 | + return previous.some((key, index) => !Object.is(key, next[index])); |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * @description A reusable React error boundary that isolates rendering failures |
| 27 | + * in its subtree, keeps the surrounding layout shell interactive, and renders |
| 28 | + * an accessible recovery UI. It supports custom fallbacks, imperative resets via |
| 29 | + * `resetKeys`, and automatic recovery on route changes. |
| 30 | + */ |
| 31 | +export default class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> { |
| 32 | + constructor(props: ErrorBoundaryProps) { |
| 33 | + super(props); |
| 34 | + this.state = { hasError: false, error: null, errorInfo: null }; |
| 35 | + this.reset = this.reset.bind(this); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @description Update state so the next render shows the fallback UI. |
| 40 | + * @param {Error} error - The error thrown by a descendant component. |
| 41 | + */ |
| 42 | + static getDerivedStateFromError(error: Error): Partial<ErrorBoundaryState> { |
| 43 | + return { hasError: true, error }; |
| 44 | + } |
| 45 | + |
| 46 | + componentDidMount(): void { |
| 47 | + const { resetOnRouteChange = true } = this.props; |
| 48 | + |
| 49 | + if (resetOnRouteChange) { |
| 50 | + Router.events.on('routeChangeComplete', this.reset); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @description Log the caught error and forward it to the optional handler. |
| 56 | + * @param {Error} error - The error thrown by a descendant component. |
| 57 | + * @param {ErrorInfo} errorInfo - React error info with the component stack. |
| 58 | + */ |
| 59 | + componentDidCatch(error: Error, errorInfo: ErrorInfo): void { |
| 60 | + const { onError } = this.props; |
| 61 | + |
| 62 | + this.setState({ errorInfo }); |
| 63 | + onError?.(error, errorInfo); |
| 64 | + |
| 65 | + // eslint-disable-next-line no-console |
| 66 | + console.error('ErrorBoundary caught an error:', error, errorInfo); |
| 67 | + } |
| 68 | + |
| 69 | + componentDidUpdate(prevProps: ErrorBoundaryProps): void { |
| 70 | + const { hasError } = this.state; |
| 71 | + const { resetKeys } = this.props; |
| 72 | + |
| 73 | + if (hasError && haveResetKeysChanged(prevProps.resetKeys, resetKeys)) { |
| 74 | + this.reset(); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + componentWillUnmount(): void { |
| 79 | + const { resetOnRouteChange = true } = this.props; |
| 80 | + |
| 81 | + if (resetOnRouteChange) { |
| 82 | + Router.events.off('routeChangeComplete', this.reset); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @description Clear the error state so the protected subtree re-mounts. |
| 88 | + */ |
| 89 | + reset(): void { |
| 90 | + const { hasError } = this.state; |
| 91 | + const { onReset } = this.props; |
| 92 | + |
| 93 | + if (!hasError) return; |
| 94 | + |
| 95 | + this.setState({ hasError: false, error: null, errorInfo: null }); |
| 96 | + onReset?.(); |
| 97 | + } |
| 98 | + |
| 99 | + render(): React.ReactNode { |
| 100 | + const { hasError, error, errorInfo } = this.state; |
| 101 | + const { children, fallback } = this.props; |
| 102 | + |
| 103 | + if (!hasError) return children; |
| 104 | + |
| 105 | + const fallbackProps: ErrorFallbackProps = { error, errorInfo, reset: this.reset }; |
| 106 | + |
| 107 | + if (typeof fallback === 'function') { |
| 108 | + return fallback(fallbackProps); |
| 109 | + } |
| 110 | + |
| 111 | + if (fallback !== undefined && fallback !== null) { |
| 112 | + return fallback; |
| 113 | + } |
| 114 | + |
| 115 | + return <GlobalErrorFallback {...fallbackProps} />; |
| 116 | + } |
| 117 | +} |
0 commit comments