Skip to content

Fix #1484 mutation not calling onSettled if onError throws - #4939

Closed
arthurgeron wants to merge 5 commits into
TanStack:mainfrom
arthurgeron:fix/onError-issue-1484
Closed

arthurgeron wants to merge 5 commits into
TanStack:mainfrom
arthurgeron:fix/onError-issue-1484

Conversation

@arthurgeron

@arthurgeron arthurgeron commented Feb 4, 2023 •

Copy link
Copy Markdown

When onError throws it aborts the current scope and dispatches another "error" state without finishing (e.g. calling onSetlled), it'd also never report the exception by calling onError with it or log it to console, which causes the mutation to freeze and always report a loading state as reported here

Bug can be reproduced here

    causes the state to not be cleaned up before the next error flow starts
    the bug caused the mutation to freeze and always report a loading state
@vercel

vercel Bot commented Feb 4, 2023 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for Git 鈫楋笌

1 Ignored Deployment
Name Status Preview Comments Updated
query 猬滐笍 Ignored (Inspect) Feb 13, 2023 at 2:15AM (UTC)

@codesandbox-ci

codesandbox-ci Bot commented Feb 4, 2023 •

Copy link
Copy Markdown

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 125fc29:

Sandbox Source
@tanstack/query-example-react-basic-typescript Configuration
@tanstack/query-example-solid-basic-typescript Configuration
@tanstack/query-example-svelte-basic Configuration
@tanstack/query-example-vue-basic Configuration

@arthurgeron arthurgeron changed the title Fix mutation not calling onSettled if onError throws Fix #1484 mutation not calling onSettled if onError throws Feb 5, 2023
@TkDodo

TkDodo commented Feb 12, 2023

Copy link
Copy Markdown
Collaborator

Not sure about your bug reproduction, can't get it to work (filter is not defined). I simplified it a bit:

  • mutationFn throws new Error('foo')
  • onError throws new Error('bar')

we're winding up correctly in an error state due to the existing finally clause, and the error displayed is 'foo'. This was a conscious decision because when you have two separate errors, we can only "store" one of them, and onError is a method to execute side-effects which mostly shouldn't have any errors. It is not meant to "transform" errors any further.

https://codesandbox.io/p/sandbox/icy-wave-wguwm9?file=%2Fsrc%2Findex.jsx&selection=%5B%7B%22endColumn%22%3A1%2C%22endLineNumber%22%3A11%2C%22startColumn%22%3A1%2C%22startLineNumber%22%3A11%7D%5D

@arthurgeron

arthurgeron commented Feb 13, 2023 •

Copy link
Copy Markdown
Author

I've tested the link here in incognito and it reproduced the bug correctly, the mutation fails but gets stuck in a loading state, you can see it in this video:
0fd0cd0a-98a7-4879-a200-c49ba13dfb7b.webm

we're winding up correctly in an error state due to the existing finally clause, and the error displayed is 'foo'.

Yes getting to an error state is what should happen, in reality it winds up in a loading state which does not change unless another mutation is triggered

@TkDodo

TkDodo commented Feb 13, 2023

Copy link
Copy Markdown
Collaborator

can you please try to make it fail in my reduced example please

@arthurgeron-work

arthurgeron-work commented Feb 13, 2023 •

Copy link
Copy Markdown

can you please try to make it fail in my reduced example please

I've reproduced it in your example but quite inconsistently, we could be dealing with two different bugs here:

1- The one I reported which might only exist in React Query and not TanStack. I strongly believe that might be the case because it's easily reproduced as you can see in the codesandbox I've forked from the gallery and modified, no timeouts needed. Still it might be important to fix it for users that haven't migrated;

2 - The one I reproduced in the first sandbox and again here but with no throws in onError, filter does not exist and trying to parse it inside a JSON on reject seems to cause it to hang in loading state, which does not happen if you define a value to filter or remove the timeout.

@TkDodo

TkDodo commented Feb 14, 2023

Copy link
Copy Markdown
Collaborator

but with no throws in onError, filter does not exist and trying to parse it inside a JSON on reject seems to cause it to hang in loading state

yeah I see it now - the promise never gets rejected because accessing filter, which doesn't exist, throws and then promise.reject is never executed, so the promise still hangs in loading state.

if you'd use an async function instead and added some sleep in there instead of creating a new Promise that never resolves or rejects (because of the Error), there wouldn't be a problem.

I'm gonna close this PR because I don't think there is an issue in the current version of Tanstack query.

@TkDodo TkDodo closed this Feb 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants