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