Skip to content

Commit b44c213

Browse files
authored
fix(useQuery): pass query as second parameter to useErrorBoundary function(TanStack#2660)
1 parent eb4e863 commit b44c213

File tree

5 files changed

+20
-15
lines changed

5 files changed

+20
-15
lines changed

docs/src/pages/reference/useQuery.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,11 @@ const result = useQuery({
175175
- Optional
176176
- Defaults to `true`
177177
- If set to `false`, structural sharing between query results will be disabled.
178-
- `useErrorBoundary: undefined | boolean | (error: TError) => boolean`
178+
- `useErrorBoundary: undefined | boolean | (error: TError, query: Query) => boolean`
179179
- Defaults to the global query config's `useErrorBoundary` value, which is `undefined`
180180
- Set this to `true` if you want errors to be thrown in the render phase and propagate to the nearest error boundary
181181
- Set this to `false` to disable `suspense`'s default behavior of throwing errors to the error boundary.
182-
- If set to a function, it will be passed the error and should return a boolean indicating whether to show the error in an error boundary (`true`) or return the error as state (`false`)
182+
- If set to a function, it will be passed the error and the query, and it should return a boolean indicating whether to show the error in an error boundary (`true`) or return the error as state (`false`)
183183
- `meta: Record<string, unknown>`
184184
- Optional
185185
- If set, stores additional information on the query cache entry that can be used as needed. It will be accessible wherever the `query` is available, and is also part of the `QueryFunctionContext` provided to the `queryFn`.

src/core/types.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,15 @@ export interface QueryObserverOptions<
183183
* Whether errors should be thrown instead of setting the `error` property.
184184
* If set to `true` or `suspense` is `true`, all errors will be thrown to the error boundary.
185185
* If set to `false` and `suspense` is `false`, errors are returned as state.
186-
* If set to a function, it will be passed the error and should return a boolean indicating whether to show the error in an error boundary (`true`) or return the error as state (`false`).
186+
* If set to a function, it will be passed the error and the query, and it should return a boolean indicating whether to show the error in an error boundary (`true`) or return the error as state (`false`).
187187
* Defaults to `false`.
188188
*/
189-
useErrorBoundary?: boolean | ((error: TError) => boolean)
189+
useErrorBoundary?:
190+
| boolean
191+
| ((
192+
error: TError,
193+
query: Query<TQueryFnData, TError, TQueryData, TQueryKey>
194+
) => boolean)
190195
/**
191196
* This option can be used to transform or select a part of the data returned by the query function.
192197
*/

src/react/useBaseQuery.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ export function useBaseQuery<
136136
shouldThrowError(
137137
defaultedOptions.suspense,
138138
defaultedOptions.useErrorBoundary,
139-
result.error
139+
[result.error, observer.getCurrentQuery()]
140140
)
141141
) {
142142
throw result.error

src/react/useMutation.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ export function useMutation<
8080
const options = parseMutationArgs(arg1, arg2, arg3)
8181
const queryClient = useQueryClient()
8282

83-
const obsRef = React.useRef<MutationObserver<any, any, any, any>>()
83+
const obsRef = React.useRef<
84+
MutationObserver<TData, TError, TVariables, TContext>
85+
>()
8486

8587
if (!obsRef.current) {
8688
obsRef.current = new MutationObserver(queryClient, options)
@@ -114,11 +116,9 @@ export function useMutation<
114116

115117
if (
116118
currentResult.error &&
117-
shouldThrowError(
118-
undefined,
119-
obsRef.current.options.useErrorBoundary,
120-
currentResult.error
121-
)
119+
shouldThrowError(undefined, obsRef.current.options.useErrorBoundary, [
120+
currentResult.error,
121+
])
122122
) {
123123
throw currentResult.error
124124
}

src/react/utils.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
export function shouldThrowError<TError>(
1+
export function shouldThrowError<T extends (...args: any[]) => boolean>(
22
suspense: boolean | undefined,
3-
_useErrorBoundary: boolean | ((err: TError) => boolean) | undefined,
4-
error: TError
3+
_useErrorBoundary: boolean | T | undefined,
4+
params: Parameters<T>
55
): boolean {
66
// Allow useErrorBoundary function to override throwing behavior on a per-error basis
77
if (typeof _useErrorBoundary === 'function') {
8-
return _useErrorBoundary(error)
8+
return _useErrorBoundary(...params)
99
}
1010

1111
// Allow useErrorBoundary to override suspense's throwing behavior

0 commit comments

Comments
 (0)