-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Error boundary handling for useQueries #4177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
TkDodo
merged 14 commits into
TanStack:main
from
zorzysty:error_boundary_for_usequeries
Sep 24, 2022
Merged
Changes from 6 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d4bf025
feat: handle useErrorBoundary option in useQueries
zorzysty 5d7d9ef
chore: update .all-contributorsrc
zorzysty 3278944
style: fix formatting issue
zorzysty 79fe68c
test: stabilize a test that fails on production but passes locally
zorzysty 237912b
refactor: rename query to options
zorzysty 7f81e88
refactor: extract reusable code of error boundary handling
zorzysty 060de7a
style: apply prettier
zorzysty c92a897
test: reject promise with Error instead of string to avoid TS error
zorzysty 3fd6426
test: cover a case where useErrorBoundary is a function
zorzysty 3518f3c
refactor: improve typing of getHasError
zorzysty 096c82a
style: apply prettier formatting
zorzysty e767221
Merge remote-tracking branch 'origin/master' into error_boundary_for_…
zorzysty a48f3f4
fix: add type keyword for imports
zorzysty e5ab278
refactor: revert renaming `query` to `options` for more consistency
zorzysty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { | ||
DefaultedQueryObserverOptions, | ||
Query, | ||
QueryKey, | ||
QueryObserverResult, | ||
} from '@tanstack/query-core' | ||
import { QueryErrorResetBoundaryValue } from './QueryErrorResetBoundary' | ||
import * as React from 'react' | ||
import { shouldThrowError } from './utils' | ||
|
||
export const ensurePreventErrorBoundaryRetry = < | ||
TQueryFnData, | ||
TError, | ||
TData, | ||
TQueryData, | ||
TQueryKey extends QueryKey, | ||
>( | ||
options: DefaultedQueryObserverOptions< | ||
TQueryFnData, | ||
TError, | ||
TData, | ||
TQueryData, | ||
TQueryKey | ||
>, | ||
errorResetBoundary: QueryErrorResetBoundaryValue, | ||
) => { | ||
if (options.suspense || options.useErrorBoundary) { | ||
// Prevent retrying failed query if the error boundary has not been reset yet | ||
if (!errorResetBoundary.isReset()) { | ||
options.retryOnMount = false | ||
} | ||
} | ||
} | ||
|
||
export const useClearResetErrorBoundary = ( | ||
errorResetBoundary: QueryErrorResetBoundaryValue, | ||
) => { | ||
React.useEffect(() => { | ||
errorResetBoundary.clearReset() | ||
}, [errorResetBoundary]) | ||
} | ||
|
||
export const getHasError = < | ||
TData, | ||
TError, | ||
TQueryFnData, | ||
TQueryData, | ||
TQueryKey extends QueryKey, | ||
TUseErrorBoundaryFn extends (...args: any[]) => boolean, | ||
>({ | ||
result, | ||
errorResetBoundary, | ||
useErrorBoundary, | ||
query, | ||
}: { | ||
result: QueryObserverResult<TData, TError> | ||
errorResetBoundary: QueryErrorResetBoundaryValue | ||
useErrorBoundary: boolean | TUseErrorBoundaryFn | undefined | ||
query: Query<TQueryFnData, TError, TQueryData, TQueryKey> | ||
}) => { | ||
return ( | ||
result.isError && | ||
!errorResetBoundary.isReset() && | ||
!result.isFetching && | ||
shouldThrowError(useErrorBoundary, [result.error, query] as Parameters<TUseErrorBoundaryFn>) | ||
TkDodo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.