Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

ErrorBoundary.ts

133 lines3.4 KB

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