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

errorBoundaryUtils.ts

103 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` 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.status === 'error' &&
40 typeof options.throwOnError === 'function'
41 ? shouldThrowError(options.throwOnError, [
42 query.state.error as TError,
43 query,
44 ])
45 : options.throwOnError
46​
47 if (options.suspense || throwOnError) {
48 // Prevent retrying failed query if the error boundary has not been reset yet
49 if (!errorResetBoundary.isReset()) {
50 options.retryOnMount = false
51 }
52 }
54​
55/**
56 * Clears the reset state of the error boundary after the component mounts, so later errors are
57 * thrown to the boundary again.
58 * @param errorResetBoundary - The value of the nearest `QueryErrorResetBoundary`.
59 */
60export const useClearResetErrorBoundary = (
61 errorResetBoundary: QueryErrorResetBoundaryValue,
62) => {
63 React.useEffect(() => {
64 errorResetBoundary.clearReset()
65 }, [errorResetBoundary])
67​
68/**
69 * Checks whether the query error should be thrown to the nearest error boundary: the query errored
70 * and isn't fetching, the boundary hasn't been reset, and either `suspense` is enabled with no data
71 * or `throwOnError` says so.
72 * @param params - The observer `result`, the `errorResetBoundary`, the `throwOnError` option, the
73 * `query`, and the `suspense` option.
74 * @returns `true` if the error should be thrown.
75 */
76export const getHasError = <
77 TData,
78 TError,
79 TQueryFnData,
80 TQueryData,
81 TQueryKey extends QueryKey,
82>({
83 result,
84 errorResetBoundary,
85 throwOnError,
86 query,
87 suspense,
88}: {
89 result: QueryObserverResult<TData, TError>
90 errorResetBoundary: QueryErrorResetBoundaryValue
91 throwOnError: ThrowOnError<TQueryFnData, TError, TQueryData, TQueryKey>
92 query: Query<TQueryFnData, TError, TQueryData, TQueryKey> | undefined
93 suspense: boolean | undefined
94}) => {
95 return (
96 result.isError &&
97 !errorResetBoundary.isReset() &&
98 !result.isFetching &&
99 query &&
100 ((suspense && result.data === undefined) ||
101 shouldThrowError(throwOnError, [result.error, query]))
102 )