Open History to see the last commit
1import { shouldThrowError } from '@tanstack/query-core'
2import { useEffect } from 'preact/hooks'
3import type {
4 DefaultedQueryObserverOptions,
5 Query,
6 QueryKey,
7 QueryObserverResult,
8 ThrowOnError,
9} from '@tanstack/query-core'
10import type { QueryErrorResetBoundaryValue } from './QueryErrorResetBoundary'
11β
12/**
13 * Turns off `retryOnMount` for a query that throws its errors (with `suspense` or `throwOnError`),
14 * unless the error boundary has been reset, so a remount doesn't retry the failed query before the
15 * user resets the boundary. Mutates the options.
16 * @param options - The defaulted query options to adjust.
17 * @param errorResetBoundary - The value of the nearest `QueryErrorResetBoundary`.
18 * @param query - The query, used to evaluate a `throwOnError` function against its error.
19 */
20export const ensurePreventErrorBoundaryRetry = <
21 TQueryFnData,
22 TError,
23 TData,
24 TQueryData,
25 TQueryKey extends QueryKey,
26>(
27 options: DefaultedQueryObserverOptions<
28 TQueryFnData,
29 TError,
30 TData,
31 TQueryData,
32 TQueryKey
33 >,
34 errorResetBoundary: QueryErrorResetBoundaryValue,
35 query: Query<TQueryFnData, TError, TQueryData, TQueryKey> | undefined,
36) => {
37 const throwOnError =
38 query?.state.status === 'error' &&
39 typeof options.throwOnError === 'function'
40 ? shouldThrowError(options.throwOnError, [
41 query.state.error as TError,
42 query,
43 ])
44 : options.throwOnError
45β
46 if (options.suspense || throwOnError) {
47 // Prevent retrying failed query if the error boundary has not been reset yet
48 if (!errorResetBoundary.isReset()) {
49 options.retryOnMount = false
50 }
51 }
52}
53β
54/**
55 * Clears the reset state of the error boundary after the component mounts, so later errors are
56 * thrown to the boundary again.
57 * @param errorResetBoundary - The value of the nearest `QueryErrorResetBoundary`.
58 */
59export const useClearResetErrorBoundary = (
60 errorResetBoundary: QueryErrorResetBoundaryValue,
61) => {
62 useEffect(() => {
63 errorResetBoundary.clearReset()
64 }, [errorResetBoundary])
65}
66β
67/**
68 * Checks whether the query error should be thrown to the nearest error boundary: the query errored
69 * and isn't fetching, the boundary hasn't been reset, and either `suspense` is enabled with no data
70 * or `throwOnError` says so.
71 * @param params - The observer `result`, the `errorResetBoundary`, the `throwOnError` option, the
72 * `query`, and the `suspense` option.
73 * @returns `true` if the error should be thrown.
74 */
75export const getHasError = <
76 TData,
77 TError,
78 TQueryFnData,
79 TQueryData,
80 TQueryKey extends QueryKey,
81>({
82 result,
83 errorResetBoundary,
84 throwOnError,
85 query,
86 suspense,
87}: {
88 result: QueryObserverResult<TData, TError>
89 errorResetBoundary: QueryErrorResetBoundaryValue
90 throwOnError: ThrowOnError<TQueryFnData, TError, TQueryData, TQueryKey>
91 query: Query<TQueryFnData, TError, TQueryData, TQueryKey> | undefined
92 suspense: boolean | undefined
93}) => {
94 return (
95 result.isError &&
96 !errorResetBoundary.isReset() &&
97 !result.isFetching &&
98 query &&
99 ((suspense && result.data === undefined) ||
100 shouldThrowError(throwOnError, [result.error, query]))
101 )
102}