Skip to content
Source
Sign inSign up
TanStack/query

NotifyManager breaks when a exception occurs in onSuccess callback #3215

Send to my agent
Closedsin3hz(GitHub profile, opens in a new tab) opened this issue 14 comments

Describe the bug

When a exception occurs in onSuccess callback

mutation.mutate(
  {},
  {
    onSuccess: () => {
      throw new Error();
    },
  },
);

whitch notify by NotifyManager.batch

batch<T>(callback: () => T): T {
  this.transactions++
  const result = callback() // exception raise
  this.transactions-- // not executed
  if (!this.transactions) { // never be 0
    this.flush()
  }
  return result
}

since NotifyManager.batch breaks, all newly created query's result would stuck in loading state even fetch succuessful

Your minimal, reproducible example

https://codesandbox.io/s/gracious-danny-zfq8u?file=/src/App.js

  1. click mutate
  2. click query

Steps to reproduce

mutation.mutate(
  {},
  {
    onSuccess: () => {
      throw new Error();
    },
  },
);

Expected behavior

QueryObervable should notify onSuccess defensively

How often does this bug happen?

No response

Screenshots or Videos

No response

Platform

irrelevant

react-query version

3.34.7

TypeScript version

No response

Additional context

No response

  1. Your minimal, reproducible example

    none

    please create a codesandbox reproduction, thanks

  2. Your minimal, reproducible example none

    please create a codesandbox reproduction, thanks

    done

  3. TkDodo added labelsbug

  4. thank you. In the attached PR, the sandbox works, and I also made a test that failed before and now works. Can you give it a quick review? Then we can merge it: https://github.com/tannerlinsley/react-query/pull/3216

  5. Thanks! I think the next batch works ok now. I haven't try yet, but I guess there still an issue that subsequence callbacks (e.g. getQueryCache().notify) on the same batch whitch exception occurs wound't get call. Catch exception above onSuccess/onSettled should fix this.

  6. do you mean that onSettled won't be called if onSuccess errors? If so, I think that's fine. I explicitly didn't want to catch errors because what should we do with it? It's not the libs responsibility to catch user errors. I think it's fine that errors bubble up, and yes that stops further execution.

    If you expect errors in your callbacks, you can and should wrap them in try/catch.

  7. I mean the cache listener in QueryObervable, will this affect other queries with same cache key if this not get call? I'm not sure, this just look like internal logic whitch shouldn't stop by user error

  8. hmm, we just tested for mutations here. For queries, it would mean that listeners (useQuery) would not be informed about the update because of the error. But I don't think that try/catch around those is really a better option...

  9. I think I'll go ahead and merge the fix and we can think about further improvements separately

  10. TkDodo closed this

  11. :tada: This issue has been resolved in version 3.34.11 :tada:

    The release is available on:

    Your semantic-release bot :package::rocket:

  12. tannerlinsley added labelsreleased

  13. :tada: This issue has been resolved in version 4.0.0-alpha.8 :tada:

    The release is available on:

    Your semantic-release bot :package::rocket:

  14. tannerlinsley added labelsreleased on @alpha

  15. I think the issue is still present, it happened to me on 4.3.4 on the onSettled function.

  16. @ncoen97 show an example please

  17. Here is a code sandbox. If you look at the onSettled, I was missing the function before the queryClient.invalidateQueries() I can't see the error in the sandbox, but in my local development this is what I'm getting:

    Uncaught (in promise) TypeError: callback is not a function
        at notifyManager.ts:51:9
        at notifyFn (notifyManager.ts:15:5)
        at notifyManager.ts:63:13
        at Array.forEach (<anonymous>)
        at notifyManager.ts:62:25
        at batchedUpdates$1 (react-dom.development.js:26096:12)
        at notifyManager.ts:61:9.
  18. this is a different problem. by doing:

    onSettled: queryClient.invalidateQueries()

    you're calling queryClient.invalidateQueries() during render, and you pass the resulting promise to onSettled, where a function is expected. TypeScript can solve this one.

Sign in to join this conversation.