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

errorBoundaryUtils.ts

111 lines3.1 KB

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