-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Allow handling some errors locally while using error boundaries #2619
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
Conversation
|
This pull request is being automatically deployed with Vercel (learn more). 🔍 Inspect: https://vercel.com/tannerlinsley/react-query/AEXSu8vr5hqMeCUqgRsokV4n1p5K |
|
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 3b2da89:
|
|
I like the idea, but not really the api: an extra boolean flag that only works if another flag is set as well :/ How about extending the |
|
Also, I think if you use suspense, you have to throw all errors - it’s how suspense works |
I thought about that. It also applies to suspense so seemed a bit odd. And the implementation a bit messier, having to handle it being boolean or function. But if you think so, I could refactor it to work that way.
I'm not so sure about that. While the error boundary is integral with suspense, I don't believe there's anything in suspense that says you can't decide an error state is part of state instead. |
It’s mentioned here: https://reactjs.org/docs/concurrent-mode-suspense.html#handling-errors since react renders before the promise resolves, I think it’s mandatory.
I think the boolean or function api would be more in line with how other flags handle these situations. If I’m right about suspense, the useErrorBoundary doesn’t do anything here, so if you set it to false (or a function), it should have no effect. We can also loop in @tannerlinsley |
That only documents how React uses an error boundary to catch errors thrown in components instead of catch. Not an assertion that all suspense uses cannot render instead of throwing. Suspense works by throwing a promise and then re-rendering when that is fulfilled, at which point you render the full component. I will concede that it's possible that when a promise is rejected React could consider that thrown, instead of rendering the component and expecting the However that doesn't actually matter because the suspense implementation in useBaseQuery actually catches any error and does not re-throw it. As a result we're guaranteed that in the suspense case, the thing that the So it's entirely possible to chose not to throw an error, even when using suspense. That said handling |
TkDodo
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thank you, this is great work - my comments are mostly stylistic changes 😅 , but please see the last comment 👍
Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>
Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>
Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>
Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>
|
I think the chances are rare that it will be a breaking change, and if it is, it can easily be fixed. This is one of those confusing moments in semver… I personally do not think this warrants a major version. If anyone does, then they’re going to be waiting a very long time to get the major release. Push it. |
|
I've updated shouldThrowError so a |
TkDodo
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks really good already, thank you 🚀
TkDodo
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
awesome, thank you 🙌
|
🎉 This PR is included in version 3.23.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
This adds a
shouldThrowErroroption that can be used whenuseErrorBoundary/suspenseto decide whether an error should be thrown or handled locally on theerrorstate.It's nice to have errors thrown to error boundaries. However sometimes you have some errors you do want to handle within the component instead of having them thrown to external boundaries.
e.g. In a product list page you may want to handle an "invalid product filter" from the server with an in-list error instead of it being thrown out to an error boundary. But you still may want to use an error boundary so 500 errors from the server and network errors still get thrown out to a shared error boundary.