|
| 1 | +import { Component, createElement, ErrorInfo } from 'preact' |
| 2 | +import { ErrorBoundaryContext } from './ErrorBoundaryContext' |
| 3 | +import { ErrorBoundaryProps, FallbackProps } from './types' |
| 4 | + |
| 5 | +type ErrorBoundaryState = |
| 6 | + | { |
| 7 | + didCatch: true |
| 8 | + error: any |
| 9 | + } |
| 10 | + | { |
| 11 | + didCatch: false |
| 12 | + error: null |
| 13 | + } |
| 14 | + |
| 15 | +const initialState: ErrorBoundaryState = { |
| 16 | + didCatch: false, |
| 17 | + error: null, |
| 18 | +} |
| 19 | + |
| 20 | +export class ErrorBoundary extends Component< |
| 21 | + ErrorBoundaryProps, |
| 22 | + ErrorBoundaryState |
| 23 | +> { |
| 24 | + constructor(props: ErrorBoundaryProps) { |
| 25 | + super(props) |
| 26 | + |
| 27 | + this.resetErrorBoundary = this.resetErrorBoundary.bind(this) |
| 28 | + this.state = initialState |
| 29 | + } |
| 30 | + |
| 31 | + static getDerivedStateFromError(error: Error) { |
| 32 | + return { didCatch: true, error } |
| 33 | + } |
| 34 | + |
| 35 | + resetErrorBoundary(...args: any[]) { |
| 36 | + const { error } = this.state |
| 37 | + |
| 38 | + if (error !== null) { |
| 39 | + this.props.onReset?.({ |
| 40 | + args, |
| 41 | + reason: 'imperative-api', |
| 42 | + }) |
| 43 | + |
| 44 | + this.setState(initialState) |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + componentDidCatch(error: Error, info: ErrorInfo) { |
| 49 | + /** |
| 50 | + * To emulate the react behaviour of console.error |
| 51 | + * we add one here to show that the errors bubble up |
| 52 | + * to the system and can be seen in the console |
| 53 | + */ |
| 54 | + console.error('%o\n\n%s', error, info) |
| 55 | + this.props.onError?.(error, info) |
| 56 | + } |
| 57 | + |
| 58 | + componentDidUpdate( |
| 59 | + prevProps: ErrorBoundaryProps, |
| 60 | + prevState: ErrorBoundaryState, |
| 61 | + ) { |
| 62 | + const { didCatch } = this.state |
| 63 | + const { resetKeys } = this.props |
| 64 | + |
| 65 | + // There's an edge case where if the thing that triggered the error happens to *also* be in the resetKeys array, |
| 66 | + // we'd end up resetting the error boundary immediately. |
| 67 | + // This would likely trigger a second error to be thrown. |
| 68 | + // So we make sure that we don't check the resetKeys on the first call of cDU after the error is set. |
| 69 | + |
| 70 | + if ( |
| 71 | + didCatch && |
| 72 | + prevState.error !== null && |
| 73 | + hasArrayChanged(prevProps.resetKeys, resetKeys) |
| 74 | + ) { |
| 75 | + this.props.onReset?.({ |
| 76 | + next: resetKeys, |
| 77 | + prev: prevProps.resetKeys, |
| 78 | + reason: 'keys', |
| 79 | + }) |
| 80 | + |
| 81 | + this.setState(initialState) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + render() { |
| 86 | + const { children, fallbackRender, FallbackComponent, fallback } = this.props |
| 87 | + const { didCatch, error } = this.state |
| 88 | + |
| 89 | + let childToRender = children |
| 90 | + |
| 91 | + if (didCatch) { |
| 92 | + const props: FallbackProps = { |
| 93 | + error, |
| 94 | + resetErrorBoundary: this.resetErrorBoundary, |
| 95 | + } |
| 96 | + |
| 97 | + if (typeof fallbackRender === 'function') { |
| 98 | + childToRender = fallbackRender(props) |
| 99 | + } else if (FallbackComponent) { |
| 100 | + childToRender = createElement(FallbackComponent, props) |
| 101 | + } else if (fallback !== undefined) { |
| 102 | + childToRender = fallback |
| 103 | + } else { |
| 104 | + console.error( |
| 105 | + 'preact-error-boundary requires either a fallback, fallbackRender, or FallbackComponent prop', |
| 106 | + ) |
| 107 | + |
| 108 | + throw error |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return createElement( |
| 113 | + ErrorBoundaryContext.Provider, |
| 114 | + { |
| 115 | + value: { |
| 116 | + didCatch, |
| 117 | + error, |
| 118 | + resetErrorBoundary: this.resetErrorBoundary, |
| 119 | + }, |
| 120 | + }, |
| 121 | + childToRender, |
| 122 | + ) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +function hasArrayChanged(a: any[] = [], b: any[] = []) { |
| 127 | + return ( |
| 128 | + a.length !== b.length || a.some((item, index) => !Object.is(item, b[index])) |
| 129 | + ) |
| 130 | +} |
0 commit comments