Skip to content

Thrown errors are cached and re-thrown rather than retrying #662

Closed

Description

@onionhammer

Describe the bug
Relating to this comment, when retry failed due to error it appears that the error is being cached forever and rethrown every time the query itself is retried (not the internal retry logic)

#638 (comment)

To Reproduce
Steps to reproduce the behavior:

  1. Create a global retry handler that returns false if there is an err argument
  2. After the error is thrown, handle it with an error boundary so the next time the query is run the query should succeed

Expected behavior
The query should re-run and no error should occur

What actually happens
the error is cached and rethrown

Activity

  1. marcosvega91 commented on Jul 13, 2020

    @marcosvega91

    Hi @onionhammer thanks for opening the issue :).

    Maybe I don't understand you question well, but If you return false from your custom function the query will not re-run. You should return true to re-run the query or you could check err argument and return true|false only for particular cases.

  2. onionhammer commented on Jul 13, 2020

    @onionhammer
    Author

    @marcosvega91 the issue is if I add in custom logic to handle a 401 error (the user isnt authorized), and redirect them to the login screen, the component that is querying the resource which 401's will be unloaded, then when they login and come back, the query simply rethrows the cached error without even giving the opportunity to check if it's still an error.

    Also this is a breaking change/error in 2.x range, not present in 1.5x range of react-query

  3. marcosvega91 commented on Jul 13, 2020

    @marcosvega91

    ok, I understand. Could you please create a case on codesandbox, in this way I can take a look at your code? thanks

  4. onionhammer commented on Jul 14, 2020

    @onionhammer
    Author

    @marcosvega91 here's some code that reproduces the issue:

    https://codesandbox.io/s/bitter-bash-13bvc?file=/src/App.js

    Note if you start at the /login route and click login, it works, but if the query tries to load the data initially it will always have that error

  5. marcosvega91 commented on Jul 14, 2020

    @marcosvega91

    I have looked at your code and I think that the problem could be the suspense because the cache will be cleaned on component unmount by scheduleGarbageCollection. useEffect is not called and his callback too

    // Subscribe to the query when the subscribe function changes
    React.useEffect(() => {
      instanceRef.current = query.subscribe(() => rerender({}))
    
      // Unsubscribe when things change
      return () => instanceRef.current.unsubscribe()
    }, [query, rerender])
  6. onionhammer commented on Jul 14, 2020

    @onionhammer
    Author

    @marcosvega91 it was cleared in previous versions of react-query, but not 2.x.

    You could fork the codesandbox and fix it with your thought

  7. marcosvega91 commented on Jul 14, 2020

    @marcosvega91

    Your code seams right, I think it's a bug. @tannerlinsley What do you think ?

  8. tannerlinsley commented on Jul 31, 2020

    @tannerlinsley
    Member

    Yeah, it seems like a bug.

  9. added
    bug
    help wanted
    on Aug 3, 2020
  10. tannerlinsley commented on Aug 20, 2020

    @tannerlinsley
    Member

    This will be fixed in #840

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bughelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions