Skip to content

Commit 77addbb

Browse files
fix: align error boundary fallback types with react-error-boundary
1 parent 9971ac8 commit 77addbb

1 file changed

Lines changed: 37 additions & 30 deletions

File tree

‎src/utils/suspenseWrapper.tsx‎

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import { Flex, Spinner } from '@chakra-ui/react'
44
import { Dialog, Portal } from '@chakra-ui/react'
55
import { QueryErrorResetBoundary } from '@tanstack/react-query'
66
import { type ComponentType, type JSX, type ReactNode, Suspense } from 'react'
7-
import { ErrorBoundary, type ErrorBoundaryPropsWithRender } from 'react-error-boundary'
7+
import {
8+
ErrorBoundary,
9+
type ErrorBoundaryPropsWithRender,
10+
type FallbackProps,
11+
} from 'react-error-boundary'
812

913
export type DefaultFallbackFormat = 'dialog' | 'default'
1014

@@ -84,11 +88,6 @@ export const withSuspense = <WrappedProps extends object>(
8488
}
8589
}
8690

87-
interface ErrorBoundaryPropsWithRenderProps {
88-
error: Error
89-
resetErrorBoundary: () => void
90-
}
91-
9291
/**
9392
* Default fallback render for ErrorBoundary
9493
*
@@ -99,12 +98,16 @@ interface ErrorBoundaryPropsWithRenderProps {
9998
const defaultFallbackRender: ErrorBoundaryPropsWithRender['fallbackRender'] = ({
10099
error,
101100
resetErrorBoundary,
102-
}: ErrorBoundaryPropsWithRenderProps): ReactNode => (
103-
<>
104-
<div>{error.message}</div>
105-
<PrimaryButton onClick={resetErrorBoundary}>Try Again</PrimaryButton>
106-
</>
107-
)
101+
}: FallbackProps): ReactNode => {
102+
const message = error instanceof Error ? error.message : 'Something went wrong.'
103+
104+
return (
105+
<>
106+
<div>{message}</div>
107+
<PrimaryButton onClick={resetErrorBoundary}>Try Again</PrimaryButton>
108+
</>
109+
)
110+
}
108111

109112
/**
110113
* Default reset for ErrorBoundary shown on a dialog
@@ -117,24 +120,28 @@ const defaultFallbackRender: ErrorBoundaryPropsWithRender['fallbackRender'] = ({
117120
const defaultFallbackRenderDialog: ErrorBoundaryPropsWithRender['fallbackRender'] = ({
118121
error,
119122
resetErrorBoundary,
120-
}: ErrorBoundaryPropsWithRenderProps): ReactNode => (
121-
<Dialog.Root
122-
open
123-
size="xs"
124-
>
125-
<Portal>
126-
<Dialog.Backdrop />
127-
<Dialog.Positioner>
128-
<Dialog.Content>
129-
<GeneralMessage
130-
actionButton={<PrimaryButton onClick={resetErrorBoundary}>Try again</PrimaryButton>}
131-
message={error.message}
132-
/>
133-
</Dialog.Content>
134-
</Dialog.Positioner>
135-
</Portal>
136-
</Dialog.Root>
137-
)
123+
}: FallbackProps): ReactNode => {
124+
const message = error instanceof Error ? error.message : 'Something went wrong.'
125+
126+
return (
127+
<Dialog.Root
128+
open
129+
size="xs"
130+
>
131+
<Portal>
132+
<Dialog.Backdrop />
133+
<Dialog.Positioner>
134+
<Dialog.Content>
135+
<GeneralMessage
136+
actionButton={<PrimaryButton onClick={resetErrorBoundary}>Try again</PrimaryButton>}
137+
message={message}
138+
/>
139+
</Dialog.Content>
140+
</Dialog.Positioner>
141+
</Portal>
142+
</Dialog.Root>
143+
)
144+
}
138145

139146
export type WithSuspenseAndRetryProps = {
140147
fallbackRender?: ErrorBoundaryPropsWithRender['fallbackRender']

0 commit comments

Comments
 (0)