Skip to content
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

fix(query-core): handle errors that occur inside setData method #7966

Merged
merged 12 commits into from
Aug 30, 2024

Conversation

Efimenko
Copy link
Contributor

As PR is a bit stuck, this PR implements the same, with respect to the comments left in the first PR. Fix #6954

@Efimenko
Copy link
Contributor Author

@TkDodo Also, currently, after the second fetch, the error will be maximum call stack size exceeded. So, probably, we need to check if the data is serializable or not, rather than wait call stack size exceeded. Inside replaceEqualDeep, some kind of:

try {
  JSON.stringify(data)
} catch {
  throw new Error('Data is not serializable')
}

Copy link

nx-cloud bot commented Aug 27, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit 82d75b7. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


✅ Successfully ran 2 targets

Sent with 💌 from NxCloud.

Copy link

pkg-pr-new bot commented Aug 27, 2024

commit: 82d75b7

@tanstack/angular-query-devtools-experimental

pnpm add https://pkg.pr.new/@tanstack/angular-query-devtools-experimental@7966

@tanstack/angular-query-experimental

pnpm add https://pkg.pr.new/@tanstack/angular-query-experimental@7966

@tanstack/eslint-plugin-query

pnpm add https://pkg.pr.new/@tanstack/eslint-plugin-query@7966

@tanstack/query-async-storage-persister

pnpm add https://pkg.pr.new/@tanstack/query-async-storage-persister@7966

@tanstack/query-broadcast-client-experimental

pnpm add https://pkg.pr.new/@tanstack/query-broadcast-client-experimental@7966

@tanstack/query-core

pnpm add https://pkg.pr.new/@tanstack/query-core@7966

@tanstack/query-devtools

pnpm add https://pkg.pr.new/@tanstack/query-devtools@7966

@tanstack/query-persist-client-core

pnpm add https://pkg.pr.new/@tanstack/query-persist-client-core@7966

@tanstack/query-sync-storage-persister

pnpm add https://pkg.pr.new/@tanstack/query-sync-storage-persister@7966

@tanstack/react-query

pnpm add https://pkg.pr.new/@tanstack/react-query@7966

@tanstack/react-query-devtools

pnpm add https://pkg.pr.new/@tanstack/react-query-devtools@7966

@tanstack/react-query-next-experimental

pnpm add https://pkg.pr.new/@tanstack/react-query-next-experimental@7966

@tanstack/react-query-persist-client

pnpm add https://pkg.pr.new/@tanstack/react-query-persist-client@7966

@tanstack/solid-query

pnpm add https://pkg.pr.new/@tanstack/solid-query@7966

@tanstack/solid-query-devtools

pnpm add https://pkg.pr.new/@tanstack/solid-query-devtools@7966

@tanstack/solid-query-persist-client

pnpm add https://pkg.pr.new/@tanstack/solid-query-persist-client@7966

@tanstack/svelte-query

pnpm add https://pkg.pr.new/@tanstack/svelte-query@7966

@tanstack/svelte-query-devtools

pnpm add https://pkg.pr.new/@tanstack/svelte-query-devtools@7966

@tanstack/svelte-query-persist-client

pnpm add https://pkg.pr.new/@tanstack/svelte-query-persist-client@7966

@tanstack/vue-query

pnpm add https://pkg.pr.new/@tanstack/vue-query@7966

@tanstack/vue-query-devtools

pnpm add https://pkg.pr.new/@tanstack/vue-query-devtools@7966

Open in Stackblitz

More templates

@Efimenko Efimenko requested a review from TkDodo August 27, 2024 15:58
packages/query-core/src/utils.ts Outdated Show resolved Hide resolved
@Efimenko Efimenko requested a review from TkDodo August 30, 2024 08:29
Copy link

codecov bot commented Aug 30, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 63.36%. Comparing base (82bfc34) to head (82d75b7).
Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##             main    #7966       +/-   ##
===========================================
+ Coverage   45.09%   63.36%   +18.26%     
===========================================
  Files         188      127       -61     
  Lines        7149     4545     -2604     
  Branches     1604     1267      -337     
===========================================
- Hits         3224     2880      -344     
+ Misses       3562     1437     -2125     
+ Partials      363      228      -135     
Components Coverage Δ
@tanstack/angular-query-devtools-experimental ∅ <ø> (∅)
@tanstack/angular-query-experimental 86.58% <ø> (ø)
@tanstack/eslint-plugin-query ∅ <ø> (∅)
@tanstack/query-async-storage-persister 43.85% <ø> (ø)
@tanstack/query-broadcast-client-experimental ∅ <ø> (∅)
@tanstack/query-codemods ∅ <ø> (∅)
@tanstack/query-core 92.87% <100.00%> (+0.04%) ⬆️
@tanstack/query-devtools 5.24% <ø> (ø)
@tanstack/query-persist-client-core 57.73% <ø> (ø)
@tanstack/query-sync-storage-persister 82.50% <ø> (ø)
@tanstack/react-query 92.50% <ø> (ø)
@tanstack/react-query-devtools 10.71% <ø> (ø)
@tanstack/react-query-next-experimental ∅ <ø> (∅)
@tanstack/react-query-persist-client 100.00% <ø> (ø)
@tanstack/solid-query 78.20% <ø> (ø)
@tanstack/solid-query-devtools ∅ <ø> (∅)
@tanstack/solid-query-persist-client 100.00% <ø> (ø)
@tanstack/svelte-query 87.33% <ø> (ø)
@tanstack/svelte-query-devtools ∅ <ø> (∅)
@tanstack/svelte-query-persist-client 100.00% <ø> (ø)
@tanstack/vue-query 71.42% <ø> (ø)
@tanstack/vue-query-devtools ∅ <ø> (∅)

Copy link
Collaborator

@TkDodo TkDodo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks. I adapted the message a bit

@TkDodo TkDodo merged commit 50315ac into TanStack:main Aug 30, 2024
8 checks passed
@Efimenko Efimenko deleted the issue-6954 branch August 30, 2024 17:29
if (process.env.NODE_ENV !== 'production') {
try {
JSON.stringify(prevData)
JSON.stringify(data)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This now throws an error when data contains BigInt. But it seems like react-query supports having BigInt in the query result. Should I create an issue for this?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm think it's still good to log the error because structuralSharing creates an overhead and you should likely turn it off if you have something that isn't serializable. But we should likely remove the throw. Logging is enough. Please file a PR

Copy link

@ethos-vitalii ethos-vitalii Sep 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @Efimenko for raising that PR 🙌

@TkDodo I have a question. From what I see in replaceEqualDeep, there's nothing leading BigInt comparison to break. For example, 123n === 123n will be true. Is there anything else I'm missing where BigInt could break this.setData()?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting. I didn't know that BigInt was referentially equal, but not json serializable. According to the documentation, we only support json serializable values in structuralSharing, so I think the warning is still warranted.

Copy link
Contributor

@jxom jxom Sep 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this seems to have affected a lot of upstream Wagmi users. Wonder if this should have been a breaking change. Changing from throwing to just a warning would be better though!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's "just" a dev mode warning. Why would it be breaking?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies. Didn't see this commit.

Copy link
Contributor

@jxom jxom Sep 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TkDodo – would you be open to a PR that checked for cyclic references instead of checking for non-serializable values via JSON.stringify? Feel like a lot of consumers would be invoking query functions that return BigInts, so it might be a bit odd seeing these warning messages flying around when replaceEqualDeep is also compatible with BigInts.

Wonder if it’s worth conveying that structural sharing is moreso only compatible with referentially stable values instead of JSON-serializable values.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wonder if it’s worth conveying that structural sharing is moreso only compatible with referentially stable values instead of JSON-serializable values.

I simply wasn't aware that replaceEqualDeep works with BigInt - our general recommendation is to use json serializable values. If we say BigInt is supported, then why isn't Map supported, or Set, or Date, or ... it's a slippery slope. Also, in queryKeys, we actually use JSON.stringify, so if you use a BigInt there, it will fail.

Why is JavaScript so weird that BigInt is referentially stable across values, but not serializable lol.

would you be open to a PR that checked for cyclic references instead of checking for non-serializable values via JSON.stringify?

yeah I guess that's fine. I think just calling replaceEqualDeep itself in a try/catch would just work, too ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done! #8030

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The query gets stuck at fetching status when re-fetched data has cycles
4 participants